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