aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMatt Holt <[email protected]>2017-04-18 09:23:06 -0600
committerGitHub <[email protected]>2017-04-18 09:23:06 -0600
commit729e4f0239528e1c2ac2f64207aef769564fc697 (patch)
treedae7b74ab08a2477c1af99179ab949790830b457
parentf28a159b72bcf4ea92d979e2b5559018a6825722 (diff)
parent790c842fad876b81d390def66b276cab70568f65 (diff)
downloadcaddy-729e4f0239528e1c2ac2f64207aef769564fc697.tar.gz
caddy-729e4f0239528e1c2ac2f64207aef769564fc697.zip
Merge pull request #1594 from tw4452852/templateFuncsTest
template: add test for custom function
-rw-r--r--caddyhttp/httpserver/context.go3
-rw-r--r--caddyhttp/httpserver/context_test.go8
-rw-r--r--caddyhttp/templates/templates_test.go2
-rw-r--r--caddyhttp/templates/testdata/root.html2
4 files changed, 12 insertions, 3 deletions
diff --git a/caddyhttp/httpserver/context.go b/caddyhttp/httpserver/context.go
index 13feedc8a..d7d233c5d 100644
--- a/caddyhttp/httpserver/context.go
+++ b/caddyhttp/httpserver/context.go
@@ -264,8 +264,7 @@ func ContextInclude(filename string, ctx interface{}, fs http.FileSystem) (strin
return "", err
}
- tpl := template.New(filename).Funcs(TemplateFuncs)
- tpl, err = tpl.Parse(string(body))
+ tpl, err := template.New(filename).Funcs(TemplateFuncs).Parse(string(body))
if err != nil {
return "", err
}
diff --git a/caddyhttp/httpserver/context_test.go b/caddyhttp/httpserver/context_test.go
index b965e214b..50d9c67ff 100644
--- a/caddyhttp/httpserver/context_test.go
+++ b/caddyhttp/httpserver/context_test.go
@@ -72,8 +72,16 @@ func TestInclude(t *testing.T) {
shouldErr: true,
expectedErrorContent: `type httpserver.Context`,
},
+ // Test 4 - all good, with custom function
+ {
+ fileContent: `hello {{ caddy }}`,
+ expectedContent: "hello caddy",
+ shouldErr: false,
+ expectedErrorContent: "",
+ },
}
+ TemplateFuncs["caddy"] = func() string { return "caddy" }
for i, test := range tests {
testPrefix := getTestPrefix(i)
diff --git a/caddyhttp/templates/templates_test.go b/caddyhttp/templates/templates_test.go
index 841cf2027..f190599c3 100644
--- a/caddyhttp/templates/templates_test.go
+++ b/caddyhttp/templates/templates_test.go
@@ -121,6 +121,8 @@ func TestTemplates(t *testing.T) {
rec = httptest.NewRecorder()
+ // register custom function which is used in template
+ httpserver.TemplateFuncs["root"] = func() string { return "root" }
tmplroot.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
diff --git a/caddyhttp/templates/testdata/root.html b/caddyhttp/templates/testdata/root.html
index e1720e726..ccc1f6349 100644
--- a/caddyhttp/templates/testdata/root.html
+++ b/caddyhttp/templates/testdata/root.html
@@ -1 +1 @@
-<!DOCTYPE html><html><head><title>root</title></head><body>{{.Include "header.html"}}</body></html>
+<!DOCTYPE html><html><head><title>{{ root }}</title></head><body>{{.Include "header.html"}}</body></html>