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

     1  package templates
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  func TestTemplateStruct(t *testing.T) {
    13  	templatePath := "./tests/match-1.yaml"
    14  	bin, err := os.ReadFile(templatePath)
    15  	require.Nil(t, err, "failed to load example template")
    16  	var yamlTemplate Template
    17  	err = yaml.Unmarshal(bin, &yamlTemplate)
    18  	require.Nil(t, err, "failed to unmarshal yaml template")
    19  	jsonBin, err := json.Marshal(yamlTemplate)
    20  	require.Nil(t, err, "failed to marshal template to json")
    21  	var jsonTemplate Template
    22  	err = json.Unmarshal(jsonBin, &jsonTemplate)
    23  	require.Nil(t, err, "failed to unmarshal json template")
    24  
    25  	templatePath = "./tests/json-template.json"
    26  	bin, err = os.ReadFile(templatePath)
    27  	require.Nil(t, err, "failed to load example template")
    28  	jsonTemplate = Template{}
    29  	err = json.Unmarshal(bin, &jsonTemplate)
    30  	require.Nil(t, err, "failed to unmarshal json template")
    31  	yamlBin, err := yaml.Marshal(jsonTemplate)
    32  	require.Nil(t, err, "failed to marshal template to yaml")
    33  	yamlTemplate = Template{}
    34  	err = yaml.Unmarshal(yamlBin, &yamlTemplate)
    35  	require.Nil(t, err, "failed to unmarshal yaml template")
    36  }