Skip to content

Commit

Permalink
all/tests: close tester.HTTPServer
Browse files Browse the repository at this point in the history
  • Loading branch information
micvbang committed May 23, 2024
1 parent c48b49c commit b89cf1d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
13 changes: 13 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
// endpoint for adding a record.
func TestRecordClientAddHappyPath(t *testing.T) {
srv := tester.HTTPServer(t)
defer srv.Close()

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

Expand Down Expand Up @@ -40,6 +42,8 @@ func TestRecordClientAddHappyPath(t *testing.T) {
// when using an invalid API key.
func TestRecordClientAddNotAuthorized(t *testing.T) {
srv := tester.HTTPServer(t, tester.HTTPAPIKey("working-api-key"))
defer srv.Close()

client, err := seb.NewRecordClient(srv.Server.URL, "invalid-api-key")
require.NoError(t, err)

Expand All @@ -52,6 +56,8 @@ func TestRecordClientAddNotAuthorized(t *testing.T) {
// endpoint for getting a record.
func TestRecordClientGetHappyPath(t *testing.T) {
srv := tester.HTTPServer(t)
defer srv.Close()

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

Expand All @@ -76,6 +82,8 @@ func TestRecordClientGetHappyPath(t *testing.T) {
// using an invalid API key.
func TestRecordClientGetNotAuthorized(t *testing.T) {
srv := tester.HTTPServer(t, tester.HTTPAPIKey("working-api-key"))
defer srv.Close()

client, err := seb.NewRecordClient(srv.Server.URL, "invalid-api-key")
require.NoError(t, err)

Expand All @@ -90,6 +98,8 @@ func TestRecordClientGetNotAuthorized(t *testing.T) {
// attempting to retrieve a record with an offset that does not exist.
func TestRecordClientGetNotFound(t *testing.T) {
srv := tester.HTTPServer(t)
defer srv.Close()

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

Expand All @@ -105,6 +115,7 @@ func TestRecordClientGetNotFound(t *testing.T) {
func TestRecordClientGetBatchHappyPath(t *testing.T) {
const topicName = "topic-name"
srv := tester.HTTPServer(t)
defer srv.Close()

expectedRecords := make([][]byte, 16)
for i := range len(expectedRecords) {
Expand Down Expand Up @@ -132,6 +143,7 @@ func TestRecordClientGetBatchHappyPath(t *testing.T) {
// returned when attempting to read from a topic that does not exist.
func TestRecordClientGetBatchTopicDoesNotExist(t *testing.T) {
srv := tester.HTTPServer(t, tester.HTTPStorageAutoCreateTopic(false))
defer srv.Close()

client, err := seb.NewRecordClient(srv.Server.URL, tester.DefaultAPIKey)
require.NoError(t, err)
Expand All @@ -151,6 +163,7 @@ func TestRecordClientGetBatchTopicDoesNotExist(t *testing.T) {
func TestRecordClientGetBatchOffsetOutOfBounds(t *testing.T) {
const topicName = "topic-name"
srv := tester.HTTPServer(t)
defer srv.Close()

offset, err := srv.Storage.AddRecord(topicName, []byte("this be record"))
require.NoError(t, err)
Expand Down
7 changes: 6 additions & 1 deletion internal/httphandlers/addrecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestAddRecordHappyPath(t *testing.T) {

// add record s.t. returned offset in HTTP response is not 0 (default value)
server := tester.HTTPServer(t)
defer server.Close()

_, err := server.Storage.AddRecord(topicName, recordbatch.Record("haps"))
require.NoError(t, err)

Expand Down Expand Up @@ -47,6 +49,9 @@ func TestAddRecordMissingTopic(t *testing.T) {
// NOTE: no topic-name set
})

response := tester.HTTPServer(t).DoWithAuth(r)
server := tester.HTTPServer(t)
defer server.Close()

response := server.DoWithAuth(r)
require.Equal(t, http.StatusBadRequest, response.StatusCode)
}
1 change: 1 addition & 0 deletions internal/httphandlers/getrecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
// either the topic name or offset does not exist.
func TestGetRecordExistence(t *testing.T) {
server := tester.HTTPServer(t)
defer server.Close()

expectedPayload := []byte("haps")
const topicName = "topicName"
Expand Down
3 changes: 3 additions & 0 deletions internal/httphandlers/getrecords_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
// either the topic name does not exist or the offset is out of bounds.
func TestGetRecordsExistence(t *testing.T) {
server := tester.HTTPServer(t, tester.HTTPStorageAutoCreateTopic(false))
defer server.Close()

const topicName = "topicName"

Expand Down Expand Up @@ -79,6 +80,7 @@ func TestGetRecordsExistence(t *testing.T) {
func TestGetRecordsURLParameters(t *testing.T) {
const topicName = "topic-name"
server := tester.HTTPServer(t, tester.HTTPStorageAutoCreateTopic(false))
defer server.Close()

err := server.Storage.CreateTopic(topicName)
require.NoError(t, err)
Expand Down Expand Up @@ -200,6 +202,7 @@ func TestGetRecordsURLParameters(t *testing.T) {
// returned in multipart/form-data formatting.
func TestGetRecordsMultipartFormData(t *testing.T) {
server := tester.HTTPServer(t)
defer server.Close()

const (
topicName = "topicName"
Expand Down
3 changes: 1 addition & 2 deletions internal/httphandlers/gettopic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestGetTopicHappyPath(t *testing.T) {
)

server := tester.HTTPServer(t)
defer server.Server.Close()
defer server.Close()

for range topicRecords {
_, err := server.Storage.AddRecord(topicName, tester.RandomBytes(t, 32))
Expand Down Expand Up @@ -86,7 +86,6 @@ func TestGetTopicNotFound(t *testing.T) {

// Assert
require.Equal(t, test.statusCode, response.StatusCode)

})
}
}

0 comments on commit b89cf1d

Please sign in to comment.