Commit 8c2c35de authored by Nodir Turakulov's avatar Nodir Turakulov Committed by Brad Fitzpatrick

net/http/httptest: ResponseRecorder.WriteString

Fixes #11000

Change-Id: Ic137e8a6c5c6b5b7eee213aca9acf78368e1d686
Reviewed-on: https://go-review.googlesource.com/14296Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 3c37a614
...@@ -55,6 +55,17 @@ func (rw *ResponseRecorder) Write(buf []byte) (int, error) { ...@@ -55,6 +55,17 @@ func (rw *ResponseRecorder) Write(buf []byte) (int, error) {
return len(buf), nil 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. // WriteHeader sets rw.Code.
func (rw *ResponseRecorder) WriteHeader(code int) { func (rw *ResponseRecorder) WriteHeader(code int) {
if !rw.wroteHeader { if !rw.wroteHeader {
......
...@@ -6,6 +6,7 @@ package httptest ...@@ -6,6 +6,7 @@ package httptest
import ( import (
"fmt" "fmt"
"io"
"net/http" "net/http"
"testing" "testing"
) )
...@@ -67,6 +68,13 @@ func TestRecorder(t *testing.T) { ...@@ -67,6 +68,13 @@ func TestRecorder(t *testing.T) {
}, },
check(hasStatus(200), hasContents("hi first"), hasFlush(false)), 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", "flush",
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment