From 749cacedc9ea5b3b54fb2c9eac230067f2aabb35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Wed, 13 Nov 2024 14:56:55 +0100 Subject: [PATCH] add tests for c.JSON and c.HTML renderers --- ron.go | 8 ++++-- ron_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 ron_test.go diff --git a/ron.go b/ron.go index e6be920..dc88158 100644 --- a/ron.go +++ b/ron.go @@ -20,7 +20,7 @@ type Engine struct { Renderer *Render } -func (c *Context) JSON(code int, data Data) { +func (c *Context) JSON(code int, data any) { c.W.WriteHeader(code) c.W.Header().Set("Content-Type", "application/json") encoder := json.NewEncoder(c.W) @@ -31,9 +31,13 @@ func (c *Context) JSON(code int, data Data) { func (c *Context) HTML(code int, name string, data Data) { c.W.WriteHeader(code) - c.E.Renderer.Template(c.W, name, &TemplateData{ + c.W.Header().Set("Content-Type", "text/html; charset=utf-8") + err := c.E.Renderer.Template(c.W, name, &TemplateData{ Data: data, }) + if err != nil { + http.Error(c.W, err.Error(), http.StatusInternalServerError) + } } func New() *Engine { diff --git a/ron_test.go b/ron_test.go new file mode 100644 index 0000000..6adc7ab --- /dev/null +++ b/ron_test.go @@ -0,0 +1,76 @@ +package ron + +import ( + "net/http" + "net/http/httptest" + "os" + "testing" +) + +type Foo struct { + Bar string `json:"bar"` + Taz int `json:"taz"` + Car *string `json:"car"` +} + +func Test_JSON(t *testing.T) { + rr := httptest.NewRecorder() + c := &Context{ + W: rr, + } + + expected := `{"bar":"bar","taz":30,"car":null}` + + c.JSON(http.StatusOK, Foo{ + Bar: "bar", + Taz: 30, + Car: nil, + }) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status) + } + + if rr.Header().Get("Content-Type") != "application/json" { + t.Errorf("Expected Content-Type: application/json, Actual: %s", c.W.Header().Get("Content-Type")) + } + + if rr.Body.String() != string(expected)+"\n" { + t.Errorf("Expected: %s, Actual: %s", string(expected), rr.Body.String()) + } +} + +func Test_HTML(t *testing.T) { + os.Mkdir("templates", os.ModePerm) + f, _ := os.Create("templates/page.index.gohtml") + f.WriteString("

{{.Data.heading1}}

{{.Data.heading2}}

") + f.Close() + defer os.RemoveAll("templates") + + rr := httptest.NewRecorder() + c := &Context{ + W: rr, + E: &Engine{ + Renderer: NewHTMLRender(), + }, + } + + expected := `

foo

bar

` + + c.HTML(http.StatusOK, "page.index.gohtml", Data{ + "heading1": "foo", + "heading2": "bar", + }) + + if status := rr.Code; status != http.StatusOK { + t.Errorf("Expected status code: %d, Actual: %d", http.StatusOK, status) + } + + if rr.Header().Get("Content-Type") != "text/html; charset=utf-8" { + t.Errorf("Expected Content-Type: text/html; charset=utf-8, Actual: %s", c.W.Header().Get("Content-Type")) + } + + if rr.Body.String() != string(expected) { + t.Errorf("Expected: %s, Actual: %s", string(expected), rr.Body.String()) + } +}