From 820b43916a8c9d1d38f0cd8022dc7ea6f7de4f8b Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Thu, 27 Jul 2017 15:51:56 -0400 Subject: [PATCH] Adds a new StringHTML Render method This new `StringHTML()` method will allow users to render HTML without the need of an actual file on disk. Example: ``` r.GET("/saludos", func(c *gin.Context) { c.StringHTML(200, "Hola amigo!") }) ``` Signed-off-by: Salim Afiune --- context.go | 5 +++++ context_test.go | 13 +++++++++++++ render/render_test.go | 13 +++++++++++++ render/text.go | 26 +++++++++++++++++++++++--- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index 35ca8d2e..45407b25 100644 --- a/context.go +++ b/context.go @@ -665,6 +665,11 @@ func (c *Context) YAML(code int, obj interface{}) { c.Render(code, render.YAML{Data: obj}) } +// StringHTML renders the provided content by setting the Content-Type as "text/html". +func (c *Context) StringHTML(code int, content string, values ...interface{}) { + c.Render(code, render.StringHTML{Format: content, Data: values}) +} + // String writes the given string into the response body. func (c *Context) String(code int, format string, values ...interface{}) { c.Render(code, render.String{Format: format, Data: values}) diff --git a/context_test.go b/context_test.go index 15569bf2..c2b98e22 100644 --- a/context_test.go +++ b/context_test.go @@ -733,6 +733,19 @@ func TestContextRenderString(t *testing.T) { assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/plain; charset=utf-8") } +// TestContextStringHTML tests that the response is returned +// with Content-Type set to text/html +func TestContextRenderStringHTML(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + + c.StringHTML(201, "

test %s %d

", "string", 2) + + assert.Equal(t, w.Code, 201) + assert.Equal(t, w.Body.String(), "

test string 2

") + assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8") +} + // Tests that no String is rendered if code is 204 func TestContextRenderNoContentString(t *testing.T) { w := httptest.NewRecorder() diff --git a/render/render_test.go b/render/render_test.go index e4df5b6d..8be0ba0c 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -161,6 +161,19 @@ func TestRenderString(t *testing.T) { assert.Equal(t, w.Header().Get("Content-Type"), "text/plain; charset=utf-8") } +func TestRenderStringHTML(t *testing.T) { + w := httptest.NewRecorder() + + err := (StringHTML{ + Format: "

Hola mi %s numero %d

", + Data: []interface{}{"amigo", 1}, + }).Render(w) + + assert.NoError(t, err) + assert.Equal(t, w.Body.String(), "

Hola mi amigo numero 1

") + assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8") +} + func TestRenderHTMLTemplate(t *testing.T) { w := httptest.NewRecorder() templ := template.Must(template.New("t").Parse(`Hello {{.name}}`)) diff --git a/render/text.go b/render/text.go index 74cd26be..a0dcbdfa 100644 --- a/render/text.go +++ b/render/text.go @@ -18,7 +18,7 @@ type String struct { var plainContentType = []string{"text/plain; charset=utf-8"} func (r String) Render(w http.ResponseWriter) error { - WriteString(w, r.Format, r.Data) + WriteString(w, r.Format, r.Data, false) return nil } @@ -26,11 +26,31 @@ func (r String) WriteContentType(w http.ResponseWriter) { writeContentType(w, plainContentType) } -func WriteString(w http.ResponseWriter, format string, data []interface{}) { - writeContentType(w, plainContentType) +func WriteString(w http.ResponseWriter, format string, data []interface{}, html bool) { + if html { + writeContentType(w, htmlContentType) + } else { + writeContentType(w, plainContentType) + } if len(data) > 0 { fmt.Fprintf(w, format, data...) } else { io.WriteString(w, format) } } + +// StringHTML will function exactly the same as the String struct +// but it will inject an html ContentType to the response +type StringHTML struct { + Format string + Data []interface{} +} + +func (r StringHTML) Render(w http.ResponseWriter) error { + WriteString(w, r.Format, r.Data, true) + return nil +} + +func (r StringHTML) WriteContentType(w http.ResponseWriter) { + writeContentType(w, htmlContentType) +}