Skip to content

Instantly share code, notes, and snippets.

@titanous
Last active December 17, 2015 21:29
Show Gist options
  • Save titanous/5674537 to your computer and use it in GitHub Desktop.
Save titanous/5674537 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"io/ioutil"
"net/http"
. "launchpad.net/gocheck"
)
func stubReq(c *C, f func(), req, res string) {
old := http.DefaultClient.Transport
stub := &stubTransport{res: res}
http.DefaultClient.Transport = stub
f()
http.DefaultClient.Transport = old
c.Assert(stub.req, Equals, req)
}
type stubTransport struct {
req string
res string
}
func (t *stubTransport) RoundTrip(req *http.Request) (*http.Response, error) {
buf := bytes.Buffer{}
buf.ReadFrom(req.Body)
t.req = buf.String()
return &http.Response{
StatusCode: 200,
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{"Content-Type": {"text/xml"}},
Body: ioutil.NopCloser(strings.NewReader(t.res)),
Request: req,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment