Skip to content

Commit 4390ffe

Browse files
authored
fix(catalog): handle content-type header correctly for fetchToken (#673)
related to #652 Signed-off-by: ferhat elmas <[email protected]>
1 parent 4a70225 commit 4390ffe

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

catalog/rest/rest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ const emptyStringHash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991
209209

210210
func (s *sessionTransport) RoundTrip(r *http.Request) (*http.Response, error) {
211211
for k, v := range s.defaultHeaders {
212+
// Skip Content-Type if it's already set in the request
213+
// to avoid duplicate headers (e.g., when using PostForm)
214+
if http.CanonicalHeaderKey(k) == "Content-Type" && r.Header.Get("Content-Type") != "" {
215+
continue
216+
}
212217
for _, hdr := range v {
213218
r.Header.Add(k, hdr)
214219
}

catalog/rest/rest_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,28 @@ func (r *RestCatalogSuite) TestToken401() {
235235
r.ErrorContains(err, "invalid_client: credentials for key invalid_key do not match")
236236
}
237237

238+
func (r *RestCatalogSuite) TestTokenContentTypeDuplicated() {
239+
r.mux.HandleFunc("/v1/oauth/tokens", func(w http.ResponseWriter, req *http.Request) {
240+
r.Equal(http.MethodPost, req.Method)
241+
242+
values := req.Header.Values("Content-Type")
243+
r.Equal([]string{"application/x-www-form-urlencoded"}, values)
244+
245+
w.WriteHeader(http.StatusOK)
246+
247+
json.NewEncoder(w).Encode(map[string]any{
248+
"access_token": TestToken,
249+
"token_type": "Bearer",
250+
"expires_in": 86400,
251+
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
252+
})
253+
})
254+
255+
cat, err := rest.NewCatalog(context.Background(), "rest", r.srv.URL, rest.WithCredential(TestCreds))
256+
r.Require().NoError(err)
257+
r.NotNil(cat)
258+
}
259+
238260
func (r *RestCatalogSuite) TestWithHeaders() {
239261
namespace := "examples"
240262
customHeaders := map[string]string{

0 commit comments

Comments
 (0)