-
Notifications
You must be signed in to change notification settings - Fork 14
/
test_util.go
52 lines (38 loc) · 1023 Bytes
/
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/http"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"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)
}
func NewTestCoreRequestContext() (*logrus.Logger, *test.Hook, context.Context) {
logger, hook := test.NewNullLogger()
ctx := NewTestCoreRequestContextWithLogger(logger)
return logger, hook, ctx
}
func NewTestCoreRequestContextWithLogger(logger *logrus.Logger) context.Context {
ctx := context.WithValue(context.Background(), coreRequestContextKey{},
&coreRequestContext{
logger: logger,
entry: logger.WithField("traceId", uuid.New().String()),
})
return ctx
}