/
statham_test.go
81 lines (61 loc) · 1.85 KB
/
statham_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package statham_test
import (
"net/http"
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/xoebus/statham"
)
func TestStatham(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Statham Suite")
}
type fakeRoundTripper struct {
Calls int
}
func (rt *fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
rt.Calls++
return nil, nil
}
var _ = Describe("A per-host transport mapping", func() {
var (
defaultTransport *fakeRoundTripper
orgTransport *fakeRoundTripper
comTransport *fakeRoundTripper
transport http.RoundTripper
)
BeforeEach(func() {
defaultTransport = &fakeRoundTripper{}
orgTransport = &fakeRoundTripper{}
comTransport = &fakeRoundTripper{}
transport = statham.NewTransport(defaultTransport, statham.Mapping{
"example.org": orgTransport,
"example.com": comTransport,
})
})
Context("when the host doesn't match any domain in the mapping", func() {
It("uses the default transport", func() {
req, _ := http.NewRequest("GET", "http://example.net", nil)
transport.RoundTrip(req)
Expect(defaultTransport.Calls).To(Equal(1))
Expect(orgTransport.Calls).To(Equal(0))
Expect(comTransport.Calls).To(Equal(0))
})
})
Context("when the host matches a domain in the mapping", func() {
It("uses the corresponding transport", func() {
req, _ := http.NewRequest("GET", "http://example.com", nil)
transport.RoundTrip(req)
Expect(defaultTransport.Calls).To(Equal(0))
Expect(orgTransport.Calls).To(Equal(0))
Expect(comTransport.Calls).To(Equal(1))
})
It("uses the corresponding transport for another host", func() {
req, _ := http.NewRequest("GET", "http://example.org", nil)
transport.RoundTrip(req)
Expect(defaultTransport.Calls).To(Equal(0))
Expect(orgTransport.Calls).To(Equal(1))
Expect(comTransport.Calls).To(Equal(0))
})
})
})