14 func composeQuery(path
string, code
int, headers http
.Header
, body
[]byte) (string, error
) {
15 u
, err
:= url
.Parse(path
)
21 q
.Set("respStatus", strconv
.Itoa(code
))
24 h
, err
:= json
.Marshal(headers
)
28 q
.Set("respHeader", base64
.URLEncoding
.EncodeToString(h
))
31 q
.Set("respBody", base64
.URLEncoding
.EncodeToString(body
))
33 u
.RawQuery
= q
.Encode()
34 return u
.String(), nil
37 func TestResponseOverride(t
*testing
.T
) {
44 {name
: "code", code
: 204},
45 {name
: "body", body
: []byte("new body")},
49 "Via": []string{"Via1", "Via2"},
50 "Content-Type": []string{"random content"},
56 body
: []byte("new body"),
58 "Via": []string{"Via1", "Via2"},
59 "Content-Type": []string{"random content"},
64 for _
, test
:= range tests
{
65 u
, err
:= composeQuery("http://test.com/override", test
.code
, test
.headers
, test
.body
)
67 t
.Errorf("%s: composeQuery: %v", test
.name
, err
)
70 req
, err
:= http
.NewRequest("GET", u
, nil)
72 t
.Errorf("%s: http.NewRequest: %v", test
.name
, err
)
75 w
:= httptest
.NewRecorder()
76 defaultResponse(w
, req
)
78 if got
, want
:= w
.Code
, test
.code
; got
!= want
{
79 t
.Errorf("%s: response code: got %d want %d", test
.name
, got
, want
)
83 if test
.headers
!= nil {
84 for k
, want
:= range test
.headers
{
85 got
, ok
:= w
.HeaderMap
[k
]
86 if !ok ||
!reflect
.DeepEqual(got
, want
) {
87 t
.Errorf("%s: header %s: code: got %v want %v", test
.name
, k
, got
, want
)
93 if got
, want
:= string(w
.Body
.Bytes()), string(test
.body
); got
!= want
{
94 t
.Errorf("%s: body: got %s want %s", test
.name
, got
, want
)