From d407ab1e8937304ead4cc64c4c5e850612cf5ccc Mon Sep 17 00:00:00 2001 From: vicanso Date: Fri, 3 Sep 2021 20:35:49 +0800 Subject: [PATCH] feat: support html function to sets content type and data --- context.go | 6 ++++++ context_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/context.go b/context.go index 7344c12..0081316 100644 --- a/context.go +++ b/context.go @@ -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) diff --git a/context_test.go b/context_test.go index f10d54d..5a31c69 100644 --- a/context_test.go +++ b/context_test.go @@ -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 := "" + 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)