-
Notifications
You must be signed in to change notification settings - Fork 14
/
test_util.go
52 lines (41 loc) · 1.25 KB
/
test_util.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
package common
import (
"context"
"net"
"net/http"
"net/http/httptest"
"github.com/anz-bank/sysl-go/log"
"github.com/stretchr/testify/mock"
)
func NewString(s string) *string {
return &s
}
func NewBool(b bool) *bool {
return &b
}
type MockRoundTripper struct {
mock.Mock
}
func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
args := m.Called(req)
if args.Error(1) != nil {
return nil, args.Error(1)
}
return args.Get(0).(*http.Response), args.Error(1)
}
// NewHTTPTestServer returns a new httptest.Server with the given handler suitable for use within
// unit tests. The returned server comes equipped with the following:
// 1. log.Logger.
func NewHTTPTestServer(handler http.Handler) *httptest.Server {
ts := NewUnstartedHTTPTestServer(handler)
ts.Start()
return ts
}
// NewHTTPTestServer returns an unstarted httptest.Server with the given handler suitable for use
// within unit tests. The returned server comes equipped with the following:
// 1. log.Logger.
func NewUnstartedHTTPTestServer(handler http.Handler) *httptest.Server {
ts := httptest.NewUnstartedServer(handler)
ts.Config.BaseContext = func(net.Listener) context.Context { return log.PutLogger(context.Background(), log.NewDefaultLogger()) }
return ts
}