Skip to content

Commit

Permalink
Allowing to define Response headers. Fixes #1 (#10)
Browse files Browse the repository at this point in the history
* Allowing to define Response headers. Fixes #1

* Allowing array of values for Response headers. Related with #1
  • Loading branch information
joanlopez authored and aperezg committed Apr 24, 2019
1 parent 0b2e339 commit 01ea4ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ imposters/create_gopher.json
},
"response": {
"status": 200,
"content_type": "application/json",
"headers": {
"Content-Type": [
"application/json"
]
},
"bodyFile": "responses/create_gopher_response.json"
}
}
Expand Down
15 changes: 14 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func ImposterHandler(imposter Imposter) http.HandlerFunc {
return
}

w.Header().Set("Content-Type", imposter.Response.ContentType)

writeHeaders(imposter, w)
w.WriteHeader(imposter.Response.Status)
writeBody(imposter, w)
}
Expand Down Expand Up @@ -100,6 +101,18 @@ func compareHeaderValues(a, b []string) bool {
return true
}

func writeHeaders(imposter Imposter, w http.ResponseWriter) {
if imposter.Response.Headers == nil {
return
}

for key, values := range *imposter.Response.Headers {
for _, val := range values {
w.Header().Add(key, val)
}
}
}

func writeBody(imposter Imposter, w http.ResponseWriter) {
wb := []byte(imposter.Response.Body)

Expand Down
6 changes: 3 additions & 3 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func TestImposterHandler(t *testing.T) {
expectedBody string
statusCode int
}{
{"valid imposter with body", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, ContentType: "application/json", Body: body}}, body, http.StatusOK},
{"valid imposter with bodyFile", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, ContentType: "application/json", BodyFile: &bodyFile}}, string(expectedBodyFileData), http.StatusOK},
{"valid imposter with not exists bodyFile", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, ContentType: "application/json", BodyFile: &bodyFileFake}}, "", http.StatusOK},
{"valid imposter with body", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, Headers: &http.Header{"Content-Type": []string{"application/json"}}, Body: body}}, body, http.StatusOK},
{"valid imposter with bodyFile", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, Headers: &http.Header{"Content-Type": []string{"application/json"}}, BodyFile: &bodyFile}}, string(expectedBodyFileData), http.StatusOK},
{"valid imposter with not exists bodyFile", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, Headers: &http.Header{"Content-Type": []string{"application/json"}}, BodyFile: &bodyFileFake}}, "", http.StatusOK},
}

for _, tt := range dataTest {
Expand Down
2 changes: 1 addition & 1 deletion imposter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ type Response struct {
Status int `json:"status"`
Body string `json:"body"`
BodyFile *string `json:"bodyFile"`
ContentType string `json:"content_type"`
Headers *http.Header `json:"headers"`
}

0 comments on commit 01ea4ba

Please sign in to comment.