Created
March 10, 2013 12:51
-
-
Save karlseguin/5128461 to your computer and use it in GitHub Desktop.
A fake http.ResponseWriter class, used for testing. See http://openmymind.net/Testing-In-Go/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fakeresponse | |
import ( | |
"testing" | |
"net/http" | |
) | |
type FakeResponse struct { | |
t *testing.T | |
headers http.Header | |
body []byte | |
status int | |
} | |
func New(t *testing.T) (*FakeResponse) { | |
return &FakeResponse { | |
t: t, | |
headers: make(http.Header), | |
} | |
} | |
func (r *FakeResponse) Header() http.Header { | |
return r.headers | |
} | |
func (r *FakeResponse) Write(body []byte) (int, error) { | |
r.body = body | |
return len(body), nil | |
} | |
func (r *FakeResponse) WriteHeader(status int) { | |
r.status = status | |
} | |
func (r *FakeResponse) Assert(status int, body string) { | |
if r.status != status { | |
r.t.Errorf("expected status %+v to equal %+v", r.status, status) | |
} | |
if string(r.body) != body { | |
r.t.Errorf("expected body %+v to equal %+v", string(r.body), body) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now you can just use the built in ResponseRecorder?