diff --git a/pkg/input/types/http.go b/pkg/input/types/http.go index dbc5c9e325..0620d85f8c 100644 --- a/pkg/input/types/http.go +++ b/pkg/input/types/http.go @@ -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 } diff --git a/pkg/input/types/http_test.go b/pkg/input/types/http_test.go index d674c56353..9525c516e0 100644 --- a/pkg/input/types/http_test.go +++ b/pkg/input/types/http_test.go @@ -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) + } + }) + } +}