Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Makefile
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 ./...
10 changes: 8 additions & 2 deletions internal/server/http/handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package http

import (
"github.com/gorilla/mux"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)

Expand All @@ -15,9 +17,10 @@ func ImposterHandler(i Imposter) http.HandlerFunc {
if res.Delay.Delay() > 0 {
time.Sleep(res.Delay.Delay())
}
vars := mux.Vars(r)
writeHeaders(res, w)
w.WriteHeader(res.Status)
writeBody(i, res, w)
writeBody(i, res, w, vars)
}
}

Expand All @@ -31,11 +34,14 @@ func writeHeaders(r Response, w http.ResponseWriter) {
}
}

func writeBody(i Imposter, r Response, w http.ResponseWriter) {
func writeBody(i Imposter, r Response, w http.ResponseWriter, vars map[string]string) {
wb := []byte(r.Body)

if r.BodyFile != nil {
bodyFile := i.CalculateFilePath(*r.BodyFile)
for key, value := range vars {
bodyFile = strings.ReplaceAll(bodyFile, "{"+key+"}", value)
}
wb = fetchBodyFromFile(bodyFile)
}
w.Write(wb)
Expand Down
62 changes: 62 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,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"
Copy link
Member

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.


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},
Copy link
Member

Choose a reason for hiding this comment

The 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 bodyFile path but not in the endpoint, please? Just to reflect what's the expectation in such case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure..
but this is the same behaviour as right now if the file could not be found

"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": {
Expand Down
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
}
}
}