forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacer_test.go
141 lines (136 loc) · 3.75 KB
/
replacer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package replacer
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestReplacerReplace(t *testing.T) {
tests := []struct {
name string
template string
values map[string]interface{}
expected string
}{
{
name: "Invalid arguments",
template: "",
values: nil,
expected: "",
},
{
name: "Nested",
template: "{{base64_encode('{{test}}')}}",
values: map[string]interface{}{"test": "random"},
expected: "{{base64_encode('random')}}",
},
{
name: "Basic",
template: "{{test}} §hello§ {{data}}",
values: map[string]interface{}{"test": "random", "hello": "world"},
expected: "random world {{data}}",
},
{
name: "No template variables",
template: "Nothing to replace",
values: map[string]interface{}{"test": "random", "hello": "world"},
expected: "Nothing to replace",
},
{
name: "Nested variable",
template: "{{§var1§}} and §{{var2}}§",
values: map[string]interface{}{"var1": "variable 1", "var2": "variable 2"},
expected: "{{variable 1}} and §variable 2§",
},
{
name: "Space in variable name",
template: "{{var 1}} has a space",
values: map[string]interface{}{"var 1": "variable 1"},
expected: "variable 1 has a space",
},
{
name: "Escaped marker in template",
template: "{{\\§var 1\\§}}",
values: map[string]interface{}{"\\§var 1\\§": "variable 1"},
expected: "variable 1",
},
{
name: "Escaping no marker in template",
template: "{{\\§var 1\\§}}",
values: map[string]interface{}{"var 1": "variable 1"},
expected: "{{\\§var 1\\§}}",
},
{
name: "Empty variable name",
template: "{{}} §§ no vars here",
values: map[string]interface{}{"var 1": "variable 1"},
expected: "{{}} §§ no vars here",
},
{
name: "Multiple replacement",
template: "{{var1}} and §var1§ and another {{var2}}",
values: map[string]interface{}{"var1": "first variable", "var2": "second variable"},
expected: "first variable and first variable and another second variable",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.expected, Replace(test.template, test.values))
})
}
}
func TestReplacerReplaceOne(t *testing.T) {
tests := []struct {
name string
template string
key string
value interface{}
expected string
}{
{
name: "Basic",
template: "once upon a time there was a {{var1}}",
key: "var1",
value: "variable 1",
expected: "once upon a time there was a variable 1",
},
{
name: "Basic Multiple Vars",
template: "once upon a time there was a {{var1}} and a §var2§",
key: "var2",
value: "variable 2",
expected: "once upon a time there was a {{var1}} and a variable 2",
},
{
name: "Missing key",
template: "once upon a time there was a {{var1}}",
key: "",
value: "variable 1",
expected: "once upon a time there was a {{var1}}",
},
{
name: "Replacement value empty",
template: "{{var1}}nothing{{var1}} to{{var1}} see",
key: "var1",
value: "",
expected: "nothing{{var1}} to{{var1}} see",
},
{
name: "Empty key and value different markers",
template: "{{}}both§§ the{{}} 1st and 2nd markers are replaced",
key: "",
value: "",
expected: "both the{{}} 1st and 2nd markers are replaced",
},
{
name: "Empty key and value same marker",
template: "{{}}only{{}} the first marker is replaced{{}}",
key: "",
value: "",
expected: "only{{}} the first marker is replaced{{}}",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
require.Equal(t, test.expected, ReplaceOne(test.template, test.key, test.value))
})
}
}