From 01ea4bad083b0f52160e7ba61c4ddf9d80dc0d2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= Date: Wed, 24 Apr 2019 21:01:04 +0200 Subject: [PATCH] Allowing to define Response headers. Fixes #1 (#10) * Allowing to define Response headers. Fixes #1 * Allowing array of values for Response headers. Related with #1 --- README.md | 6 +++++- handler.go | 15 ++++++++++++++- handler_test.go | 6 +++--- imposter.go | 2 +- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 651b008..b0935fd 100644 --- a/README.md +++ b/README.md @@ -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" } } diff --git a/handler.go b/handler.go index 9834716..cc48667 100644 --- a/handler.go +++ b/handler.go @@ -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) } @@ -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) diff --git a/handler_test.go b/handler_test.go index 6cb06c3..fd92ab5 100644 --- a/handler_test.go +++ b/handler_test.go @@ -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 { diff --git a/imposter.go b/imposter.go index 237fd33..5ade3c1 100644 --- a/imposter.go +++ b/imposter.go @@ -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"` }