Added String method to Request and Response

This commit is contained in:
Aliaksandr Valialkin
2015-11-22 16:47:27 +02:00
parent d5e425206e
commit c9abc7ac4c
2 changed files with 32 additions and 0 deletions
+22
View File
@@ -344,6 +344,28 @@ func (resp *Response) Write(w *bufio.Writer) error {
return err
}
// String returns request representation.
//
// Use Write instead of String for performance-critical code.
func (req *Request) String() string {
var w bytes.Buffer
bw := bufio.NewWriter(&w)
req.Write(bw)
bw.Flush()
return string(w.Bytes())
}
// String returns response representation.
//
// Use Write instead of String for performance-critical code.
func (resp *Response) String() string {
var w bytes.Buffer
bw := bufio.NewWriter(&w)
resp.Write(bw)
bw.Flush()
return string(w.Bytes())
}
func writeBodyChunked(w *bufio.Writer, r io.Reader) error {
vbuf := copyBufPool.Get()
if vbuf == nil {
+10
View File
@@ -8,6 +8,16 @@ import (
"testing"
)
func TestRequestString(t *testing.T) {
var r Request
r.SetRequestURI("http://foobar.com/aaa")
s := r.String()
expectedS := "GET /aaa HTTP/1.1\r\nUser-Agent: fasthttp client\r\nHost: foobar.com\r\n\r\n"
if s != expectedS {
t.Fatalf("unexpected request: %q. Expecting %q", s, expectedS)
}
}
func TestRequestBodyWriter(t *testing.T) {
var r Request
w := r.BodyWriter()