Skip to content

Commit

Permalink
fix: types.RequestResponse url field UnmarshalJSON bug
Browse files Browse the repository at this point in the history
  • Loading branch information
LazyMaple committed Jun 6, 2024
1 parent 5428d05 commit 0b556c1
Show file tree
Hide file tree
Showing 2 changed files with 27 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
21 changes: 21 additions & 0 deletions pkg/input/types/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/json"
"io"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 0b556c1

Please sign in to comment.