forked from yarpc/yarpc-go
/
behavior.go
196 lines (171 loc) · 5.49 KB
/
behavior.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package headers
import (
"context"
"time"
"github.com/crossdock/crossdock-go"
"go.uber.org/yarpc"
"go.uber.org/yarpc/encoding/json"
"go.uber.org/yarpc/encoding/raw"
disp "go.uber.org/yarpc/internal/crossdock/client/dispatcher"
"go.uber.org/yarpc/internal/crossdock/client/params"
"go.uber.org/yarpc/internal/crossdock/client/random"
"go.uber.org/yarpc/internal/crossdock/internal"
"go.uber.org/yarpc/internal/crossdock/thrift/echo"
"go.uber.org/yarpc/internal/crossdock/thrift/echo/echoclient"
)
func createHeadersT(t crossdock.T) crossdock.T {
t.Tag("transport", t.Param(params.Transport))
t.Tag("encoding", t.Param(params.Encoding))
t.Tag("server", t.Param(params.Server))
return t
}
// Run runs the headers behavior
func Run(t crossdock.T) {
t = createHeadersT(t)
fatals := crossdock.Fatals(t)
assert := crossdock.Assert(t)
checks := crossdock.Checks(t)
dispatcher := disp.Create(t)
fatals.NoError(dispatcher.Start(), "could not start Dispatcher")
defer dispatcher.Stop()
var caller headerCaller
encoding := t.Param(params.Encoding)
switch encoding {
case "raw":
caller = rawCaller{raw.New(dispatcher.ClientConfig("yarpc-test"))}
case "json":
caller = jsonCaller{json.New(dispatcher.ClientConfig("yarpc-test"))}
case "thrift":
caller = thriftCaller{echoclient.New(dispatcher.ClientConfig("yarpc-test"))}
default:
fatals.Fail("", "unknown encoding %q", encoding)
}
token1 := random.String(10)
token2 := random.String(10)
tests := []struct {
desc string
give map[string]string
want map[string]string
}{
{
"valid headers",
map[string]string{"token1": token1, "token2": token2},
map[string]string{"token1": token1, "token2": token2},
},
{
"non-string values",
map[string]string{"token": "42"},
map[string]string{"token": "42"},
},
{
"empty strings",
map[string]string{"token": ""},
map[string]string{"token": ""},
},
{
"no headers",
nil,
nil,
},
{
"empty map",
map[string]string{},
map[string]string{},
},
{
"varying casing",
map[string]string{"ToKeN1": token1, "tOkEn2": token2},
map[string]string{"token1": token1, "token2": token2},
},
{
"http header conflict",
map[string]string{"Rpc-Procedure": "does not exist"},
map[string]string{"rpc-procedure": "does not exist"},
},
{
"mixed case value",
map[string]string{"token": "MIXED case Value"},
map[string]string{"token": "MIXED case Value"},
},
}
for _, tt := range tests {
gotHeaders, err := caller.Call(tt.give)
if checks.NoError(err, "%v: call failed", tt.desc) {
internal.RemoveVariableMapKeys(gotHeaders)
// assert.Equal doesn't work with nil maps
if len(tt.want) == 0 {
assert.Empty(gotHeaders, "%v: returns valid headers", tt.desc)
} else {
assert.Equal(tt.want, gotHeaders, "%v: returns valid headers", tt.desc)
}
}
}
}
type headerCaller interface {
Call(map[string]string) (map[string]string, error)
}
type rawCaller struct{ c raw.Client }
func (c rawCaller) Call(h map[string]string) (map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
var (
opts []yarpc.CallOption
resHeaders map[string]string
)
for k, v := range h {
opts = append(opts, yarpc.WithHeader(k, v))
}
opts = append(opts, yarpc.ResponseHeaders(&resHeaders))
_, err := c.c.Call(ctx, "echo/raw", []byte("hello"), opts...)
return resHeaders, err
}
type jsonCaller struct{ c json.Client }
func (c jsonCaller) Call(h map[string]string) (map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
var (
opts []yarpc.CallOption
resHeaders map[string]string
)
for k, v := range h {
opts = append(opts, yarpc.WithHeader(k, v))
}
opts = append(opts, yarpc.ResponseHeaders(&resHeaders))
var resBody interface{}
err := c.c.Call(ctx, "echo", map[string]interface{}{}, &resBody, opts...)
return resHeaders, err
}
type thriftCaller struct{ c echoclient.Interface }
func (c thriftCaller) Call(h map[string]string) (map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
var (
opts []yarpc.CallOption
resHeaders map[string]string
)
for k, v := range h {
opts = append(opts, yarpc.WithHeader(k, v))
}
opts = append(opts, yarpc.ResponseHeaders(&resHeaders))
_, err := c.c.Echo(ctx, &echo.Ping{Beep: "hello"}, opts...)
return resHeaders, err
}