From d40eb82570c3a4358b7c3f8c948d302115ccc373 Mon Sep 17 00:00:00 2001 From: Joseph Weigl Date: Wed, 21 Aug 2024 11:44:20 +0200 Subject: [PATCH 1/2] test: add tests for variables in body file --- internal/server/http/handler_test.go | 56 +++++++++++++++++++ .../gopher_variables.imp.json | 34 +++++++++++ .../responses/gopher_1_1_response.json | 11 ++++ .../responses/gopher_1_2_response.json | 11 ++++ .../responses/gopher_1_response.json | 11 ++++ .../responses/gopher_2_response.json | 11 ++++ 6 files changed, 134 insertions(+) create mode 100644 internal/server/http/test/testdata/imposters_variables/gopher_variables.imp.json create mode 100644 internal/server/http/test/testdata/imposters_variables/responses/gopher_1_1_response.json create mode 100644 internal/server/http/test/testdata/imposters_variables/responses/gopher_1_2_response.json create mode 100644 internal/server/http/test/testdata/imposters_variables/responses/gopher_1_response.json create mode 100644 internal/server/http/test/testdata/imposters_variables/responses/gopher_2_response.json diff --git a/internal/server/http/handler_test.go b/internal/server/http/handler_test.go index ba87c9e..c18938c 100644 --- a/internal/server/http/handler_test.go +++ b/internal/server/http/handler_test.go @@ -2,6 +2,8 @@ package http import ( "bytes" + "encoding/json" + "github.com/gorilla/mux" "io" "net/http" "net/http/httptest" @@ -69,6 +71,60 @@ func TestImposterHandler(t *testing.T) { } } +func TestImposterHandler_Variables(t *testing.T) { + var headers = make(map[string]string) + headers["Content-Type"] = "application/json" + + responseId1 := "test/testdata/imposters_variables/responses/gopher_1_response.json" + responseId2 := "test/testdata/imposters_variables/responses/gopher_2_response.json" + responseId1Variable1 := "test/testdata/imposters_variables/responses/gopher_1_1_response.json" + responseId1Variable2 := "test/testdata/imposters_variables/responses/gopher_1_2_response.json" + + imposterFilePath := "test/testdata/imposters_variables/gopher_variables.imp.json" + imposterFile, _ := os.Open(imposterFilePath) + defer imposterFile.Close() + imposterBytes, _ := io.ReadAll(imposterFile) + + var imposters []Imposter + err := json.Unmarshal(imposterBytes, &imposters) + assert.NoError(t, err) + + var dataTest = []struct { + name string + imposter Imposter + url string + expectedBodyPath string + statusCode int + }{ + {"valid imposter with id 1 in path", imposters[0], "/gophers/1", responseId1, http.StatusOK}, + {"valid imposter with id 2 in path", imposters[0], "/gophers/2", responseId2, http.StatusOK}, + {"valid imposter with id 1 and second variable 1 in path", imposters[1], "/gophers/1/1", responseId1Variable1, http.StatusOK}, + {"valid imposter with id 1 and second variable 2 in path", imposters[1], "/gophers/1/2", responseId1Variable2, http.StatusOK}, + } + + for _, tt := range dataTest { + t.Run(tt.name, func(t *testing.T) { + req, err := http.NewRequest("GET", tt.url, nil) + assert.NoError(t, err) + rec := httptest.NewRecorder() + handler := ImposterHandler(tt.imposter) + + m := mux.NewRouter() + m.Handle(tt.imposter.Request.Endpoint, handler) + m.ServeHTTP(rec, req) + + expectedBodyPathFile, _ := os.Open(tt.expectedBodyPath) + defer expectedBodyPathFile.Close() + expectedBody, _ := io.ReadAll(expectedBodyPathFile) + + //handler.ServeHTTP(rec, req) + assert.Equal(t, rec.Code, tt.statusCode) + assert.Equal(t, string(expectedBody), rec.Body.String()) + + }) + } +} + func TestInvalidRequestWithSchema(t *testing.T) { validRequest := []byte(`{ "data": { diff --git a/internal/server/http/test/testdata/imposters_variables/gopher_variables.imp.json b/internal/server/http/test/testdata/imposters_variables/gopher_variables.imp.json new file mode 100644 index 0000000..8d785ba --- /dev/null +++ b/internal/server/http/test/testdata/imposters_variables/gopher_variables.imp.json @@ -0,0 +1,34 @@ +[ + { + "request": { + "method": "GET", + "endpoint": "/gophers/{id:.*}", + "headers": { + "Content-Type": "application/json" + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "bodyFile": "test/testdata/imposters_variables/responses/gopher_{id}_response.json" + } + }, + { + "request": { + "method": "GET", + "endpoint": "/gophers/{id:.*}/{second_id:.*}", + "headers": { + "Content-Type": "application/json" + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "bodyFile": "test/testdata/imposters_variables/responses/gopher_{id}_{second_id}_response.json" + } + } +] diff --git a/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_1_response.json b/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_1_response.json new file mode 100644 index 0000000..f825bae --- /dev/null +++ b/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_1_response.json @@ -0,0 +1,11 @@ +{ + "data": { + "type": "gophers", + "id": "1_1", + "attributes": { + "name": "Zebediah", + "color": "Purple", + "age": 54 + } + } +} diff --git a/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_2_response.json b/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_2_response.json new file mode 100644 index 0000000..befb38e --- /dev/null +++ b/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_2_response.json @@ -0,0 +1,11 @@ +{ + "data": { + "type": "gophers", + "id": "1_2", + "attributes": { + "name": "Zebediah", + "color": "Purple", + "age": 54 + } + } +} diff --git a/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_response.json b/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_response.json new file mode 100644 index 0000000..410d54d --- /dev/null +++ b/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_response.json @@ -0,0 +1,11 @@ +{ + "data": { + "type": "gophers", + "id": "1", + "attributes": { + "name": "Zebediah", + "color": "Purple", + "age": 54 + } + } +} diff --git a/internal/server/http/test/testdata/imposters_variables/responses/gopher_2_response.json b/internal/server/http/test/testdata/imposters_variables/responses/gopher_2_response.json new file mode 100644 index 0000000..1bbd76e --- /dev/null +++ b/internal/server/http/test/testdata/imposters_variables/responses/gopher_2_response.json @@ -0,0 +1,11 @@ +{ + "data": { + "type": "gophers", + "id": "2", + "attributes": { + "name": "Zebediah", + "color": "Purple", + "age": 54 + } + } +} From 18e28e7f7c16c2a4fc670f2aae6b744a82c1be51 Mon Sep 17 00:00:00 2001 From: Joseph Weigl Date: Wed, 21 Aug 2024 14:10:14 +0200 Subject: [PATCH 2/2] remove unused line --- internal/server/http/handler_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/server/http/handler_test.go b/internal/server/http/handler_test.go index c18938c..19e374b 100644 --- a/internal/server/http/handler_test.go +++ b/internal/server/http/handler_test.go @@ -117,7 +117,6 @@ func TestImposterHandler_Variables(t *testing.T) { defer expectedBodyPathFile.Close() expectedBody, _ := io.ReadAll(expectedBodyPathFile) - //handler.ServeHTTP(rec, req) assert.Equal(t, rec.Code, tt.statusCode) assert.Equal(t, string(expectedBody), rec.Body.String())