-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_test.go
57 lines (48 loc) · 1.44 KB
/
context_test.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
package hexa
import (
"context"
"net/http"
"testing"
"github.com/kamva/gutil"
"github.com/kamva/hexa/hlog"
"github.com/stretchr/testify/assert"
)
func newTestContext() (context.Context, *ContextParams) {
r := gutil.Must(http.NewRequest("POST", "http://a.com", nil)).(*http.Request)
params := ContextParams{
Request: r,
CorrelationId: "abc",
Locale: "def",
User: NewGuest(),
BaseLogger: hlog.GlobalLogger(),
BaseTranslator: &emptyTranslator{},
}
return NewContext(context.Background(), params), ¶ms
}
func assertContextWithParams(ctx context.Context, t *testing.T, params *ContextParams) {
assert.Equal(t, params.Request, CtxRequest(ctx))
assert.Equal(t, params.CorrelationId, CtxCorrelationId(ctx))
assert.Equal(t, params.Locale, CtxLocale(ctx))
assert.Equal(t, params.User, CtxUser(ctx))
assert.NotNil(t, CtxLogger(ctx))
assert.NotNil(t, CtxTranslator(ctx))
}
func TestNewContext(t *testing.T) {
ctx, params := newTestContext()
if !assert.NotNil(t, ctx) {
return
}
assertContextWithParams(ctx, t, params)
}
func TestWithUser(t *testing.T) {
ctx, params := newTestContext()
assert.Equal(t, CtxUser(ctx), params.User)
newUser := NewGuest()
newCtx := WithUser(ctx, newUser)
assert.Equal(t, CtxUser(ctx), params.User)
assert.Equal(t, CtxUser(ctx), newUser)
// assert old context is good:
assertContextWithParams(ctx, t, params)
params.User = newUser
assertContextWithParams(newCtx, t, params)
}