Skip to content

Commit

Permalink
test: add tests for variables in body file
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 '18e28e7f7c16c2a4fc670f2aae6b744a82c1be51':
  remove unused line
  test: add tests for variables in body file
  • Loading branch information
eloo-abi committed Aug 21, 2024
2 parents ddbf72c + 18e28e7 commit 9927a4e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internal/server/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package http

import (
"bytes"
"encoding/json"
"github.com/gorilla/mux"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -69,6 +71,59 @@ 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)

assert.Equal(t, rec.Code, tt.statusCode)
assert.Equal(t, string(expectedBody), rec.Body.String())

})
}
}

func TestInvalidRequestWithSchema(t *testing.T) {
validRequest := []byte(`{
"data": {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"type": "gophers",
"id": "1_1",
"attributes": {
"name": "Zebediah",
"color": "Purple",
"age": 54
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"type": "gophers",
"id": "1_2",
"attributes": {
"name": "Zebediah",
"color": "Purple",
"age": 54
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"type": "gophers",
"id": "1",
"attributes": {
"name": "Zebediah",
"color": "Purple",
"age": 54
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"type": "gophers",
"id": "2",
"attributes": {
"name": "Zebediah",
"color": "Purple",
"age": 54
}
}
}

0 comments on commit 9927a4e

Please sign in to comment.