Skip to content

Commit 0585641

Browse files
lidelhacdias
authored andcommitted
test: Accept header overides conflicting ?format
1 parent 20c11e2 commit 0585641

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

gateway/gateway_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func TestHeaders(t *testing.T) {
440440
DeserializedResponses: true,
441441
})
442442

443-
runTest := func(name, path, accept, host, expectedContentPath string) {
443+
runTest := func(name, path, accept, host, expectedContentLocationHdr string) {
444444
t.Run(name, func(t *testing.T) {
445445
t.Parallel()
446446

@@ -461,7 +461,7 @@ func TestHeaders(t *testing.T) {
461461
require.NoError(t, err)
462462

463463
require.Equal(t, http.StatusOK, resp.StatusCode, string(body))
464-
require.Equal(t, expectedContentPath, resp.Header.Get("Content-Location"))
464+
require.Equal(t, expectedContentLocationHdr, resp.Header.Get("Content-Location"))
465465
})
466466
}
467467

@@ -490,6 +490,8 @@ func TestHeaders(t *testing.T) {
490490
runTest("DNSLink gateway with Accept: "+responseFormat, "/empty-dir/", responseFormat, dnslinkGatewayHost, "/empty-dir/?format="+formatParam)
491491
runTest("DNSLink gateway with ?format="+formatParam, "/empty-dir/?format="+formatParam, "", dnslinkGatewayHost, "")
492492
}
493+
494+
runTest("Accept: application/vnd.ipld.car overrides ?format=raw in Content-Location", contentPath+"?format=raw", "application/vnd.ipld.car", "", contentPath+"?format=car")
493495
})
494496
}
495497

gateway/handler.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,22 @@ func customResponseFormat(r *http.Request) (mediaType string, params map[string]
652652
// correct caching of such format requests when the format is passed via the
653653
// Accept header, for example.
654654
func addContentLocation(r *http.Request, w http.ResponseWriter, rq *requestData) {
655-
// Skip Content-Location if no explicit format was requested via the HTTP
656-
// Accept header, or if it was requested via URL query parameter.
657-
if rq.responseFormat == "" || r.URL.Query().Get("format") != "" {
655+
// Skip Content-Location if no explicit format was requested
656+
// via Accept HTTP header or ?format URL param
657+
if rq.responseFormat == "" {
658658
return
659659
}
660660

661661
format := responseFormatToFormatParam[rq.responseFormat]
662+
663+
// Skip Content-Location if there is no conflict between
664+
// 'format' in URL and value in 'Accept' header.
665+
// If both are present and don't match, we continue and generate
666+
// Content-Location to ensure value from Accept overrides 'format' from URL.
667+
if urlFormat := r.URL.Query().Get("format"); urlFormat != "" && urlFormat == format {
668+
return
669+
}
670+
662671
path := r.URL.Path
663672
if p, ok := r.Context().Value(OriginalPathKey).(string); ok {
664673
path = p

0 commit comments

Comments
 (0)