Skip to content

Commit

Permalink
net/http/httptest: ResponseRecorder.WriteString
Browse files Browse the repository at this point in the history
Fixes #11000

Change-Id: Ic137e8a6c5c6b5b7eee213aca9acf78368e1d686
Reviewed-on: https://go-review.googlesource.com/14296
Reviewed-by: Brad Fitzpatrick <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
nodirt authored and bradfitz committed Sep 4, 2015
1 parent 3c37a61 commit 8c2c35d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/net/http/httptest/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ func (rw *ResponseRecorder) Write(buf []byte) (int, error) {
return len(buf), nil
}

// WriteString always succeeds and writes to rw.Body, if not nil.
func (rw *ResponseRecorder) WriteString(str string) (int, error) {
if !rw.wroteHeader {
rw.WriteHeader(200)
}
if rw.Body != nil {
rw.Body.WriteString(str)
}
return len(str), nil
}

// WriteHeader sets rw.Code.
func (rw *ResponseRecorder) WriteHeader(code int) {
if !rw.wroteHeader {
Expand Down
8 changes: 8 additions & 0 deletions src/net/http/httptest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package httptest

import (
"fmt"
"io"
"net/http"
"testing"
)
Expand Down Expand Up @@ -67,6 +68,13 @@ func TestRecorder(t *testing.T) {
},
check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
},
{
"write string",
func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hi first")
},
check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
},
{
"flush",
func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 8c2c35d

Please sign in to comment.