| 1 | package render |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestRenderPartial(t *testing.T) { |
| 10 | render := New(Options{ |
| 11 | Directory: "testdata/partials", |
| 12 | Layout: "layout", |
| 13 | }) |
| 14 | |
| 15 | var renErr error |
| 16 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | renErr = render.HTML(w, http.StatusOK, "content", "gophers") |
| 18 | }) |
| 19 | |
| 20 | res := httptest.NewRecorder() |
| 21 | req, err := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 22 | if err != nil { |
| 23 | t.Fatalf("couldn't create a request. err = %s", err) |
| 24 | } |
| 25 | h.ServeHTTP(res, req) |
| 26 | |
| 27 | expectNil(t, renErr) |
| 28 | expect(t, res.Body.String(), "before gophers\n<h1>during</h1>\nafter gophers\n") |
| 29 | } |
| 30 | |
| 31 | func TestRenderPartialRequirePartialsOff(t *testing.T) { |
| 32 | render := New(Options{ |
| 33 | Directory: "testdata/partials", |
| 34 | Layout: "layout", |
| 35 | RequirePartials: false, |
| 36 | }) |
| 37 | |
| 38 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 39 | _ = render.HTML(w, http.StatusOK, "content-partial", "gophers") |
| 40 | }) |
| 41 | |
| 42 | res := httptest.NewRecorder() |
| 43 | req, err := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 44 | if err != nil { |
| 45 | t.Fatalf("couldn't create a request. err = %s", err) |
| 46 | } |
| 47 | h.ServeHTTP(res, req) |
| 48 | |
| 49 | expect(t, res.Body.String(), "\n<h1>during</h1>\nafter gophers\n") |
| 50 | } |
| 51 | |
| 52 | func TestRenderPartialRequirePartialsOn(t *testing.T) { |
| 53 | render := New(Options{ |
| 54 | Directory: "testdata/partials", |
| 55 | Layout: "layout", |
| 56 | RequirePartials: true, |
| 57 | }) |
| 58 | |
| 59 | h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | _ = render.HTML(w, http.StatusOK, "content-partial", "gophers") |
| 61 | }) |
| 62 | |
| 63 | res := httptest.NewRecorder() |
| 64 | req, err := http.NewRequestWithContext(ctx, "GET", "/foo", nil) |
| 65 | if err != nil { |
| 66 | t.Fatalf("couldn't create a request. err = %s", err) |
| 67 | } |
| 68 | h.ServeHTTP(res, req) |
| 69 | |
| 70 | expect(t, res.Body.String(), "template: layout:1:3: executing \"layout\" at <partial \"before\">: error calling partial: html/template: \"before-content-partial\" is undefined\n") |
| 71 | } |