From 0b556c132dd1732cacc26a752e48094f38581377 Mon Sep 17 00:00:00 2001 From: map1e Date: Thu, 6 Jun 2024 10:50:37 +0800 Subject: [PATCH] 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) + }) + } +}