-
Notifications
You must be signed in to change notification settings - Fork 44
/
testing.go
65 lines (58 loc) · 1.63 KB
/
testing.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
package recurly
import (
"context"
"crypto/tls"
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
// TestServer is a server used for testing when mocks are not sufficient.
// This enables pointing a recurly client to this test server and simulating
// requests/responses directly.
type TestServer struct {
server *httptest.Server
mux *http.ServeMux
Invoked bool
}
// NewTestServer returns an instance of *Client and *Server, with the
// client resolving to the test server.
func NewTestServer() (*Client, *TestServer) {
s := &TestServer{
mux: http.NewServeMux(),
}
s.server = httptest.NewTLSServer(s.mux)
client := NewClient("test", "foo")
client.Client = &http.Client{
Timeout: 100 * time.Millisecond,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
u, _ := url.Parse(s.server.URL)
return (&net.Dialer{
Timeout: 100 * time.Millisecond,
KeepAlive: 500 * time.Millisecond,
DualStack: true,
}).DialContext(ctx, network, u.Host)
},
},
}
return client, s
}
// HandleFunc sets up an HTTP handler where the HTTP method is asserted.
// fn is the handler, and all invocation will set s.Invoked to true.
func (s *TestServer) HandleFunc(method string, pattern string, fn http.HandlerFunc, t *testing.T) {
s.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
s.Invoked = true
if r.Method != method {
t.Fatalf("unexpected method: %s", r.Method)
}
fn(w, r)
})
}
// Close closes the server.
func (s *TestServer) Close() {
s.server.Close()
}