-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
78 lines (68 loc) · 1.7 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package servicectx
import (
"context"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)
func TestFromContext(t *testing.T) {
emptyCtx := context.Background()
require.Equal(
t,
Properties{},
FromContext(emptyCtx),
"an empty struct must be returned from an empty context",
)
}
func TestProperties_InjectIntoContext(t *testing.T) {
props := New()
props.Set("a", "property", "value-a")
props.Set("b", "property", "value-b")
ctx := props.InjectIntoContext(context.Background())
require.Equal(
t,
Properties{
"a": Values{
"property": "value-a",
},
"b": Values{
"property": "value-b",
},
},
FromContext(ctx),
"properties must be successfully added to and retrieved from the context",
)
}
func Test_InjectIntoHeadersFromContext(t *testing.T) {
props := New()
props.Set("a", "property", "value-a")
props.Set("b", "property", "value-b")
ctx := props.InjectIntoContext(context.Background())
httpHeader := http.Header{}
InjectIntoHeadersFromContext(ctx, httpHeader)
require.Equal(t, "value-a", httpHeader.Get("x-service-a-property"))
require.Equal(t, "value-b", httpHeader.Get("x-service-b-property"))
}
func Test_InjectIntoContextFromRequest(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Header.Set("X-Service-A-Option", "value-a")
req.Header.Set("x-service-b-option", "value-b")
req.URL.RawQuery = "x-service-c-option=value-c"
ctx := InjectIntoContextFromRequest(context.Background(), req)
require.Equal(
t,
Properties{
"a": Values{
"option": "value-a",
},
"b": Values{
"option": "value-b",
},
"c": Values{
"option": "value-c",
},
},
FromContext(ctx),
"properties must be parsed from http.Header",
)
}