Skip to content

Instantly share code, notes, and snippets.

@bgadrian
Created December 15, 2018 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgadrian/92e51f4b2d9b32893ec8f95c40327e3a to your computer and use it in GitHub Desktop.
Save bgadrian/92e51f4b2d9b32893ec8f95c40327e3a to your computer and use it in GitHub Desktop.
Fake a HTTP Request/Client.Do response w/o a webserver.
type recordingTransport struct {
req *http.Request
}
func (t *recordingTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
t.req = req
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("alfa")),
}, nil
}
func TestNewScraper(t *testing.T) {
client = &http.Client{
Timeout: time.Millisecond * 10,
Transport: &recordingTransport{},
}
request, _ := http.NewRequest("", "http://dummy.faketld/", nil)
resp := Client.Do(request)
//respBody will be "alfa"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment