[go: up one dir, main page]

Skip to content

Commit

Permalink
fix: types.RequestResponse url field UnmarshalJSON bug (#5267)
Browse files Browse the repository at this point in the history
* fix: types.RequestResponse url field UnmarshalJSON bug

* use UnmarshalJSON method in test

* add http unmarshal json test case
  • Loading branch information
LazyMaple committed Jun 15, 2024
1 parent 9f1faa0 commit 4720d8c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/input/types/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,15 @@ func (rr *RequestResponse) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &m); err != nil {
return err
}
urlStr, ok := m["url"]
urlStrRaw, ok := m["url"]
if !ok {
return fmt.Errorf("missing url in request response")
}
parsed, err := urlutil.ParseAbsoluteURL(string(urlStr), false)
var urlStr string
if err := json.Unmarshal(urlStrRaw, &urlStr); err != nil {
return err
}
parsed, err := urlutil.ParseAbsoluteURL(urlStr, false)
if err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/input/types/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,26 @@ func TestParseHttpRequest(t *testing.T) {
})
}
}

func TestUnmarshalJSON(t *testing.T) {
tests := []struct {
name string
rawJSONStr string
expectedURLStr string
}{
{"basic url", `{"url": "example.com"}`, "example.com"},
{"basic url with scheme", `{"url": "http://example.com"}`, "http://example.com"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var rr RequestResponse
err := rr.UnmarshalJSON([]byte(tc.rawJSONStr))
if err != nil {
t.Fatal(err)
}
if tc.expectedURLStr != "" {
require.Equal(t, rr.URL.String(), tc.expectedURLStr)
}
})
}
}

0 comments on commit 4720d8c

Please sign in to comment.