1// Copyright The OpenTelemetry Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package baggage
16
17import (
18	"context"
19	"testing"
20
21	"github.com/stretchr/testify/assert"
22	"github.com/stretchr/testify/require"
23)
24
25func 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
34func 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
47func 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
56func 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	// Ensure resetting the hook works.
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
77func 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	// Ensure resetting the hook works.
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