Path: blob/dev/pkg/protocols/common/expressions/expressions_test.go
2072 views
package expressions12import (3"testing"45"github.com/stretchr/testify/require"6)78func TestEvaluate(t *testing.T) {9items := []struct {10input string11expected string12extra map[string]interface{}13}{14{input: "{{url_encode('test}aaa')}}", expected: "test%7Daaa", extra: map[string]interface{}{}},15{input: "{{hex_encode('PING')}}", expected: "50494e47", extra: map[string]interface{}{}},16{input: "{{hex_encode('{{')}}", expected: "7b7b", extra: map[string]interface{}{}},17{input: `{{concat("{{", 123, "*", 123, "}}")}}`, expected: "{{123*123}}", extra: map[string]interface{}{}},18{input: `{{concat("{{", "123*123", "}}")}}`, expected: "{{123*123}}", extra: map[string]interface{}{}},19{input: `{{"{{" + '123*123' + "}}"}}`, expected: `{{"{{" + '123*123' + "}}"}}`, extra: map[string]interface{}{}},20{input: `{{a + '123*123' + b}}`, expected: `aa123*123bb`, extra: map[string]interface{}{"a": "aa", "b": "bb"}},21{input: `{{concat(123,'*',123)}}`, expected: "123*123", extra: map[string]interface{}{}},22{input: `{{1+1}}`, expected: "{{1+1}}", extra: map[string]interface{}{}},23{input: `{{"1"+"1"}}`, expected: `{{"1"+"1"}}`, extra: map[string]interface{}{}},24{input: `{{"1" + '*' + "1"}}`, expected: `{{"1" + '*' + "1"}}`, extra: map[string]interface{}{}},25{input: `{{"a" + 'b' + "c"}}`, expected: `{{"a" + 'b' + "c"}}`, extra: map[string]interface{}{}},26{input: `{{10*2}}`, expected: `{{10*2}}`, extra: map[string]interface{}{}},27{input: `{{10/2}}`, expected: `{{10/2}}`, extra: map[string]interface{}{}},28{input: `{{10-2}}`, expected: `{{10-2}}`, extra: map[string]interface{}{}},29{input: "test", expected: "test", extra: map[string]interface{}{}},30{input: "{{hex_encode(Item)}}", expected: "50494e47", extra: map[string]interface{}{"Item": "PING"}},31{input: "{{hex_encode(Item)}}\r\n", expected: "50494e47\r\n", extra: map[string]interface{}{"Item": "PING"}},32{input: "{{someTestData}}{{hex_encode('PING')}}", expected: "{{someTestData}}50494e47", extra: map[string]interface{}{}},33{input: `_IWP_JSON_PREFIX_{{base64("{\"iwp_action\":\"add_site\",\"params\":{\"username\":\"\"}}")}}`, expected: "_IWP_JSON_PREFIX_eyJpd3BfYWN0aW9uIjoiYWRkX3NpdGUiLCJwYXJhbXMiOnsidXNlcm5hbWUiOiIifX0=", extra: map[string]interface{}{}},34{input: "{{}}", expected: "{{}}", extra: map[string]interface{}{}},35{input: `"{{hex_encode('PING')}}"`, expected: `"50494e47"`, extra: map[string]interface{}{}},36}37for _, item := range items {38value, err := Evaluate(item.input, item.extra)39require.Nil(t, err, "could not evaluate helper")4041require.Equal(t, item.expected, value, "could not get correct expression")42}43}4445func TestEval(t *testing.T) {46items := []struct {47input string48values map[string]interface{}49expected interface{}50}{51{input: "'a' + 'a'", values: nil, expected: "aa"},52{input: "10 + to_number(b)", values: map[string]interface{}{"b": "4"}, expected: float64(14)},53}54for _, item := range items {55value, err := Eval(item.input, item.values)56require.Nil(t, err, "could not evaluate helper")57require.Equal(t, item.expected, value, "could not get correct expression")58}59}606162