mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-26 17:46:34 +03:00
Added String method to Request and Response
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user