-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
106 lines (92 loc) · 2.6 KB
/
api.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package mockhttp
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
)
// TestingT is an interface wrapper around *testing.T.
type TestingT interface {
Error(args ...any)
Errorf(format string, args ...any)
Fatal(args ...any)
Fatalf(format string, args ...any)
}
// APIMock is a representation of a mocked API. It allows to stub HTTP calls and verify invocations.
type APIMock struct {
testServer *httptest.Server
calls map[HTTPCall]http.HandlerFunc
testState TestingT
invocations map[HTTPCall][]*Invocation
mu sync.Mutex
}
// HTTPCall is a simple representation of an endpoint call.
type HTTPCall struct {
Method string
Path string
}
// API creates a new APIMock instance and starts a server exposing it.
func API(testState TestingT) *APIMock {
mockedAPI := &APIMock{
calls: map[HTTPCall]http.HandlerFunc{},
testState: testState,
invocations: map[HTTPCall][]*Invocation{},
}
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, request *http.Request) {
call := HTTPCall{
Method: strings.ToLower(request.Method),
Path: request.URL.Path,
}
mockedAPI.mu.Lock()
invocations := mockedAPI.invocations[call]
invocations = append(invocations, newInvocation(request, testState))
mockedAPI.invocations[call] = invocations
mockedAPI.mu.Unlock()
handler := mockedAPI.calls[call]
if handler != nil {
handler(res, request)
} else {
res.WriteHeader(http.StatusNotFound)
testState.Fatalf("unmocked invocation %s %s\n", call.Method, call.Path)
}
}))
mockedAPI.testServer = testServer
return mockedAPI
}
// Close stops the underlying server.
func (mockedAPI *APIMock) Close() {
mockedAPI.testServer.Close()
}
// GetURL returns the URL of the API underlying server.
func (mockedAPI *APIMock) GetURL() *url.URL {
testServerURL, err := url.Parse(mockedAPI.testServer.URL)
if err != nil {
mockedAPI.testState.Fatal(err)
}
return testServerURL
}
// GetHost returns the host of the API underlying server.
func (mockedAPI *APIMock) GetHost() string {
return mockedAPI.GetURL().Host
}
// Stub creates a new StubBuilder instance for the given method and path.
func (mockedAPI *APIMock) Stub(method string, path string) *StubBuilder {
return &StubBuilder{
api: mockedAPI,
call: &HTTPCall{
Method: strings.ToLower(method),
Path: path,
},
}
}
// Verify creates a new CallVerifier instance for the given method and path.
func (mockedAPI *APIMock) Verify(method string, path string) *CallVerifier {
return &CallVerifier{
api: mockedAPI,
call: &HTTPCall{
Method: strings.ToLower(method),
Path: path,
},
}
}