github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/protocols/common/variables/variables_test.go (about)

     1  package variables
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  func TestVariablesEvaluate(t *testing.T) {
    13  	data := `a2: "{{md5('test')}}"
    14  a3: "this_is_random_text"
    15  a4: "{{date_time('%Y-%M-%D')}}"
    16  a5: "{{reverse(hostname)}}"
    17  a6: "123456"`
    18  
    19  	variables := Variable{}
    20  	err := yaml.Unmarshal([]byte(data), &variables)
    21  	require.NoError(t, err, "could not unmarshal variables")
    22  
    23  	result := variables.Evaluate(map[string]interface{}{"hostname": "google.com"})
    24  	a4 := time.Now().Format("2006-01-02")
    25  	require.Equal(t, map[string]interface{}{"a2": "098f6bcd4621d373cade4e832627b4f6", "a3": "this_is_random_text", "a4": a4, "a5": "moc.elgoog", "a6": "123456"}, result, "could not get correct elements")
    26  
    27  	// json
    28  	data = `{
    29    "a2": "{{md5('test')}}",
    30    "a3": "this_is_random_text",
    31    "a4": "{{date_time('%Y-%M-%D')}}",
    32    "a5": "{{reverse(hostname)}}",
    33    "a6": "123456"
    34  }`
    35  	variables = Variable{}
    36  	err = json.Unmarshal([]byte(data), &variables)
    37  	require.NoError(t, err, "could not unmarshal json variables")
    38  
    39  	result = variables.Evaluate(map[string]interface{}{"hostname": "google.com"})
    40  	a4 = time.Now().Format("2006-01-02")
    41  	require.Equal(t, map[string]interface{}{"a2": "098f6bcd4621d373cade4e832627b4f6", "a3": "this_is_random_text", "a4": a4, "a5": "moc.elgoog", "a6": "123456"}, result, "could not get correct elements")
    42  
    43  }