diff --git a/internal/server/http/handler_test.go b/internal/server/http/handler_test.go index 19e374b..79df2fc 100644 --- a/internal/server/http/handler_test.go +++ b/internal/server/http/handler_test.go @@ -79,15 +79,19 @@ func TestImposterHandler_Variables(t *testing.T) { 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" + responseId1WithoutVariable := "test/testdata/imposters_variables/responses/gopher_1_without_variable_response.json" imposterFilePath := "test/testdata/imposters_variables/gopher_variables.imp.json" - imposterFile, _ := os.Open(imposterFilePath) + imposterFile, err := os.Open(imposterFilePath) + require.NoError(t, err) defer imposterFile.Close() - imposterBytes, _ := io.ReadAll(imposterFile) + + imposterBytes, err := io.ReadAll(imposterFile) + require.NoError(t, err) var imposters []Imposter - err := json.Unmarshal(imposterBytes, &imposters) - assert.NoError(t, err) + err = json.Unmarshal(imposterBytes, &imposters) + require.NoError(t, err) var dataTest = []struct { name string @@ -100,12 +104,14 @@ func TestImposterHandler_Variables(t *testing.T) { {"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}, + {"valid imposter without variable but body file has variable", imposters[2], "/gophers/1", responseId1WithoutVariable, http.StatusOK}, } for _, tt := range dataTest { + tt := tt t.Run(tt.name, func(t *testing.T) { req, err := http.NewRequest("GET", tt.url, nil) - assert.NoError(t, err) + require.NoError(t, err) rec := httptest.NewRecorder() handler := ImposterHandler(tt.imposter) @@ -115,7 +121,8 @@ func TestImposterHandler_Variables(t *testing.T) { expectedBodyPathFile, _ := os.Open(tt.expectedBodyPath) defer expectedBodyPathFile.Close() - expectedBody, _ := io.ReadAll(expectedBodyPathFile) + expectedBody, err := io.ReadAll(expectedBodyPathFile) + require.NoError(t, err) assert.Equal(t, rec.Code, tt.statusCode) assert.Equal(t, string(expectedBody), rec.Body.String()) 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 index 8d785ba..47d783a 100644 --- a/internal/server/http/test/testdata/imposters_variables/gopher_variables.imp.json +++ b/internal/server/http/test/testdata/imposters_variables/gopher_variables.imp.json @@ -30,5 +30,21 @@ }, "bodyFile": "test/testdata/imposters_variables/responses/gopher_{id}_{second_id}_response.json" } + }, + { + "request": { + "method": "GET", + "endpoint": "/gophers/1", + "headers": { + "Content-Type": "application/json" + } + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "bodyFile": "test/testdata/imposters_variables/responses/gopher_{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 index f825bae..42f60d7 100644 --- 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 @@ -3,9 +3,9 @@ "type": "gophers", "id": "1_1", "attributes": { - "name": "Zebediah", - "color": "Purple", - "age": 54 + "name": "Hannes", + "color": "Yellow", + "age": 32 } } } 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 index befb38e..03d1499 100644 --- 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 @@ -3,9 +3,9 @@ "type": "gophers", "id": "1_2", "attributes": { - "name": "Zebediah", - "color": "Purple", - "age": 54 + "name": "Manfred", + "color": "Blue", + "age": 31 } } } diff --git a/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_without_variable_response.json b/internal/server/http/test/testdata/imposters_variables/responses/gopher_1_without_variable_response.json new file mode 100644 index 0000000..e69de29 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 index 1bbd76e..7e95255 100644 --- 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 @@ -3,9 +3,9 @@ "type": "gophers", "id": "2", "attributes": { - "name": "Zebediah", - "color": "Purple", - "age": 54 + "name": "Brian", + "color": "Red", + "age": 35 } } }