Add load html file and func map.
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
a40699e07f
commit
6d071c1d36
27
gin_test.go
27
gin_test.go
@ -23,12 +23,21 @@ func formatAsDate(t time.Time) string {
|
|||||||
|
|
||||||
func setupHTMLFiles(t *testing.T) func() {
|
func setupHTMLFiles(t *testing.T) func() {
|
||||||
go func() {
|
go func() {
|
||||||
|
SetMode(TestMode)
|
||||||
router := New()
|
router := New()
|
||||||
router.Delims("{[{", "}]}")
|
router.Delims("{[{", "}]}")
|
||||||
router.LoadHTMLFiles("./fixtures/basic/hello.tmpl")
|
router.SetFuncMap(template.FuncMap{
|
||||||
|
"formatAsDate": formatAsDate,
|
||||||
|
})
|
||||||
|
router.LoadHTMLFiles("./fixtures/basic/hello.tmpl", "./fixtures/basic/raw.tmpl")
|
||||||
router.GET("/test", func(c *Context) {
|
router.GET("/test", func(c *Context) {
|
||||||
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
||||||
})
|
})
|
||||||
|
router.GET("/raw", func(c *Context) {
|
||||||
|
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
|
||||||
|
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
||||||
|
})
|
||||||
|
})
|
||||||
router.Run(":8888")
|
router.Run(":8888")
|
||||||
}()
|
}()
|
||||||
t.Log("waiting 1 second for server startup")
|
t.Log("waiting 1 second for server startup")
|
||||||
@ -74,7 +83,7 @@ func TestLoadHTMLGlob(t *testing.T) {
|
|||||||
td()
|
td()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadHTMLFromFuncMap(t *testing.T) {
|
func TestLoadHTMLGlobFromFuncMap(t *testing.T) {
|
||||||
time.Now()
|
time.Now()
|
||||||
td := setupHTMLGlob(t)
|
td := setupHTMLGlob(t)
|
||||||
res, err := http.Get("http://127.0.0.1:8888/raw")
|
res, err := http.Get("http://127.0.0.1:8888/raw")
|
||||||
@ -129,6 +138,20 @@ func TestLoadHTMLFiles(t *testing.T) {
|
|||||||
td()
|
td()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFilesFuncMap(t *testing.T) {
|
||||||
|
time.Now()
|
||||||
|
td := setupHTMLFiles(t)
|
||||||
|
res, err := http.Get("http://127.0.0.1:8888/raw")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := ioutil.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "Date: 2017/07/01\n", string(resp[:]))
|
||||||
|
|
||||||
|
td()
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadHTMLReleaseMode(t *testing.T) {
|
func TestLoadHTMLReleaseMode(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user