From ed5c2215f578970e43839782cac329615b299cab Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 6 Jan 2015 17:50:36 +0500 Subject: [PATCH] Fix POST requests --- output_http.go | 8 ++++++++ output_http_test.go | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/output_http.go b/output_http.go index a905646..4295517 100644 --- a/output_http.go +++ b/output_http.go @@ -10,6 +10,7 @@ import ( "strings" "sync/atomic" "time" + "io/ioutil" ) type RedirectNotAllowed struct{} @@ -33,6 +34,13 @@ func ParseRequest(data []byte) (request *http.Request, err error) { request, err = http.ReadRequest(reader) + if request.Method == "POST" { + body, _ := ioutil.ReadAll(reader) + bodyBuf := bytes.NewBuffer(body) + request.Body = ioutil.NopCloser(bodyBuf) + request.ContentLength = int64(bodyBuf.Len()) + } + return } diff --git a/output_http_test.go b/output_http_test.go index 788878a..eec343a 100644 --- a/output_http_test.go +++ b/output_http_test.go @@ -7,11 +7,14 @@ import ( "sync" "testing" "time" + "io/ioutil" + "net/http/httputil" + _ "strings" ) func startHTTP(cb func(*http.Request)) net.Listener { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - go cb(r) + cb(r) }) listener, _ := net.Listen("tcp", ":0") @@ -60,6 +63,16 @@ func TestHTTPOutput(t *testing.T) { t.Error("Wrong method") } + if req.Method == "POST" { + defer req.Body.Close() + body, _ := ioutil.ReadAll(req.Body) + + if string(body) != "a=1&b=2\r\n\r\n" { + buf, _ := httputil.DumpRequest(req, true) + t.Error("Wrong POST body:", string(buf)) + } + } + wg.Done() })