Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 04:00 UTC
templates: Add tests for funcInclude and funcImport (#4357)
* Update tplcontext.go Add {{ render "/path/to/file.ext" $data }} via funcRender * Update tplcontext.go * Refactor funcInclude, add funcImport to enable {{block}} and {{template}} * Fix funcImport return of nil showing up in html * Update godocs for and * Add tests for funcInclude * Add tests for funcImport * os.RemoveAll -> os.Remove for TestFuncInclude and TestFuncImport
Tim Culverhouse committed
Sep 20, 2021 at 13:29 UTC
16f752125fb5d6fe46b9dea95eec8804bfdb24f8
1 file changed
+134
modules/caddyhttp/templates/tplcontext_test.go
+134
@@ -91,6 +91,140 @@ func TestCookie(t *testing.T) {
91
}
92
}
93
94
+func TestImport(t *testing.T) {
95
+ for i, test := range []struct {
96
+ fileContent string
97
+ fileName string
98
+ shouldErr bool
99
+ expect string
100
+ }{
101
+ {
102
+ // file exists, template is defined
103
+ fileContent: `{{ define "imported" }}text{{end}}`,
104
+ fileName: "file1",
105
+ shouldErr: false,
106
+ expect: `"imported"`,
107
+ },
108
+ {
109
+ // file does not exit
110
+ fileContent: "",
111
+ fileName: "",
112
+ shouldErr: true,
113
+ },
114
+ } {
115
+ context := getContextOrFail(t)
116
+ var absFilePath string
117
+
118
+ // create files for test case
119
+ if test.fileName != "" {
120
+ absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
121
+ if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
122
+ os.Remove(absFilePath)
123
+ t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
124
+ }
125
+ }
126
+
127
+ // perform test
128
+ context.NewTemplate("parent")
129
+ actual, err := context.funcImport(test.fileName)
130
+ templateWasDefined := strings.Contains(context.tpl.DefinedTemplates(), test.expect)
131
+ if err != nil {
132
+ if !test.shouldErr {
133
+ t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
134
+ }
135
+ } else if test.shouldErr {
136
+ t.Errorf("Test %d: Expected error but had none", i)
137
+ } else if !templateWasDefined && actual != "" {
138
+ // template should be defined, return value should be an empty string
139
+ t.Errorf("Test %d: Expected template %s to be define but got %s", i, test.expect, context.tpl.DefinedTemplates())
140
+
141
+ }
142
+
143
+ if absFilePath != "" {
144
+ if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
145
+ t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
146
+ }
147
+ }
148
+ }
149
+}
150
+
151
+func TestInclude(t *testing.T) {
152
+ for i, test := range []struct {
153
+ fileContent string
154
+ fileName string
155
+ shouldErr bool
156
+ expect string
157
+ args string
158
+ }{
159
+ {
160
+ // file exists, content is text only
161
+ fileContent: "text",
162
+ fileName: "file1",
163
+ shouldErr: false,
164
+ expect: "text",
165
+ },
166
+ {
167
+ // file exists, content is template
168
+ fileContent: "{{ if . }}text{{ end }}",
169
+ fileName: "file1",
170
+ shouldErr: false,
171
+ expect: "text",
172
+ },
173
+ {
174
+ // file does not exit
175
+ fileContent: "",
176
+ fileName: "",
177
+ shouldErr: true,
178
+ },
179
+ {
180
+ // args
181
+ fileContent: "{{ index .Args 0 }}",
182
+ fileName: "file1",
183
+ shouldErr: false,
184
+ args: "text",
185
+ expect: "text",
186
+ },
187
+ {
188
+ // args, reference arg out of range
189
+ fileContent: "{{ index .Args 1 }}",
190
+ fileName: "file1",
191
+ shouldErr: true,
192
+ args: "text",
193
+ },
194
+ } {
195
+ context := getContextOrFail(t)
196
+ var absFilePath string
197
+
198
+ // create files for test case
199
+ if test.fileName != "" {
200
+ absFilePath := filepath.Join(fmt.Sprintf("%s", context.Root), test.fileName)
201
+ if err := ioutil.WriteFile(absFilePath, []byte(test.fileContent), os.ModePerm); err != nil {
202
+ os.Remove(absFilePath)
203
+ t.Fatalf("Test %d: Expected no error creating file, got: '%s'", i, err.Error())
204
+ }
205
+ }
206
+
207
+ // perform test
208
+ actual, err := context.funcInclude(test.fileName, test.args)
209
+ if err != nil {
210
+ if !test.shouldErr {
211
+ t.Errorf("Test %d: Expected no error, got: '%s'", i, err)
212
+ }
213
+ } else if test.shouldErr {
214
+ t.Errorf("Test %d: Expected error but had none", i)
215
+ } else if actual != test.expect {
216
+ t.Errorf("Test %d: Expected %s but got %s", i, test.expect, actual)
217
+
218
+ }
219
+
220
+ if absFilePath != "" {
221
+ if err := os.Remove(absFilePath); err != nil && !os.IsNotExist(err) {
222
+ t.Fatalf("Test %d: Expected no error removing temporary test file, got: %v", i, err)
223
+ }
224
+ }
225
+ }
226
+}
227
+
228
func TestCookieMultipleCookies(t *testing.T) {
229
context := getContextOrFail(t)
230