From 0b556c132dd1732cacc26a752e48094f38581377 Mon Sep 17 00:00:00 2001 From: map1e Date: Thu, 6 Jun 2024 10:50:37 +0800 Subject: [PATCH 1/3] fix: types.RequestResponse url field UnmarshalJSON bug --- pkg/input/types/http.go | 8 ++++++-- pkg/input/types/http_test.go | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) 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..4ec4a46f81 100644 --- a/pkg/input/types/http_test.go +++ b/pkg/input/types/http_test.go @@ -1,6 +1,7 @@ package types import ( + "encoding/json" "io" "net/http" "net/http/httputil" @@ -65,3 +66,23 @@ func TestParseHttpRequest(t *testing.T) { }) } } + +func TestUnmarshalJSON(t *testing.T) { + tests := []struct { + name string + rawJSONStr string + }{ + {"basic url", `{"url": "example.com"}`}, + {"basic url with scheme", `{"url": "http://example.com"}`}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + var rr RequestResponse + err := json.Unmarshal([]byte(tc.rawJSONStr), &rr) + if err != nil { + t.Fatal(err) + } + t.Logf("url: %+v", rr.URL) + }) + } +} From 443eb4205e6c0e99393d445d52189c10a8125c0e Mon Sep 17 00:00:00 2001 From: map1e Date: Thu, 6 Jun 2024 23:38:13 +0800 Subject: [PATCH 2/3] use UnmarshalJSON method in test --- pkg/input/types/http_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/input/types/http_test.go b/pkg/input/types/http_test.go index 4ec4a46f81..055c25dd18 100644 --- a/pkg/input/types/http_test.go +++ b/pkg/input/types/http_test.go @@ -1,7 +1,6 @@ package types import ( - "encoding/json" "io" "net/http" "net/http/httputil" @@ -78,7 +77,7 @@ func TestUnmarshalJSON(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { var rr RequestResponse - err := json.Unmarshal([]byte(tc.rawJSONStr), &rr) + err := rr.UnmarshalJSON([]byte(tc.rawJSONStr)) if err != nil { t.Fatal(err) } From d4b27918a01601383e7b796bf5ee46505e707442 Mon Sep 17 00:00:00 2001 From: map1e Date: Sat, 8 Jun 2024 00:48:07 +0800 Subject: [PATCH 3/3] add http unmarshal json test case --- pkg/input/types/http_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/input/types/http_test.go b/pkg/input/types/http_test.go index 055c25dd18..9525c516e0 100644 --- a/pkg/input/types/http_test.go +++ b/pkg/input/types/http_test.go @@ -68,11 +68,12 @@ func TestParseHttpRequest(t *testing.T) { func TestUnmarshalJSON(t *testing.T) { tests := []struct { - name string - rawJSONStr string + name string + rawJSONStr string + expectedURLStr string }{ - {"basic url", `{"url": "example.com"}`}, - {"basic url with scheme", `{"url": "http://example.com"}`}, + {"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) { @@ -81,7 +82,9 @@ func TestUnmarshalJSON(t *testing.T) { if err != nil { t.Fatal(err) } - t.Logf("url: %+v", rr.URL) + if tc.expectedURLStr != "" { + require.Equal(t, rr.URL.String(), tc.expectedURLStr) + } }) } }