-
Notifications
You must be signed in to change notification settings - Fork 107
/
transport_test.go
52 lines (43 loc) · 1.11 KB
/
transport_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
51
52
package gock
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/nbio/st"
)
func TestTransportMatch(t *testing.T) {
defer after()
const uri = "http://foo.com"
New(uri).Reply(204)
u, _ := url.Parse(uri)
req := &http.Request{URL: u}
res, err := NewTransport().RoundTrip(req)
st.Expect(t, err, nil)
st.Expect(t, res.StatusCode, 204)
st.Expect(t, res.Request, req)
}
func TestTransportCannotMatch(t *testing.T) {
defer after()
New("http://foo.com").Reply(204)
u, _ := url.Parse("http://127.0.0.1:1234")
req := &http.Request{URL: u}
_, err := NewTransport().RoundTrip(req)
st.Expect(t, err, ErrCannotMatch)
}
func TestTransportNotIntercepting(t *testing.T) {
defer after()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, world")
}))
defer ts.Close()
New(ts.URL).Reply(200)
Disable()
u, _ := url.Parse(ts.URL)
req := &http.Request{URL: u, Header: make(http.Header)}
res, err := NewTransport().RoundTrip(req)
st.Expect(t, err, nil)
st.Expect(t, Intercepting(), false)
st.Expect(t, res.StatusCode, 200)
}