Skip to content

Commit eb2db6c

Browse files
committed
Merge branch 'master-allow-html-buffers' of https://github.com/GannettDigital/echo into GannettDigital-master-allow-html-buffers
2 parents f10daac + ceac2dd commit eb2db6c

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

context.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ type (
106106
// HTML sends an HTTP response with status code.
107107
HTML(int, string) error
108108

109+
// HTMLBlob sends an HTTP blob response with status code.
110+
HTMLBlob(int, []byte) error
111+
109112
// String sends a string response with status code.
110113
String(int, string) error
111114

@@ -356,17 +359,15 @@ func (c *context) Render(code int, name string, data interface{}) (err error) {
356359
}
357360

358361
func (c *context) HTML(code int, html string) (err error) {
359-
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
360-
c.response.WriteHeader(code)
361-
_, err = c.response.Write([]byte(html))
362-
return
362+
return c.Blob(code, MIMETextHTMLCharsetUTF8, []byte(html))
363+
}
364+
365+
func (c *context) HTMLBlob(code int, b []byte) (err error) {
366+
return c.Blob(code, MIMETextHTMLCharsetUTF8, b)
363367
}
364368

365369
func (c *context) String(code int, s string) (err error) {
366-
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
367-
c.response.WriteHeader(code)
368-
_, err = c.response.Write([]byte(s))
369-
return
370+
return c.Blob(code, MIMETextPlainCharsetUTF8, []byte(s))
370371
}
371372

372373
func (c *context) JSON(code int, i interface{}) (err error) {

context_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ func TestContext(t *testing.T) {
126126
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
127127
}
128128

129+
// HTMLBlob
130+
rec = httptest.NewRecorder()
131+
c = e.NewContext(req, rec).(*context)
132+
err = c.HTMLBlob(http.StatusOK, []byte("Hello, <strong>World!</strong>"))
133+
if assert.NoError(t, err) {
134+
assert.Equal(t, http.StatusOK, rec.Code)
135+
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
136+
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
137+
}
138+
129139
// Stream
130140
rec = httptest.NewRecorder()
131141
c = e.NewContext(req, rec).(*context)

0 commit comments

Comments
 (0)