]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | package thrift | |
21 | ||
22 | import ( | |
23 | "context" | |
24 | "reflect" | |
25 | "testing" | |
26 | ) | |
27 | ||
28 | func TestSetGetHeader(t *testing.T) { | |
29 | const ( | |
30 | key = "foo" | |
31 | value = "bar" | |
32 | ) | |
33 | ctx := context.Background() | |
34 | ||
35 | ctx = SetHeader(ctx, key, value) | |
36 | ||
37 | checkGet := func(t *testing.T, ctx context.Context) { | |
38 | t.Helper() | |
39 | got, ok := GetHeader(ctx, key) | |
40 | if !ok { | |
41 | t.Fatalf("Cannot get header %q back after setting it.", key) | |
42 | } | |
43 | if got != value { | |
44 | t.Fatalf("Header value expected %q, got %q instead", value, got) | |
45 | } | |
46 | } | |
47 | ||
48 | checkGet(t, ctx) | |
49 | ||
50 | t.Run( | |
51 | "NoConflicts", | |
52 | func(t *testing.T) { | |
53 | type otherType string | |
54 | const otherValue = "bar2" | |
55 | ||
56 | ctx = context.WithValue(ctx, otherType(key), otherValue) | |
57 | checkGet(t, ctx) | |
58 | }, | |
59 | ) | |
60 | ||
61 | t.Run( | |
62 | "GetHeaderOnNonExistKey", | |
63 | func(t *testing.T) { | |
64 | const otherKey = "foo2" | |
65 | ||
66 | if _, ok := GetHeader(ctx, otherKey); ok { | |
67 | t.Errorf("GetHeader returned ok on non-existing key %q", otherKey) | |
68 | } | |
69 | }, | |
70 | ) | |
71 | } | |
72 | ||
73 | func TestReadKeyList(t *testing.T) { | |
74 | headers := THeaderMap{ | |
75 | "key1": "value1", | |
76 | "key2": "value2", | |
77 | } | |
78 | ctx := context.Background() | |
79 | ||
80 | ctx = AddReadTHeaderToContext(ctx, headers) | |
81 | ||
82 | got := make(THeaderMap) | |
83 | keys := GetReadHeaderList(ctx) | |
84 | t.Logf("keys: %+v", keys) | |
85 | for _, key := range keys { | |
86 | value, ok := GetHeader(ctx, key) | |
87 | if ok { | |
88 | got[key] = value | |
89 | } else { | |
90 | t.Errorf("Cannot get key %q from context", key) | |
91 | } | |
92 | } | |
93 | ||
94 | if !reflect.DeepEqual(headers, got) { | |
95 | t.Errorf("Expected header map %+v, got %+v", headers, got) | |
96 | } | |
97 | ||
98 | writtenKeys := GetWriteHeaderList(ctx) | |
99 | if len(writtenKeys) > 0 { | |
100 | t.Errorf( | |
101 | "Expected empty GetWriteHeaderList() result, got %+v", | |
102 | writtenKeys, | |
103 | ) | |
104 | } | |
105 | } | |
106 | ||
107 | func TestWriteKeyList(t *testing.T) { | |
108 | keys := []string{ | |
109 | "key1", | |
110 | "key2", | |
111 | } | |
112 | ctx := context.Background() | |
113 | ||
114 | ctx = SetWriteHeaderList(ctx, keys) | |
115 | got := GetWriteHeaderList(ctx) | |
116 | ||
117 | if !reflect.DeepEqual(keys, got) { | |
118 | t.Errorf("Expected header keys %+v, got %+v", keys, got) | |
119 | } | |
120 | ||
121 | readKeys := GetReadHeaderList(ctx) | |
122 | if len(readKeys) > 0 { | |
123 | t.Errorf( | |
124 | "Expected empty GetReadHeaderList() result, got %+v", | |
125 | readKeys, | |
126 | ) | |
127 | } | |
128 | } |