Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: routing/http: Set correct headers when returning JSON. #12

Merged
merged 1 commit into from
Dec 6, 2022
Merged
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
2 changes: 2 additions & 0 deletions routing/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
}

func writeResult(w http.ResponseWriter, method string, val any) {
w.Header().Add("Content-Type", "application/json")

// keep the marshaling separate from the writing, so we can distinguish bugs (which surface as 500)
// from transient network issues (which surface as transport errors)
b, err := drjson.MarshalJSONBytes(val)
Expand Down
62 changes: 62 additions & 0 deletions routing/http/server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package server

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-libipfs/routing/http/types"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestHeaders(t *testing.T) {
router := &mockContentRouter{}
server := httptest.NewServer(Handler(router))
t.Cleanup(server.Close)
serverAddr := "http://" + server.Listener.Addr().String()

result := []types.ProviderResponse{
&types.ReadBitswapProviderRecord{
Protocol: types.BitswapProviderID,
},
}

c := "baeabep4vu3ceru7nerjjbk37sxb7wmftteve4hcosmyolsbsiubw2vr6pqzj6mw7kv6tbn6nqkkldnklbjgm5tzbi4hkpkled4xlcr7xz4bq"
cb, err := cid.Decode(c)
require.NoError(t, err)

router.On("FindProviders", mock.Anything, cb).
Return(result, nil)

resp, err := http.Get(serverAddr + ProvidePath + c)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
header := resp.Header.Get("Content-Type")
require.Equal(t, "application/json", header)

resp, err = http.Get(serverAddr + ProvidePath + "BAD_CID")
require.NoError(t, err)
require.Equal(t, 400, resp.StatusCode)
header = resp.Header.Get("Content-Type")
require.Equal(t, "text/plain; charset=utf-8", header)
}

type mockContentRouter struct{ mock.Mock }

func (m *mockContentRouter) FindProviders(ctx context.Context, key cid.Cid) ([]types.ProviderResponse, error) {
args := m.Called(ctx, key)
return args.Get(0).([]types.ProviderResponse), args.Error(1)
}
func (m *mockContentRouter) ProvideBitswap(ctx context.Context, req *BitswapWriteProvideRequest) (time.Duration, error) {
args := m.Called(ctx, req)
return args.Get(0).(time.Duration), args.Error(1)
}

func (m *mockContentRouter) Provide(ctx context.Context, req *WriteProvideRequest) (types.ProviderResponse, error) {
args := m.Called(ctx, req)
return args.Get(0).(types.ProviderResponse), args.Error(1)
}