Skip to content

Commit

Permalink
add tests for http ingestion
Browse files Browse the repository at this point in the history
  • Loading branch information
adelowo committed Jan 24, 2024
1 parent 3cbcbb7 commit 2a9afa4
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 5 deletions.
1 change: 1 addition & 0 deletions generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package sdump

//go:generate mockgen --source url.go -destination mocks/url.go -package mocks
//go:generate mockgen --source ingest.go -destination mocks/ingest.go -package mocks
55 changes: 55 additions & 0 deletions mocks/ingest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions mocks/url.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"an error occurred while generating endpoint"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"an error occurred while ingesting request"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"an error occurred while ingesting HTTP request"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"http: request body too large"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"Request ingested"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"Dump url does not exist"}
4 changes: 3 additions & 1 deletion server/httpd/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ func (u *urlHandler) ingest(w http.ResponseWriter, r *http.Request) {
size, err := io.Copy(s, r.Body)
if err != nil {
msg := "could not copy request body"
status := http.StatusInternalServerError
if maxErr, ok := err.(*http.MaxBytesError); ok {
msg = maxErr.Error()
status = http.StatusBadRequest
}

logger.WithError(err).Error("could not copy request body")
_ = render.Render(w, r, newAPIError(http.StatusInternalServerError,
_ = render.Render(w, r, newAPIError(status,
msg))
return
}
Expand Down
118 changes: 114 additions & 4 deletions server/httpd/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
"strings"
"testing"

"github.com/adelowo/sdump"
"github.com/adelowo/sdump/config"
"github.com/adelowo/sdump/mocks"
"github.com/r3labs/sse/v2"
"github.com/sebdah/goldie/v2"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
Expand All @@ -35,7 +37,7 @@ func verifyMatch(t *testing.T, v interface{}) {
g.Assert(t, t.Name(), b.Bytes())
}

func TestURLHandler(t *testing.T) {
func TestURLHandler_Create(t *testing.T) {
tt := []struct {
name string
mockFn func(urlRepo *mocks.MockURLRepository)
Expand Down Expand Up @@ -84,9 +86,10 @@ func TestURLHandler(t *testing.T) {
v.mockFn(urlRepo)

u := &urlHandler{
logger: logger,
cfg: config.Config{},
urlRepo: urlRepo,
logger: logger,
cfg: config.Config{},
urlRepo: urlRepo,
sseServer: sse.New(),
}

u.create(recorder, req)
Expand All @@ -99,3 +102,110 @@ func TestURLHandler(t *testing.T) {
})
}
}

func TestURLHandler_Ingest(t *testing.T) {
tt := []struct {
name string
mockFn func(urlRepo *mocks.MockURLRepository, requestRepo *mocks.MockIngestRepository)
expectedStatusCode int
requestBody io.Reader
requestBodySize int64
}{
{
name: "url reference not found",
mockFn: func(urlRepo *mocks.MockURLRepository, requestRepo *mocks.MockIngestRepository) {
urlRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).Return(&sdump.URLEndpoint{}, sdump.ErrURLEndpointNotFound)
},
expectedStatusCode: http.StatusNotFound,
requestBody: strings.NewReader(``),
requestBodySize: 10,
},
{
name: "error while fetching url",
mockFn: func(urlRepo *mocks.MockURLRepository, requestRepo *mocks.MockIngestRepository) {
urlRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).Return(&sdump.URLEndpoint{}, errors.New("could not fetch url"))
},
expectedStatusCode: http.StatusInternalServerError,
requestBody: strings.NewReader(``),
requestBodySize: 10,
},
{
name: "http request body too large",
mockFn: func(urlRepo *mocks.MockURLRepository, requestRepo *mocks.MockIngestRepository) {
urlRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).Return(&sdump.URLEndpoint{}, nil)
},
expectedStatusCode: http.StatusBadRequest,
requestBody: strings.NewReader(`{"name" : "Lanre", "occupation" :"Software"}`),
requestBodySize: 10,
},
{
name: "could not create ingestion",
mockFn: func(urlRepo *mocks.MockURLRepository, requestRepo *mocks.MockIngestRepository) {
urlRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).Return(&sdump.URLEndpoint{}, nil)

requestRepo.EXPECT().Create(gomock.Any(), gomock.Any()).
Times(1).
Return(errors.New("could not insert into the database"))
},
expectedStatusCode: http.StatusInternalServerError,
requestBody: strings.NewReader(`{"name" : "Lanre", "occupation" :"Software"}`),
requestBodySize: 100,
},
{
name: "ingested correctly",
mockFn: func(urlRepo *mocks.MockURLRepository, requestRepo *mocks.MockIngestRepository) {
urlRepo.EXPECT().Get(gomock.Any(), gomock.Any()).
Times(1).Return(&sdump.URLEndpoint{}, nil)

requestRepo.EXPECT().Create(gomock.Any(), gomock.Any()).
Times(1).
Return(nil)
},
expectedStatusCode: http.StatusAccepted,
requestBody: strings.NewReader(`{"name" : "Lanre", "occupation" :"Software"}`),
requestBodySize: 100,
},
}

for _, v := range tt {
t.Run(v.name, func(t *testing.T) {
recorder := httptest.NewRecorder()

req := httptest.NewRequest(http.MethodPost, "/", v.requestBody)

logrus.SetOutput(io.Discard)

logger := logrus.WithField("module", "test")

ctrl := gomock.NewController(t)
defer ctrl.Finish()

urlRepo := mocks.NewMockURLRepository(ctrl)

requestRepo := mocks.NewMockIngestRepository(ctrl)

v.mockFn(urlRepo, requestRepo)

u := &urlHandler{
logger: logger,
cfg: config.Config{
HTTP: config.HTTPConfig{
MaxRequestBodySize: v.requestBodySize,
},
},
urlRepo: urlRepo,
ingestRepo: requestRepo,
sseServer: sse.New(),
}

u.ingest(recorder, req)

require.Equal(t, v.expectedStatusCode, recorder.Result().StatusCode)
verifyMatch(t, recorder)
})
}
}

0 comments on commit 2a9afa4

Please sign in to comment.