Last active
December 17, 2015 21:29
-
-
Save titanous/5674537 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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