Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,24 @@ func (c *BaseHttpClient) NewRequest(ctx context.Context, method string, url *url
}

for k, v := range h {
headers[k] = v
switch {
// special check for Content-Type and Accept header
case strings.EqualFold(k, "content-type") || strings.EqualFold(k, "accept"):
current := headers[k]
if current == "" {
headers[k] = v
continue
}
// if the incoming value carries extra information
// we respect that and set it as the new value for that
// header
split := strings.Split(v, ";")
if len(split) > 1 {
headers[k] = v
}
default:
headers[k] = v
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Header Handling Fails on Case and Simple Values

The special handling for Content-Type and Accept headers has two issues. The logic's case-sensitive map access can prevent existing headers from being found, leading to duplicate logical headers. Also, it only updates these headers if the new value contains a semicolon, silently ignoring explicit overrides with simpler values.

Fix in Cursor Fix in Web

}
}

Expand Down
26 changes: 26 additions & 0 deletions pkg/uhttp/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,32 @@ func TestWrapper_NewRequest(t *testing.T) {
err: nil,
},
},
{
name: "POST request with JSON body with specific content-type header value",
method: http.MethodPost,
url: "http://example.com",
options: []RequestOption{WithHeader("Content-Type", "application/json;odata=verbose"), WithJSONBody(exampleBody), WithAcceptJSONHeader()},
expected: expected{
method: http.MethodPost,
url: "http://example.com",
headers: http.Header{"Accept": []string{"application/json"}, "Content-Type": []string{"application/json;odata=verbose"}},
body: io.NopCloser(exampleBodyBuffer),
err: nil,
},
},
{
name: "POST request with JSON body with specific Accept header value",
method: http.MethodPost,
url: "http://example.com",
options: []RequestOption{WithHeader("Accept", "application/json;odata=verbose"), WithJSONBody(exampleBody), WithAcceptJSONHeader()},
expected: expected{
method: http.MethodPost,
url: "http://example.com",
headers: http.Header{"Accept": []string{"application/json;odata=verbose"}, "Content-Type": []string{"application/json"}},
body: io.NopCloser(exampleBodyBuffer),
err: nil,
},
},
}

for _, tc := range test {
Expand Down