-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
testutils.go
72 lines (55 loc) · 1.39 KB
/
testutils.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
package mercury
import (
"net/http"
"net/http/httptest"
"sync/atomic"
)
type MercuryEndpointMock interface {
URL() string
Username() string
Password() string
CallCount() int
RegisterHandler(http.HandlerFunc)
}
var _ MercuryEndpointMock = &SimulatedMercuryServer{}
var notFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
type SimulatedMercuryServer struct {
server *httptest.Server
handler http.HandlerFunc
callCounter atomic.Int32
}
func NewSimulatedMercuryServer() *SimulatedMercuryServer {
srv := &SimulatedMercuryServer{
handler: notFoundHandler,
callCounter: atomic.Int32{},
}
srv.server = httptest.NewUnstartedServer(srv)
return srv
}
func (ms *SimulatedMercuryServer) URL() string {
return ms.server.URL
}
func (ms *SimulatedMercuryServer) Username() string {
return "username1"
}
func (ms *SimulatedMercuryServer) Password() string {
return "password1"
}
func (ms *SimulatedMercuryServer) CallCount() int {
return int(ms.callCounter.Load())
}
func (ms *SimulatedMercuryServer) RegisterHandler(h http.HandlerFunc) {
ms.handler = h
}
func (ms *SimulatedMercuryServer) Start() {
ms.server.Start()
}
func (ms *SimulatedMercuryServer) Stop() {
ms.server.Close()
}
func (ms *SimulatedMercuryServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ms.callCounter.Add(1)
ms.handler.ServeHTTP(w, r)
}