github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/templates/cluster_test.go (about)

     1  package templates
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/projectdiscovery/nuclei/v2/pkg/protocols/dns"
     7  	"github.com/projectdiscovery/nuclei/v2/pkg/protocols/http"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestClusterTemplates(t *testing.T) {
    12  	t.Run("http-cluster-get", func(t *testing.T) {
    13  		tp1 := &Template{Path: "first.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}"}}}}
    14  		tp2 := &Template{Path: "second.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}"}}}}
    15  		tpls := []*Template{tp1, tp2}
    16  		// cluster 0
    17  		expected := []*Template{tp1, tp2}
    18  		got := Cluster(tpls)[0]
    19  		require.ElementsMatchf(t, expected, got, "different %v %v", len(expected), len(got))
    20  	})
    21  	t.Run("no-http-cluster", func(t *testing.T) {
    22  		tp1 := &Template{Path: "first.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}/random"}}}}
    23  		tp2 := &Template{Path: "second.yaml", RequestsHTTP: []*http.Request{{Path: []string{"{{BaseURL}}/another"}}}}
    24  		tpls := []*Template{tp1, tp2}
    25  		expected := [][]*Template{{tp1}, {tp2}}
    26  		got := Cluster(tpls)
    27  		require.ElementsMatch(t, expected, got)
    28  	})
    29  	t.Run("dns-cluster", func(t *testing.T) {
    30  		tp1 := &Template{Path: "first.yaml", RequestsDNS: []*dns.Request{{Name: "{{Hostname}}"}}}
    31  		tp2 := &Template{Path: "second.yaml", RequestsDNS: []*dns.Request{{Name: "{{Hostname}}"}}}
    32  		tpls := []*Template{tp1, tp2}
    33  		// cluster 0
    34  		expected := []*Template{tp1, tp2}
    35  		got := Cluster(tpls)[0]
    36  		require.ElementsMatch(t, got, expected)
    37  	})
    38  }