Skip to content

Commit

Permalink
Merge pull request #49 from fzipi/fix-url-parse
Browse files Browse the repository at this point in the history
fix(request): fix data request parsing
  • Loading branch information
fzipi authored Mar 5, 2022
2 parents 98b3bb2 + da5a95b commit badb58d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ftwhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestGetTrackedTime(t *testing.T) {

h := Header{"Accept": "*/*", "User-Agent": "go-ftw test agent", "Host": "localhost"}

data := []byte(`test=me&one=two`)
data := []byte(`test=me&one=two&one=twice`)
req := NewRequest(rl, h, data, true)

err := c.NewConnection(*d)
Expand Down
4 changes: 2 additions & 2 deletions ftwhttp/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ func encodeDataParameters(h Header, data []byte) ([]byte, error) {
if h.Get(ContentTypeHeader) == "application/x-www-form-urlencoded" {
if escapedData, _ := url.QueryUnescape(string(data)); escapedData == string(data) {
queryString, err := url.ParseQuery(string(data))
if err != nil || emptyQueryValues(queryString) {
return data, err
if (err != nil && strings.Contains(err.Error(), "invalid semicolon separator in query")) || emptyQueryValues(queryString) {
return data, nil
}
encodedData := queryString.Encode()
if encodedData != string(data) {
Expand Down
26 changes: 25 additions & 1 deletion ftwhttp/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func TestRequestRawData(t *testing.T) {
}
}

func TestRequesSettingDataaWhenThereIsRawData(t *testing.T) {
func TestRequestSettingDataaWhenThereIsRawData(t *testing.T) {
req := generateBaseRawRequestForTesting()

err := req.SetData([]byte("This is the data now"))
Expand All @@ -242,3 +242,27 @@ func TestRequesSettingDataaWhenThereIsRawData(t *testing.T) {
t.Errorf("Failed !")
}
}

func TestRequestURLParse(t *testing.T) {
req := generateBaseRequestForTesting()

h := req.Headers()
h.Add(ContentTypeHeader, "application/x-www-form-urlencoded")
// Test adding semicolons to test parse
err := req.SetData([]byte("test=This&test=nothing"))
if err != nil {
t.Errorf("Failed !")
}
}

func TestRequestURLParseFail(t *testing.T) {
req := generateBaseRequestForTesting()

h := req.Headers()
h.Add(ContentTypeHeader, "application/x-www-form-urlencoded")
// Test adding semicolons to test parse
err := req.SetData([]byte("test=This&that=but with;;;;;; data now"))
if err != nil {
t.Errorf("Failed !")
}
}

0 comments on commit badb58d

Please sign in to comment.