Skip to content

Instantly share code, notes, and snippets.

@justonia
Last active August 29, 2015 14:10
Show Gist options
  • Save justonia/d95cc2da53d418b24433 to your computer and use it in GitHub Desktop.
Save justonia/d95cc2da53d418b24433 to your computer and use it in GitHub Desktop.
type MockRoundTripper struct {
Code int
Body string
Err error
Called int
}
func (t *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
t.Called += 1
if t.Err != nil {
return nil, t.Err
}
resp := &http.Response{}
resp.Status = fmt.Sprintf("%d OK", t.Code)
resp.StatusCode = t.Code
resp.Proto = "HTTP/1.1"
resp.ProtoMajor = 1
resp.ProtoMinor = 1
resp.Body = ioutil.NopCloser(strings.NewReader(t.Body))
resp.ContentLength = int64(len(t.Body))
resp.Close = true
resp.Request = req
return resp, nil
}
func (s *ConnSuite) TestRoundtrip(c *C) {
data := "blah"
client := &http.Client{Transport: &MockRoundTripper{200, data, nil}}
resp, err := client.Get("http://foo/bar")
c.Assert(err, IsNil)
c.Assert(resp, NotNil)
c.Assert(resp.StatusCode, Equals, 200)
respData, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
c.Assert(string(respData), Equals, data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment