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: http redirect for /piece/ in booster-http #956

Merged
merged 2 commits into from
Nov 16, 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
57 changes: 56 additions & 1 deletion cmd/booster-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestHttpGzipResponse(t *testing.T) {
mockHttpServer.EXPECT().PiecesContainingMultihash(gomock.Any(), gomock.Any()).AnyTimes().Return(cids, nil)
mockHttpServer.EXPECT().GetPieceInfo(gomock.Any()).AnyTimes().Return(&pieceInfo, nil)

// Create a client and make request with Encoding header
//Create a client and make request with Encoding header
client := new(http.Client)
request, err := http.NewRequest("GET", "http://localhost:7777/piece?payloadCid=bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi&format=piece", nil)
require.NoError(t, err)
Expand All @@ -101,3 +101,58 @@ func TestHttpGzipResponse(t *testing.T) {
err = httpServer.Stop()
require.NoError(t, err)
}

func TestHttpResponseRedirects(t *testing.T) {

// Create a new mock Http server with custom functions
ctrl := gomock.NewController(t)
mockHttpServer := mocks_booster_http.NewMockHttpServerApi(ctrl)
httpServer := NewHttpServer("", 7777, false, mockHttpServer)
httpServer.Start(context.Background())

// Create mock unsealed file for piece/car
f, _ := os.Open(testFile)
defer f.Close()

//Create CID
var cids []cid.Cid
cid, err := cid.Parse("bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi")
require.NoError(t, err)
cids = append(cids, cid)

// Crate pieceInfo
deal := piecestore.DealInfo{
DealID: 1234567,
SectorID: 0,
Offset: 1233,
Length: 123,
}
var deals []piecestore.DealInfo

pieceInfo := piecestore.PieceInfo{
PieceCID: cid,
Deals: append(deals, deal),
}

mockHttpServer.EXPECT().UnsealSectorAt(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(f, nil)
mockHttpServer.EXPECT().IsUnsealed(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(true, nil)
mockHttpServer.EXPECT().PiecesContainingMultihash(gomock.Any(), gomock.Any()).AnyTimes().Return(cids, nil)
mockHttpServer.EXPECT().GetPieceInfo(gomock.Any()).AnyTimes().Return(&pieceInfo, nil)

// Create a client with check against redirects
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
request, err := http.NewRequest("GET", "http://localhost:7777/piece?payloadCid=bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi&format=piece", nil)
require.NoError(t, err)

response, err := client.Do(request)
require.NoError(t, err)
require.Equal(t, 200, response.StatusCode)

// Stop the server
err = httpServer.Stop()
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewHttpServer(path string, port int, allowIndexing bool, api HttpServerApi)
}

func (s *HttpServer) pieceBasePath() string {
return s.path + "/piece/"
return s.path + "/piece"
}

func (s *HttpServer) Start(ctx context.Context) {
Expand Down