Fix fake handler so it can be used for more than one call.

This commit is contained in:
Daniel Smith 2014-06-17 17:36:27 -07:00
parent 3737b4e4e2
commit b6a260940c
2 changed files with 5 additions and 4 deletions

View File

@ -166,7 +166,7 @@ func TestDoRequest(t *testing.T) {
if body != expectedBody {
t.Errorf("Expected body: '%s', saw: '%s'", expectedBody, body)
}
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", &fakeHandler.ResponseBody)
fakeHandler.ValidateRequest(t, "/foo/bar", "GET", nil)
}
func TestRunController(t *testing.T) {

View File

@ -32,6 +32,7 @@ type LogInterface interface {
// FakeHandler is to assist in testing HTTP requests.
type FakeHandler struct {
RequestReceived *http.Request
RequestBody string
StatusCode int
ResponseBody string
// For logging - you can use a *testing.T
@ -48,7 +49,7 @@ func (f *FakeHandler) ServeHTTP(response http.ResponseWriter, request *http.Requ
if err != nil && f.T != nil {
f.T.Logf("Received read error: %#v", err)
}
f.ResponseBody = string(bodyReceived)
f.RequestBody = string(bodyReceived)
}
func (f FakeHandler) ValidateRequest(t TestInterface, expectedPath, expectedMethod string, body *string) {
@ -59,8 +60,8 @@ func (f FakeHandler) ValidateRequest(t TestInterface, expectedPath, expectedMeth
t.Errorf("Unexpected method: %s", f.RequestReceived.Method)
}
if body != nil {
if *body != f.ResponseBody {
t.Errorf("Received body:\n%s\n Doesn't match expected body:\n%s", f.ResponseBody, *body)
if *body != f.RequestBody {
t.Errorf("Received body:\n%s\n Doesn't match expected body:\n%s", f.RequestBody, *body)
}
}
}