Skip to content

Commit

Permalink
Added RequestCtx.WriteString
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Dec 24, 2015
1 parent 1382150 commit 80105c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ func (resp *Response) AppendBody(p []byte) {
resp.body = append(resp.body, p...)
}

// AppendBodyString appends s to response body.
func (resp *Response) AppendBodyString(s string) {
resp.closeBodyStream()
resp.body = append(resp.body, s...)
}

// SetBody sets response body.
func (resp *Response) SetBody(body []byte) {
resp.closeBodyStream()
Expand Down Expand Up @@ -229,6 +235,11 @@ func (req *Request) AppendBody(p []byte) {
req.body = append(req.body, p...)
}

// AppendBodyString appends s to request body.
func (req *Request) AppendBodyString(s string) {
req.body = append(req.body, s...)
}

// SetBody sets request body.
func (req *Request) SetBody(body []byte) {
req.body = append(req.body[:0], body...)
Expand Down
11 changes: 11 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import (
"testing"
)

func TestRequestCtxWriteString(t *testing.T) {
var ctx RequestCtx
ctx.WriteString("foo")
ctx.WriteString("bar")

s := ctx.Response.Body()
if string(s) != "foobar" {
t.Fatalf("unexpected response body %q. Expecting %q", s, "foobar")
}
}

func TestRequestMultipartForm(t *testing.T) {
var w bytes.Buffer
mw := multipart.NewWriter(&w)
Expand Down
6 changes: 6 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ func (ctx *RequestCtx) Write(p []byte) (int, error) {
return len(p), nil
}

// WriteString appends s to response body.
func (ctx *RequestCtx) WriteString(s string) (int, error) {
ctx.Response.AppendBodyString(s)
return len(s), nil
}

// PostBody returns POST request body.
//
// The returned value is valid until RequestHandler return.
Expand Down

0 comments on commit 80105c1

Please sign in to comment.