Skip to content

Commit

Permalink
Pull request friendsofgo#3: fix: adjust test data and handle errors
Browse files Browse the repository at this point in the history
Merge in OSSC/killgrave from feature/friendsofgoGH-175-reuse-path-variable-for-bodyfile to feat/friendsofgoGH-175-reuse-path-variable-for-bodyfile

* commit 'c9dbb4b7afa8c138d678ba338d7f4be397297034':
  fix: adjust test data and handle errors
  • Loading branch information
eloo-abi committed Sep 13, 2024
2 parents 9927a4e + c9dbb4b commit d69a211
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
19 changes: 13 additions & 6 deletions internal/server/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"type": "gophers",
"id": "1_1",
"attributes": {
"name": "Zebediah",
"color": "Purple",
"age": 54
"name": "Hannes",
"color": "Yellow",
"age": 32
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"type": "gophers",
"id": "1_2",
"attributes": {
"name": "Zebediah",
"color": "Purple",
"age": 54
"name": "Manfred",
"color": "Blue",
"age": 31
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"type": "gophers",
"id": "2",
"attributes": {
"name": "Zebediah",
"color": "Purple",
"age": 54
"name": "Brian",
"color": "Red",
"age": 35
}
}
}

0 comments on commit d69a211

Please sign in to comment.