Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Łukasz Nowak
caddy
Commits
790c842f
Commit
790c842f
authored
Apr 18, 2017
by
Tw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
template: add test for custom function
Signed-off-by:
Tw
<
tw19881113@gmail.com
>
parent
f28a159b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
12 additions
and
3 deletions
+12
-3
caddyhttp/httpserver/context.go
caddyhttp/httpserver/context.go
+1
-2
caddyhttp/httpserver/context_test.go
caddyhttp/httpserver/context_test.go
+8
-0
caddyhttp/templates/templates_test.go
caddyhttp/templates/templates_test.go
+2
-0
caddyhttp/templates/testdata/root.html
caddyhttp/templates/testdata/root.html
+1
-1
No files found.
caddyhttp/httpserver/context.go
View file @
790c842f
...
...
@@ -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
}
...
...
caddyhttp/httpserver/context_test.go
View file @
790c842f
...
...
@@ -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
)
...
...
caddyhttp/templates/templates_test.go
View file @
790c842f
...
...
@@ -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
{
...
...
caddyhttp/templates/testdata/root.html
View file @
790c842f
<!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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment