-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reuse path variable for bodyfile #176
base: main
Are you sure you want to change the base?
Changes from all commits
a780e39
ddbf72c
d40eb82
18e28e7
9927a4e
c9dbb4b
d69a211
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.PHONY: build | ||
build: | ||
go build -ldflags "-s -w -X 'github.com/friendsofgo/killgrave/internal/app/cmd._version=`git rev-parse --abbrev-ref HEAD`-`git rev-parse --short HEAD`'" -o bin/killgrave cmd/killgrave/main.go | ||
go build -ldflags "-s -w -X 'github.com/friendsofgo/killgrave/internal/app/cmd._version=`git rev-parse --abbrev-ref HEAD`-`git rev-parse --short HEAD`'" -o bin/killgrave cmd/killgrave/main.go | ||
|
||
.PHONY: test | ||
test: | ||
go test -v -vet=off -race ./... |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package http | |
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/gorilla/mux" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
|
@@ -69,6 +71,66 @@ 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" | ||
responseId1WithoutVariable := "test/testdata/imposters_variables/responses/gopher_1_without_variable_response.json" | ||
|
||
imposterFilePath := "test/testdata/imposters_variables/gopher_variables.imp.json" | ||
imposterFile, err := os.Open(imposterFilePath) | ||
require.NoError(t, err) | ||
defer imposterFile.Close() | ||
|
||
imposterBytes, err := io.ReadAll(imposterFile) | ||
require.NoError(t, err) | ||
|
||
var imposters []Imposter | ||
err = json.Unmarshal(imposterBytes, &imposters) | ||
require.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}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a test case where there's a value defined in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for sure.. "200" with empty body |
||
{"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) | ||
require.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) | ||
eloo-abi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer expectedBodyPathFile.Close() | ||
expectedBody, err := io.ReadAll(expectedBodyPathFile) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, rec.Code, tt.statusCode) | ||
assert.Equal(t, string(expectedBody), rec.Body.String()) | ||
|
||
}) | ||
} | ||
} | ||
|
||
func TestInvalidRequestWithSchema(t *testing.T) { | ||
validRequest := []byte(`{ | ||
"data": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
[ | ||
{ | ||
"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" | ||
} | ||
}, | ||
{ | ||
"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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"data": { | ||
"type": "gophers", | ||
"id": "1_1", | ||
"attributes": { | ||
"name": "Hannes", | ||
"color": "Yellow", | ||
"age": 32 | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
eloo-abi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"data": { | ||
"type": "gophers", | ||
"id": "1_2", | ||
"attributes": { | ||
"name": "Manfred", | ||
"color": "Blue", | ||
"age": 31 | ||
} | ||
} | ||
} |
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": "Brian", | ||
"color": "Red", | ||
"age": 35 | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit; I think this could be refactored in a way so we use the file contents in the test (instead of the file path), so we don't need an empty file for the case where there's no data, but it's not critical/blocking for merging this PR.