Skip to content

Commit

Permalink
fix: read in smaller increments when expecting an error
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Sep 8, 2023
1 parent d82d848 commit c8ced9b
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions pkg/internal/itest/http_fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,14 +1233,12 @@ func TestHttpFetch(t *testing.T) {
require.NotEmpty(t, requestId)
_, err := uuid.Parse(requestId)
req.NoError(err)
body, err := io.ReadAll(resp.Body)
if !testCase.expectUncleanEnd {
req.NoError(err)
} else {
req.Error(err)
expectBodyReadError := ""
if testCase.expectUncleanEnd {
expectBodyReadError = "http: unexpected EOF reading trailer"
}
err = resp.Body.Close()
req.NoError(err)
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))
Expand Down Expand Up @@ -1348,3 +1346,31 @@ func debugRemotes(t *testing.T, ctx context.Context, name string, remotes []test
}
return carFiles
}

func readAllBody(t *testing.T, r io.Reader, expectError string) []byte {
if expectError == "" {
body, err := io.ReadAll(r)
require.NoError(t, err)
return body
}
// expect an error, so let's creep up on it and collect as much of the body
// as we can before the error blocks us
// see readLocked() in src/net/http/transfer.go:
// → b.src.Read(p)
// → followed by b.readTrailer() which should error; we want to capture both
var buf bytes.Buffer
var byt [1]byte
var err error
var n int
for {
n, err = r.Read(byt[:])
// record the bytes we read, the error should come after the normal body
// read and then it attempts to read trailers where it should fail
buf.Write(byt[:n])
if err != nil {
require.EqualError(t, err, expectError)
break
}
}
return buf.Bytes()
}

0 comments on commit c8ced9b

Please sign in to comment.