Skip to content

Commit

Permalink
fix: read body asap
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Sep 8, 2023
1 parent c8ced9b commit 36ebbef
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions pkg/internal/itest/http_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func TestHttpFetch(t *testing.T) {
type queryModifier func(url.Values, []testpeer.TestPeer)
type bodyValidator func(*testing.T, unixfs.DirEntry, []byte)
type lassieOptsGen func(*testing.T, *mocknet.MockRetrievalNet) []lassie.LassieOption
type response struct {
StatusCode int
Header http.Header
Body []byte
}
wrapPath := "/want2/want1/want0"

testCases := []struct {
Expand Down Expand Up @@ -1158,9 +1163,9 @@ func TestHttpFetch(t *testing.T) {
}
}

responseChans := make([]chan *http.Response, 0)
responseChans := make([]chan response, 0)
for i := 0; i < len(srcData); i++ {
responseChan := make(chan *http.Response, 1)
responseChan := make(chan response, 1)
responseChans = append(responseChans, responseChan)
go func(i int) {
// Make a request for our CID and read the complete CAR bytes
Expand All @@ -1180,11 +1185,17 @@ func TestHttpFetch(t *testing.T) {
t.Log("Fetching", getReq.URL.String())
resp, err := http.DefaultClient.Do(getReq)
req.NoError(err)
responseChan <- resp
expectBodyReadError := ""
if testCase.expectUncleanEnd {
expectBodyReadError = "http: unexpected EOF reading trailer"
}
body := readAllBody(t, resp.Body, expectBodyReadError)
req.NoError(resp.Body.Close())
responseChan <- response{StatusCode: resp.StatusCode, Header: resp.Header, Body: body}
}(i)
}

responses := make([]*http.Response, 0)
responses := make([]response, 0)
for _, responseChan := range responseChans {
select {
case resp := <-responseChan:
Expand Down Expand Up @@ -1214,9 +1225,7 @@ func TestHttpFetch(t *testing.T) {
req.Equal(http.StatusUnauthorized, resp.StatusCode)
} else {
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
req.NoError(err)
req.Failf("200 response code not received", "got code: %d, body: %s", resp.StatusCode, string(body))
req.Failf("200 response code not received", "got code: %d, body: %s", resp.StatusCode, string(resp.Body))
}
req.Regexp(`^lassie/v\d+\.\d+\.\d+-\w+$`, resp.Header.Get("Server"))
req.Equal(fmt.Sprintf(`attachment; filename="%s.car"`, srcData[i].Root.String()), resp.Header.Get("Content-Disposition"))
Expand All @@ -1233,28 +1242,21 @@ func TestHttpFetch(t *testing.T) {
require.NotEmpty(t, requestId)
_, err := uuid.Parse(requestId)
req.NoError(err)
expectBodyReadError := ""
if testCase.expectUncleanEnd {
expectBodyReadError = "http: unexpected EOF reading trailer"
}
body := readAllBody(t, resp.Body, expectBodyReadError)
req.NoError(resp.Body.Close())

if DEBUG_DATA {
t.Logf("Creating CAR %s in temp dir", fmt.Sprintf("%s_received%d.car", testCase.name, i))
dstf, err := os.CreateTemp("", fmt.Sprintf("%s_received%d.car", testCase.name, i))
req.NoError(err)
t.Logf("Writing received data to CAR @ %s", dstf.Name())
_, err = dstf.Write(body)
_, err = dstf.Write(resp.Body)
req.NoError(err)
carFiles = append(carFiles, dstf)
}

if testCase.validateBodies != nil && testCase.validateBodies[i] != nil {
testCase.validateBodies[i](t, srcData[i], body)
testCase.validateBodies[i](t, srcData[i], resp.Body)
} else {
// gotDir := CarToDirEntry(t, bytes.NewReader(body), srcData[i].Root, true)
gotLsys := CarBytesLinkSystem(t, bytes.NewReader(body))
gotLsys := CarBytesLinkSystem(t, bytes.NewReader(resp.Body))
gotDir := unixfs.ToDirEntry(t, gotLsys, srcData[i].Root, true)
unixfs.CompareDirEntries(t, srcData[i], gotDir)
}
Expand Down

0 comments on commit 36ebbef

Please sign in to comment.