Skip to content

Commit

Permalink
feat: support html function to sets content type and data
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 3, 2021
1 parent 0cbbe5d commit d407ab1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,12 @@ func (c *Context) ReadFile(key string) ([]byte, *multipart.FileHeader, error) {
return buf, header, nil
}

// HTML sets content type and response body as html
func (c *Context) HTML(html string) {
c.SetContentTypeByExt(".html")
c.BodyBuffer = bytes.NewBufferString(html)
}

// DisableReuse sets the context disable reuse
func (c *Context) DisableReuse() {
atomic.StoreInt32(&c.reuseStatus, ReuseContextDisabled)
Expand Down
10 changes: 10 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,16 @@ func TestReadFile(t *testing.T) {
assert.Equal(testData, data)
}

func TestHTML(t *testing.T) {
assert := assert.New(t)

resp := httptest.NewRecorder()
c := NewContext(resp, nil)
html := "<html><body></body></html>"
c.HTML(html)
assert.Equal("text/html; charset=utf-8", resp.Header().Get(HeaderContentType))
assert.Equal(html, c.BodyBuffer.String())
}

func TestRedirect(t *testing.T) {
assert := assert.New(t)
Expand Down

0 comments on commit d407ab1

Please sign in to comment.