Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ type (
// HTML sends an HTTP response with status code.
HTML(int, string) error

// HTMLBlob sends an HTTP blob response with status code.
HTMLBlob(int, []byte) error

// String sends a string response with status code.
String(int, string) error

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

func (c *context) HTML(code int, html string) (err error) {
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
c.response.WriteHeader(code)
_, err = c.response.Write([]byte(html))
return
return c.Blob(code, MIMETextHTMLCharsetUTF8, []byte(html))
}

func (c *context) HTMLBlob(code int, b []byte) (err error) {
return c.Blob(code, MIMETextHTMLCharsetUTF8, b)
}

Copy link
Member

@vishr vishr Dec 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also modify HTML to use HTMLBlob like in JSON. With that you don't need your test case as it will be already covered - just for consistency.

func (c *context) String(code int, s string) (err error) {
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
c.response.WriteHeader(code)
_, err = c.response.Write([]byte(s))
return
return c.Blob(code, MIMETextPlainCharsetUTF8, []byte(s))
}

func (c *context) JSON(code int, i interface{}) (err error) {
Expand Down
10 changes: 10 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ func TestContext(t *testing.T) {
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
}

// HTMLBlob
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
err = c.HTMLBlob(http.StatusOK, []byte("Hello, <strong>World!</strong>"))
if assert.NoError(t, err) {
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
}

// Stream
rec = httptest.NewRecorder()
c = e.NewContext(req, rec).(*context)
Expand Down