-
Notifications
You must be signed in to change notification settings - Fork 14
/
rest_downstream.go
62 lines (52 loc) · 1.34 KB
/
rest_downstream.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 e2e
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"time"
"github.com/anz-bank/sysl-go/syslgo"
"github.com/go-chi/chi"
)
const (
// DownstreamTimeout is only used for mocked downstreams, and is as small as possible to make the timeout tests
// quicker, but give them time to actually succeed.
DownstreamTimeout = time.Millisecond * 400
)
type restDownstream struct {
t syslgo.TestingT
server *httptest.Server
r chi.Router
hostname string
handlers map[string]*restOrderedHandlers
}
func newBackEnd(t syslgo.TestingT, hostname string) *restDownstream {
be := &restDownstream{
t: t,
r: chi.NewRouter(),
handlers: map[string]*restOrderedHandlers{},
hostname: hostname,
}
be.server = httptest.NewServer(be.r)
be.server.Client().Timeout = DownstreamTimeout
return be
}
func (d *restDownstream) init(method, path string) *restOrderedHandlers {
h := &restOrderedHandlers{
Mutex: sync.Mutex{},
t: d.t,
handlers: nil,
}
d.handlers[fmt.Sprintf("%s %s", method, path)] = h
d.r.Method(method, path, h)
return h
}
func (d *restDownstream) getClient() (*http.Client, string, error) {
return d.server.Client(), d.server.URL, nil
}
func (d *restDownstream) close() {
for methodAndPath, h := range d.handlers {
h.assertCompleted(d.hostname, methodAndPath)
}
d.server.Close()
}