Skip to content

Commit

Permalink
parse form drains request body (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik authored and tommysitu committed Mar 5, 2023
1 parent 9bab80c commit 36cdf2e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/models/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ type RequestDetails struct {
}

func NewRequestDetailsFromHttpRequest(req *http.Request) (RequestDetails, error) {
req.ParseForm()

if req.Body == nil {
req.Body = ioutil.NopCloser(bytes.NewBuffer([]byte("")))
}

reqBody, err := util.GetRequestBody(req)

req.ParseForm()
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
Expand Down
17 changes: 17 additions & 0 deletions core/models/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"compress/gzip"
"io/ioutil"
"os"
"strings"
"testing"

"net/http"
"net/url"

v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/models"
Expand Down Expand Up @@ -227,6 +229,21 @@ func Test_NewRequestDetailsFromHttpRequest_SortsQueryString(t *testing.T) {
Expect(requestDetails.QueryString()).To(Equal("a=a&a=b"))
}

func Test_NewRequestDetailsFromHttpRequest_WithFormDataHavingNonEmptyBody(t *testing.T) {
RegisterTestingT(t)
form := url.Values{}
form.Add("key1", "value1")
form.Add("key2", "value2")
request, _ := http.NewRequest("POST", "http://test.org", strings.NewReader(form.Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
requestDetails, err := models.NewRequestDetailsFromHttpRequest(request)
Expect(err).To(BeNil())

Expect(requestDetails.FormData).To(HaveLen(2))
Expect(requestDetails.FormData["key1"][0]).To(Equal("value1"))
Expect(requestDetails.FormData["key2"][0]).To(Equal("value2"))
Expect(requestDetails.Body).NotTo(Equal(""))
}
func Test_NewRequestDetailsFromHttpRequest_StripsArbitaryGolangColonEscaping(t *testing.T) {
RegisterTestingT(t)
request, _ := http.NewRequest("GET", "http://test.org/?a=b:c", nil)
Expand Down

0 comments on commit 36cdf2e

Please sign in to comment.