nuclei/pkg/templates/templates_test.go
Dwi Siswanto 622c5503fa
perf(*): replace encoding/json w/ sonic or go-json (fallback) (#6019)
* perf(*): replace `encoding/json` w/ sonic

Signed-off-by: Dwi Siswanto <git@dw1.io>

* feat(utils): add `json` pkg (sonic wrapper)

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore(*): use `sonic` wrapper instead

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore(*): replace `sonic.ConfigStd` -> `json` (wrapper)

Signed-off-by: Dwi Siswanto <git@dw1.io>

* test(model): adjust expected marshal'd JSON

Signed-off-by: Dwi Siswanto <git@dw1.io>

* feat(json): dynamic backend; `sonic` -> `go-json` (fallback)

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore(json): merge config - as its not usable

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore(json): rm go version constraints

Signed-off-by: Dwi Siswanto <git@dw1.io>

* chore: go mod tidy

Signed-off-by: Dwi Siswanto <git@dw1.io>

---------

Signed-off-by: Dwi Siswanto <git@dw1.io>
2025-02-11 03:01:37 +05:30

37 lines
1.2 KiB
Go

package templates
import (
"os"
"testing"
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestTemplateStruct(t *testing.T) {
templatePath := "./tests/match-1.yaml"
bin, err := os.ReadFile(templatePath)
require.Nil(t, err, "failed to load example template")
var yamlTemplate Template
err = yaml.Unmarshal(bin, &yamlTemplate)
require.Nil(t, err, "failed to unmarshal yaml template")
jsonBin, err := json.Marshal(yamlTemplate)
require.Nil(t, err, "failed to marshal template to json")
var jsonTemplate Template
err = json.Unmarshal(jsonBin, &jsonTemplate)
require.Nil(t, err, "failed to unmarshal json template")
templatePath = "./tests/json-template.json"
bin, err = os.ReadFile(templatePath)
require.Nil(t, err, "failed to load example template")
jsonTemplate = Template{}
err = json.Unmarshal(bin, &jsonTemplate)
require.Nil(t, err, "failed to unmarshal json template")
yamlBin, err := yaml.Marshal(jsonTemplate)
require.Nil(t, err, "failed to marshal template to yaml")
yamlTemplate = Template{}
err = yaml.Unmarshal(yamlBin, &yamlTemplate)
require.Nil(t, err, "failed to unmarshal yaml template")
}