Create a gist now

Instantly share code, notes, and snippets.

A fake http.ResponseWriter class, used for testing. See http://openmymind.net/Testing-In-Go/
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