-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_test.go
50 lines (39 loc) · 1 KB
/
http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)
const testURL = "127.0.0.1"
// RoundTripFunc .
type RoundTripFunc func(req *http.Request) *http.Response
// RoundTrip .
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req), nil
}
//NewTestClient returns *http.Client with Transport replaced to avoid making real calls
func NewTestClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: RoundTripFunc(fn),
}
}
//TestDoStuffWithRoundTripper
func TestDoStuffWithRoundTripper(t *testing.T) {
client := NewTestClient(func(req *http.Request) *http.Response {
text := "test test"
// Test request parameters
return &http.Response{
StatusCode: 200,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString(text)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}
})
api := New(client)
_, errAPIGet := api.Get(testURL)
if errAPIGet != nil {
t.Errorf("%#v", errAPIGet)
}
}