Skip to content

Commit

Permalink
test(routing/http/server): mediaTypeWildcard
Browse files Browse the repository at this point in the history
Added support and test for wildcard value which is sent by every browser
Accept: text/plain,*/*"
on match, we default to default response type, the same way as
when Accept is missing
  • Loading branch information
lidel committed Apr 5, 2024
1 parent 3f25132 commit 4c5650b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion routing/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (s *server) GetIPNS(w http.ResponseWriter, r *http.Request) {
acceptHdrValue := r.Header.Get("Accept")
// When 'Accept' header is missing, default to 'application/vnd.ipfs.ipns-record'
// (improved UX, similar to how we default to JSON response for /providers and /peers)
if len(acceptHdrValue) == 0 {
if len(acceptHdrValue) == 0 || strings.Contains(acceptHdrValue, mediaTypeWildcard) {
acceptHdrValue = mediaTypeIPNSRecord
}
if !strings.Contains(acceptHdrValue, mediaTypeIPNSRecord) {
Expand Down
49 changes: 48 additions & 1 deletion routing/http/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httptest"
"regexp"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -129,7 +130,7 @@ func TestProviders(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, urlStr, nil)
require.NoError(t, err)

if contentType == "" {
if contentType == "" || strings.Contains(contentType, mediaTypeWildcard) {
// When no Accept header is provided with request
// we default expected response to JSON
contentType = mediaTypeJSON
Expand Down Expand Up @@ -165,6 +166,11 @@ func TestProviders(t *testing.T) {
runTest(t, mediaTypeJSON, true, false, `{"Providers":null}`)
})

t.Run("Wildcard Accept header defaults to JSON Response", func(t *testing.T) {
accept := "text/html,*/*"
runTest(t, accept, true, false, `{"Providers":null}`)
})

t.Run("Missing Accept header defaults to JSON Response", func(t *testing.T) {
accept := ""
runTest(t, accept, true, false, `{"Providers":null}`)
Expand Down Expand Up @@ -220,6 +226,24 @@ func TestPeers(t *testing.T) {
requireCloseToNow(t, resp.Header.Get("Last-Modified"))
})

t.Run("GET /routing/v1/peers/{cid-libp2p-key-peer-id} returns 200 with correct body and headers (No Results, implicit JSON, wildcard Accept header)", func(t *testing.T) {
t.Parallel()

_, pid := makePeerID(t)
results := iter.FromSlice([]iter.Result[*types.PeerRecord]{})

router := &mockContentRouter{}
router.On("FindPeers", mock.Anything, pid, 20).Return(results, nil)

// Simulate request with Accept header that includes wildcard match
resp := makeRequest(t, router, "text/html,*/*", peer.ToCid(pid).String())

// Expect response to default to application/json
require.Equal(t, 200, resp.StatusCode)
require.Equal(t, mediaTypeJSON, resp.Header.Get("Content-Type"))

})

t.Run("GET /routing/v1/peers/{cid-libp2p-key-peer-id} returns 200 with correct body and headers (No Results, implicit JSON, no Accept header)", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -524,6 +548,29 @@ func TestIPNS(t *testing.T) {
require.Equal(t, body, rawRecord1)
})

t.Run("GET /routing/v1/ipns/{cid-peer-id} returns 200 (Accept header with wildcard)", func(t *testing.T) {
t.Parallel()

rec, err := ipns.UnmarshalRecord(rawRecord1)
require.NoError(t, err)

router := &mockContentRouter{}
router.On("GetIPNS", mock.Anything, name1).Return(rec, nil)

// Simulate request with wildcard Accept header
wcAccept := "text/html,*/*"
resp := makeRequest(t, router, "/routing/v1/ipns/"+name1.String(), wcAccept)

// Expect application/vnd.ipfs.ipns-record in response
require.Equal(t, 200, resp.StatusCode)
require.Equal(t, mediaTypeIPNSRecord, resp.Header.Get("Content-Type"))

// Confirm body matches expected bytes
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, body, rawRecord1)
})

t.Run("GET /routing/v1/ipns/{non-peer-cid} returns 400", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 4c5650b

Please sign in to comment.