Skip to content

Commit 3e5aec1

Browse files
authored
feat: add support for sending string and byte data (#47)
1 parent 4085c30 commit 3e5aec1

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

context.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,52 @@ func (c *Context) HTMLString(code int, tpl string) {
162162
c.mustWrite([]byte(tpl))
163163
}
164164

165+
// String sends bare string as a plain text response with the given status code.
166+
func (c *Context) String(code int, data string) {
167+
c.writeContentType("text/plain")
168+
c.response.WriteHeader(code)
169+
c.mustWrite([]byte(data))
170+
}
171+
172+
// Byte sends bare bytes as response with the given status code.
173+
func (c *Context) Byte(code int, data []byte) {
174+
c.writeContentType("application/octet-stream")
175+
c.response.WriteHeader(code)
176+
c.mustWrite([]byte(data))
177+
}
178+
165179
// NoContent returns an empty response with the given status code.
166180
func (c *Context) NoContent(code int) {
167181
c.response.WriteHeader(code)
168182
c.response.WriteHeaderNow()
169183
}
170184

185+
// GetResponseHeader gets a response header.
186+
func (c *Context) GetResponseHeader(key string) string {
187+
return c.response.Header().Get(key)
188+
}
189+
190+
// SetResponseHeader sets a header to the response.
191+
func (c *Context) SetResponseHeader(key, value string) {
192+
c.response.Header().Set(key, value)
193+
}
194+
195+
// SetRequestHeader sets a header to the request.
196+
func (c *Context) SetRequestHeader(key, value string) {
197+
c.request.Header.Set(key, value)
198+
}
199+
200+
// GetRequestHeader gets a request header.
201+
func (c *Context) GetRequestHeader(key string) string {
202+
return c.request.Header.Get(key)
203+
}
204+
171205
// writeContentType sets content type header for response.
172206
// It won't overwrite content type if it's already set.
173207
func (c *Context) writeContentType(contentType string) {
174208
contentTypeHeader := "Content-Type"
175-
header := c.response.Header()
176-
if header.Get(contentTypeHeader) == "" {
177-
header.Set(contentTypeHeader, contentType)
209+
if c.GetResponseHeader(contentTypeHeader) == "" {
210+
c.SetResponseHeader(contentTypeHeader, contentType)
178211
}
179212
}
180213

context_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,71 @@ func TestContext_Debug(t *testing.T) {
466466

467467
assert.True(t, ctx.Debug())
468468
}
469+
470+
func TestContext_String(t *testing.T) {
471+
ctx := newContext(New())
472+
473+
res := httptest.NewRecorder()
474+
ctx.reset(nil, res)
475+
476+
ctx.String(http.StatusAccepted, "hello")
477+
478+
assert.Equal(t, http.StatusAccepted, res.Code)
479+
assert.Equal(t, "hello", res.Body.String())
480+
assert.Equal(t, "text/plain", res.Header().Get("Content-Type"))
481+
}
482+
483+
func TestContext_Byte(t *testing.T) {
484+
ctx := newContext(New())
485+
486+
res := httptest.NewRecorder()
487+
ctx.reset(nil, res)
488+
489+
ctx.Byte(http.StatusOK, []byte("mojix"))
490+
491+
assert.Equal(t, http.StatusOK, res.Code)
492+
assert.Equal(t, "mojix", res.Body.String())
493+
assert.Equal(t, "application/octet-stream", res.Header().Get("Content-Type"))
494+
}
495+
496+
func TestContext_GetResponseHeader(t *testing.T) {
497+
ctx := newContext(New())
498+
499+
res := httptest.NewRecorder()
500+
res.Header().Set("key", "value")
501+
502+
ctx.reset(nil, res)
503+
504+
assert.Equal(t, "value", ctx.GetResponseHeader("key"))
505+
}
506+
507+
func TestContext_SetResponseHeader(t *testing.T) {
508+
ctx := newContext(New())
509+
510+
res := httptest.NewRecorder()
511+
ctx.reset(nil, res)
512+
513+
ctx.SetResponseHeader("key", "value")
514+
assert.Equal(t, "value", ctx.GetResponseHeader("key"))
515+
}
516+
517+
func TestContext_GetRequestHeader(t *testing.T) {
518+
ctx := newContext(New())
519+
520+
req := httptest.NewRequest(http.MethodGet, "/path", nil)
521+
req.Header.Set("key", "value")
522+
523+
ctx.reset(req, nil)
524+
525+
assert.Equal(t, "value", ctx.GetRequestHeader("key"))
526+
}
527+
528+
func TestContext_SetRequestHeader(t *testing.T) {
529+
ctx := newContext(New())
530+
531+
req := httptest.NewRequest(http.MethodGet, "/path", nil)
532+
ctx.reset(req, nil)
533+
534+
ctx.SetRequestHeader("key", "value")
535+
assert.Equal(t, "value", ctx.GetRequestHeader("key"))
536+
}

0 commit comments

Comments
 (0)