Skip to content

Commit

Permalink
fix: ensure existing non-car behaviour is stable
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Oct 3, 2023
1 parent e8ddedb commit 4ef62b7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
33 changes: 20 additions & 13 deletions cmd/booster-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

mocks_booster_http "github.com/filecoin-project/boost/cmd/booster-http/mocks"
"github.com/filecoin-project/boost/testutil"
"github.com/filecoin-project/boostd-data/model"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
Expand All @@ -21,19 +22,21 @@ const testFile = "test/test_file"

func TestNewHttpServer(t *testing.T) {
// Create a new mock Http server
port, err := testutil.OpenPort()
require.NoError(t, err)
ctrl := gomock.NewController(t)
httpServer := NewHttpServer("", "0.0.0.0", 7777, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
err := httpServer.Start(context.Background())
httpServer := NewHttpServer("", "0.0.0.0", port, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
err = httpServer.Start(context.Background())
require.NoError(t, err)
waitServerUp(t, 7777)
waitServerUp(t, port)

// Check that server is responding with 200 status code
resp, err := http.Get("http://localhost:7777/")
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", port))
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)

// Create a request with Cors header
req, err := http.NewRequest("GET", "http://localhost:7777/", nil)
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/", port), nil)
require.NoError(t, err)
req.Header.Add("Origin", "test")
client := new(http.Client)
Expand All @@ -50,12 +53,14 @@ func TestNewHttpServer(t *testing.T) {

func TestHttpGzipResponse(t *testing.T) {
// Create a new mock Http server with custom functions
port, err := testutil.OpenPort()
require.NoError(t, err)
ctrl := gomock.NewController(t)
mockHttpServer := mocks_booster_http.NewMockHttpServerApi(ctrl)
httpServer := NewHttpServer("", "0.0.0.0", 7777, mockHttpServer, nil)
err := httpServer.Start(context.Background())
httpServer := NewHttpServer("", "0.0.0.0", port, mockHttpServer, nil)
err = httpServer.Start(context.Background())
require.NoError(t, err)
waitServerUp(t, 7777)
waitServerUp(t, port)

// Create mock unsealed file for piece/car
f, _ := os.Open(testFile)
Expand All @@ -80,7 +85,7 @@ func TestHttpGzipResponse(t *testing.T) {

//Create a client and make request with Encoding header
client := new(http.Client)
request, err := http.NewRequest("GET", "http://localhost:7777/piece/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", nil)
request, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/piece/bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi", port), nil)
require.NoError(t, err)
request.Header.Add("Accept-Encoding", "gzip")

Expand Down Expand Up @@ -108,14 +113,16 @@ func TestHttpGzipResponse(t *testing.T) {
func TestHttpInfo(t *testing.T) {
var v apiVersion

port, err := testutil.OpenPort()
require.NoError(t, err)
// Create a new mock Http server
ctrl := gomock.NewController(t)
httpServer := NewHttpServer("", "0.0.0.0", 7777, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
err := httpServer.Start(context.Background())
httpServer := NewHttpServer("", "0.0.0.0", port, mocks_booster_http.NewMockHttpServerApi(ctrl), nil)
err = httpServer.Start(context.Background())
require.NoError(t, err)
waitServerUp(t, 7777)
waitServerUp(t, port)

response, err := http.Get("http://localhost:7777/info")
response, err := http.Get(fmt.Sprintf("http://localhost:%d/info", port))
require.NoError(t, err)
defer response.Body.Close()

Expand Down
2 changes: 1 addition & 1 deletion cmd/booster-http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type HttpServerOptions struct {

func NewHttpServer(path string, listenAddr string, port int, api HttpServerApi, opts *HttpServerOptions) *HttpServer {
if opts == nil {
opts = &HttpServerOptions{ServePieces: true, ServeTrustless: true}
opts = &HttpServerOptions{ServePieces: true, ServeTrustless: false}
}
return &HttpServer{path: path, listenAddr: listenAddr, port: port, api: api, opts: *opts, idxPage: parseTemplate(*opts)}
}
Expand Down

0 comments on commit 4ef62b7

Please sign in to comment.