Skip to content

Instantly share code, notes, and snippets.

@jwreagor
Forked from karlseguin/fakeresponse.go
Created March 27, 2017 18:57
Show Gist options
  • Save jwreagor/c6177ef57dcf686fe56d1f2ed2f30706 to your computer and use it in GitHub Desktop.
Save jwreagor/c6177ef57dcf686fe56d1f2ed2f30706 to your computer and use it in GitHub Desktop.
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