Skip to content

Commit 01ea4ba

Browse files
joanlopezaperezg
authored andcommitted
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
1 parent 0b2e339 commit 01ea4ba

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ imposters/create_gopher.json
7070
},
7171
"response": {
7272
"status": 200,
73-
"content_type": "application/json",
73+
"headers": {
74+
"Content-Type": [
75+
"application/json"
76+
]
77+
},
7478
"bodyFile": "responses/create_gopher_response.json"
7579
}
7680
}

handler.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ func ImposterHandler(imposter Imposter) http.HandlerFunc {
2929
return
3030
}
3131

32-
w.Header().Set("Content-Type", imposter.Response.ContentType)
32+
33+
writeHeaders(imposter, w)
3334
w.WriteHeader(imposter.Response.Status)
3435
writeBody(imposter, w)
3536
}
@@ -100,6 +101,18 @@ func compareHeaderValues(a, b []string) bool {
100101
return true
101102
}
102103

104+
func writeHeaders(imposter Imposter, w http.ResponseWriter) {
105+
if imposter.Response.Headers == nil {
106+
return
107+
}
108+
109+
for key, values := range *imposter.Response.Headers {
110+
for _, val := range values {
111+
w.Header().Add(key, val)
112+
}
113+
}
114+
}
115+
103116
func writeBody(imposter Imposter, w http.ResponseWriter) {
104117
wb := []byte(imposter.Response.Body)
105118

handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func TestImposterHandler(t *testing.T) {
4545
expectedBody string
4646
statusCode int
4747
}{
48-
{"valid imposter with body", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, ContentType: "application/json", Body: body}}, body, http.StatusOK},
49-
{"valid imposter with bodyFile", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, ContentType: "application/json", BodyFile: &bodyFile}}, string(expectedBodyFileData), http.StatusOK},
50-
{"valid imposter with not exists bodyFile", Imposter{Request: validRequest, Response: Response{Status: http.StatusOK, ContentType: "application/json", BodyFile: &bodyFileFake}}, "", http.StatusOK},
48+
{"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},
49+
{"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},
50+
{"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},
5151
}
5252

5353
for _, tt := range dataTest {

imposter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ type Response struct {
2121
Status int `json:"status"`
2222
Body string `json:"body"`
2323
BodyFile *string `json:"bodyFile"`
24-
ContentType string `json:"content_type"`
24+
Headers *http.Header `json:"headers"`
2525
}

0 commit comments

Comments
 (0)