From 55a564df6dd34f4745ee8ba4d9c5671064b47992 Mon Sep 17 00:00:00 2001 From: Tw Date: Tue, 16 Jan 2018 09:27:55 +0800 Subject: [PATCH] template: add extension filter test and simplify test code (#1996) Signed-off-by: Tw --- caddyhttp/templates/templates_test.go | 159 ++++++++++------------ caddyhttp/templates/testdata/as_it_is.txt | 1 + 2 files changed, 70 insertions(+), 90 deletions(-) create mode 100644 caddyhttp/templates/testdata/as_it_is.txt diff --git a/caddyhttp/templates/templates_test.go b/caddyhttp/templates/templates_test.go index 839c39f76..289f1a85d 100644 --- a/caddyhttp/templates/templates_test.go +++ b/caddyhttp/templates/templates_test.go @@ -62,100 +62,79 @@ func TestTemplates(t *testing.T) { BufPool: &sync.Pool{New: func() interface{} { return new(bytes.Buffer) }}, } - // Test tmpl on /photos/test.html - req, err := http.NewRequest("GET", "/photos/test.html", nil) - if err != nil { - t.Fatalf("Test: Could not create HTTP request: %v", err) - } - req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL)) - - rec := httptest.NewRecorder() - - tmpl.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK) - } - - respBody := rec.Body.String() - expectedBody := `test page

Header title

- -` - - if respBody != expectedBody { - t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody) - } - - // Test tmpl on /images/img.htm - req, err = http.NewRequest("GET", "/images/img.htm", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) - } - req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL)) - - rec = httptest.NewRecorder() - - tmpl.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK) - } - - respBody = rec.Body.String() - expectedBody = `img

Header title

- -` - - if respBody != expectedBody { - t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody) - } - - // Test tmpl on /images/img2.htm - req, err = http.NewRequest("GET", "/images/img2.htm", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) - } - req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL)) - - rec = httptest.NewRecorder() - - tmpl.ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK) - } - - respBody = rec.Body.String() - expectedBody = `img{{.Include "header.html"}} -` - - if respBody != expectedBody { - t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody) - } - - // Test tmplroot on /root.html - req, err = http.NewRequest("GET", "/root.html", nil) - if err != nil { - t.Fatalf("Could not create HTTP request: %v", err) - } - req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL)) - - 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 { - t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK) - } - - respBody = rec.Body.String() - expectedBody = `root

Header title

+ for _, c := range []struct { + tpl Templates + req string + respCode int + res string + }{ + { + tpl: tmpl, + req: "/photos/test.html", + respCode: http.StatusOK, + res: `test page

Header title

-` +`, + }, - if respBody != expectedBody { - t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody) + { + tpl: tmpl, + req: "/images/img.htm", + respCode: http.StatusOK, + res: `img

Header title

+ +`, + }, + + { + tpl: tmpl, + req: "/images/img2.htm", + respCode: http.StatusOK, + res: `img{{.Include "header.html"}} +`, + }, + + { + tpl: tmplroot, + req: "/root.html", + respCode: http.StatusOK, + res: `root

Header title

+ +`, + }, + + // test extension filter + { + tpl: tmplroot, + req: "/as_it_is.txt", + respCode: http.StatusOK, + res: `as it is{{.Include "header.html"}} +`, + }, + } { + c := c + t.Run("", func(t *testing.T) { + req, err := http.NewRequest("GET", c.req, nil) + if err != nil { + t.Fatalf("Test: Could not create HTTP request: %v", err) + } + req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL)) + + rec := httptest.NewRecorder() + + c.tpl.ServeHTTP(rec, req) + + if rec.Code != c.respCode { + t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, c.respCode) + } + + respBody := rec.Body.String() + if respBody != c.res { + t.Fatalf("Test: the expected body %v is different from the response one: %v", c.res, respBody) + } + }) } } diff --git a/caddyhttp/templates/testdata/as_it_is.txt b/caddyhttp/templates/testdata/as_it_is.txt new file mode 100644 index 000000000..487ee7273 --- /dev/null +++ b/caddyhttp/templates/testdata/as_it_is.txt @@ -0,0 +1 @@ +as it is{{.Include "header.html"}}