Skip to content

Commit

Permalink
RecordClient: return ErrPayloadTooLarge on http.StatusRequestEntityTo…
Browse files Browse the repository at this point in the history
…oLarge
  • Loading branch information
micvbang committed May 25, 2024
1 parent 5ba3914 commit 59c7b6e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func (c *RecordClient) statusCode(res *http.Response) error {
return fmt.Errorf("status code %d: %w", res.StatusCode, ErrNotFound)
}

if res.StatusCode == http.StatusRequestEntityTooLarge {
return fmt.Errorf("status code %d: %w", res.StatusCode, ErrPayloadTooLarge)
}

return nil
}

Expand Down
24 changes: 24 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

seb "github.com/micvbang/simple-event-broker"
"github.com/micvbang/simple-event-broker/internal/infrastructure/tester"
"github.com/micvbang/simple-event-broker/internal/recordbatch"
"github.com/micvbang/simple-event-broker/internal/sebhttp"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -234,3 +236,25 @@ func TestRecordClientGetRecordsOffsetOutOfBounds(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 0, len(records))
}

// TestRecordClientAddRecordsPayloadTooLarge verifies that AddRecords()
// returns ErrPayloadTooLarge when receiving status code
// http.StatusRequestEntityTooLarge.
func TestRecordClientAddRecordsPayloadTooLarge(t *testing.T) {
deps := &sebhttp.MockDependencies{}
deps.AddRecordsMock = func(topicName string, records []recordbatch.Record) ([]uint64, error) {
return nil, seb.ErrPayloadTooLarge
}

srv := tester.HTTPServer(t, tester.HTTPDependencies(deps))
defer srv.Close()

client, err := seb.NewRecordClient(srv.Server.URL, tester.DefaultAPIKey)
require.NoError(t, err)

// Act
err = client.AddRecords("topicName", [][]byte{})

// Assert
require.ErrorIs(t, err, seb.ErrPayloadTooLarge)
}

0 comments on commit 59c7b6e

Please sign in to comment.