...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package baggage
16
17 import (
18 "context"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23 )
24
25 func TestContextWithList(t *testing.T) {
26 ctx := context.Background()
27 l := List{"foo": {Value: "1"}}
28
29 nCtx := ContextWithList(ctx, l)
30 assert.Equal(t, baggageState{list: l}, nCtx.Value(baggageKey))
31 assert.Nil(t, ctx.Value(baggageKey))
32 }
33
34 func TestClearContextOfList(t *testing.T) {
35 l := List{"foo": {Value: "1"}}
36
37 ctx := context.Background()
38 ctx = context.WithValue(ctx, baggageKey, l)
39
40 nCtx := ContextWithList(ctx, nil)
41 nL, ok := nCtx.Value(baggageKey).(baggageState)
42 require.True(t, ok, "wrong type stored in context")
43 assert.Nil(t, nL.list)
44 assert.Equal(t, l, ctx.Value(baggageKey))
45 }
46
47 func TestListFromContext(t *testing.T) {
48 ctx := context.Background()
49 assert.Nil(t, ListFromContext(ctx))
50
51 l := List{"foo": {Value: "1"}}
52 ctx = context.WithValue(ctx, baggageKey, baggageState{list: l})
53 assert.Equal(t, l, ListFromContext(ctx))
54 }
55
56 func TestContextWithSetHook(t *testing.T) {
57 var called bool
58 f := func(ctx context.Context, list List) context.Context {
59 called = true
60 return ctx
61 }
62
63 ctx := context.Background()
64 ctx = ContextWithSetHook(ctx, f)
65 assert.False(t, called, "SetHookFunc called when setting hook")
66 ctx = ContextWithList(ctx, nil)
67 assert.True(t, called, "SetHookFunc not called when setting List")
68
69
70 called = false
71 ctx = ContextWithSetHook(ctx, f)
72 assert.False(t, called, "SetHookFunc called when re-setting hook")
73 ContextWithList(ctx, nil)
74 assert.True(t, called, "SetHookFunc not called when re-setting List")
75 }
76
77 func TestContextWithGetHook(t *testing.T) {
78 var called bool
79 f := func(ctx context.Context, list List) List {
80 called = true
81 return list
82 }
83
84 ctx := context.Background()
85 ctx = ContextWithGetHook(ctx, f)
86 assert.False(t, called, "GetHookFunc called when setting hook")
87 _ = ListFromContext(ctx)
88 assert.True(t, called, "GetHookFunc not called when getting List")
89
90
91 called = false
92 ctx = ContextWithGetHook(ctx, f)
93 assert.False(t, called, "GetHookFunc called when re-setting hook")
94 _ = ListFromContext(ctx)
95 assert.True(t, called, "GetHookFunc not called when re-getting List")
96 }
97
View as plain text