-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstubber.go
62 lines (55 loc) · 1.24 KB
/
stubber.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
package httpstub
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/http/httptest"
"testing"
)
type Stubber struct {
Stubs []*Stub
Client Client
Config StubberConfig
}
type StubberConfig struct {
DontAssertUnstubbed bool
}
func (s *Stubber) Serve(t *testing.T) (Close func()) {
t.Helper()
if s == nil {
return func() {}
}
sv := httptest.NewTLSServer(s.Router(t))
s.Client.SetClient(stubberClient(sv))
return sv.Close
}
func (s *Stubber) Router(t *testing.T) http.Handler {
t.Helper()
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
stub := s.stubByURL(req.URL.Path)
if !s.Config.DontAssertUnstubbed {
if stub == nil {
t.Fatalf("couldnt match stub for %s url", req.URL.Path)
}
}
stub.intercept(t).ServeHTTP(res, req)
})
}
func (s *Stubber) stubByURL(url string) *Stub {
for _, stub := range s.Stubs {
if stub.URL == url {
return stub
}
}
return nil
}
func stubberClient(sv *httptest.Server) http.Client {
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: func(_ context.Context, network, _ string) (net.Conn, error) {
return net.Dial(network, sv.Listener.Addr().String())
},
}
return http.Client{Transport: transport}
}