Skip to content

Commit

Permalink
Merge pull request #24 from hurbcom/bugfix/slice-out-of-range
Browse files Browse the repository at this point in the history
Bugfix/slice out of range
  • Loading branch information
bernardolm authored May 17, 2021
2 parents 34ef32e + d462544 commit 6493ad3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
54 changes: 46 additions & 8 deletions lib/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,17 @@ func GetStringBodyHTTPRequest(r *http.Request) *string {
if r == nil {
return nil
}
headers, _ := httputil.DumpRequest(r, false)
headersAndBody, _ := httputil.DumpRequest(r, true)

headers, err := httputil.DumpRequest(r, false)
if err != nil {
return nil
}

headersAndBody, err := httputil.DumpRequest(r, true)
if err != nil || len(headersAndBody) == 0 {
return nil
}

body := headersAndBody[len(headers):]
s := string(bytes.TrimSpace(body))
return &s
Expand All @@ -315,15 +324,26 @@ func GetStringBodyHTTPRequestJSON(r *http.Request) *string {
if r == nil {
return nil
}
headers, _ := httputil.DumpRequest(r, false)
headersAndBody, _ := httputil.DumpRequest(r, true)

headers, err := httputil.DumpRequest(r, false)
if err != nil {
return nil
}

headersAndBody, err := httputil.DumpRequest(r, true)
if err != nil || len(headersAndBody) == 0 {
return nil
}

body := bytes.TrimSpace(headersAndBody[len(headers):])

if len(body) > 0 {
start := bytes.IndexAny(body, "{")
end := bytes.LastIndexAny(body, "}")
r := string(body[start : end+1])
return &r
}

return nil
}

Expand All @@ -332,8 +352,17 @@ func GetStringBodyHTTPResponse(r *http.Response) *string {
if r == nil {
return nil
}
headers, _ := httputil.DumpResponse(r, false)
headersAndBody, _ := httputil.DumpResponse(r, true)

headers, err := httputil.DumpResponse(r, false)
if err != nil {
return nil
}

headersAndBody, err := httputil.DumpResponse(r, true)
if err != nil || len(headersAndBody) == 0 {
return nil
}

body := headersAndBody[len(headers):]
s := string(bytes.TrimSpace(body))
return &s
Expand All @@ -344,8 +373,17 @@ func GetStringBodyHTTPResponseJSON(r *http.Response) *string {
if r == nil {
return nil
}
headers, _ := httputil.DumpResponse(r, false)
headersAndBody, _ := httputil.DumpResponse(r, true)

headers, err := httputil.DumpResponse(r, false)
if err != nil {
return nil
}

headersAndBody, err := httputil.DumpResponse(r, true)
if err != nil || len(headersAndBody) == 0 {
return nil
}

body := bytes.TrimSpace(headersAndBody[len(headers):])
if len(body) > 0 {
start := bytes.IndexAny(body, "{")
Expand Down
29 changes: 29 additions & 0 deletions lib/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ func TestRegexpRFC3339(t *testing.T) {
}
}

func TestGetStringBodyHTTPRequest(t *testing.T) {
body, _ := json.Marshal(nil)
req, _ := http.NewRequest("POST", "http://server.com", bytes.NewBuffer(body))
actual := GetStringBodyHTTPRequest(req)

assert.NotNil(t, actual)
assert.Equal(t, "null", *actual)

req, _ = http.NewRequest("POST", "http://server.com", nil)
actual = GetStringBodyHTTPRequest(req)

assert.NotNil(t, actual)
assert.Equal(t, "", *actual)

req, _ = http.NewRequest("POST", "http://server.com", nil)
req.Header = nil
actual = GetStringBodyHTTPRequest(req)

assert.NotNil(t, actual)
assert.Equal(t, "", *actual)

req, _ = http.NewRequest("POST", "http://server.com", bytes.NewBuffer([]byte("PLAIN TEXT")))
req.Header = nil
actual = GetStringBodyHTTPRequest(req)

assert.NotNil(t, actual)
assert.Equal(t, "PLAIN TEXT", *actual)
}

func TestGetStringBodyHTTPRequestJSON(t *testing.T) {
body, _ := json.Marshal(map[string]string{"foo": "bar"})
req, _ := http.NewRequest("POST", "http://server.com", bytes.NewBuffer(body))
Expand Down

0 comments on commit 6493ad3

Please sign in to comment.