/
behavior.go
197 lines (177 loc) · 6.43 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
197
// Copyright (c) 2018 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 errorstchclient
import (
"fmt"
"time"
"github.com/crossdock/crossdock-go"
"github.com/uber/tchannel-go"
"github.com/uber/tchannel-go/json"
"go.uber.org/yarpc/internal/crossdock/client/params"
)
const (
serverPort = 8082
serviceName = "yarpc-test"
)
type test struct {
name string
encoding string
procedure string
headers []byte
body []byte
validate func(res3 []byte, isAppExpr bool, err error)
}
// Run exercises a YARPC server with outbound TChannel requests from a rigged
// client and validates behavior that might only be visible to a TChannel
// client without the YARPC abstraction interposed, typically errors.
func Run(t crossdock.T) {
fatals := crossdock.Fatals(t)
assert := crossdock.Assert(t)
tests := []test{
{
name: "happy path",
procedure: "echo",
body: []byte("{}"),
headers: []byte("{}"),
validate: func(res3 []byte, isAppErr bool, err error) {
assert.NoError(err, "is not error")
assert.False(isAppErr, "malformed body must not be application error")
},
},
{
name: "malformed body",
procedure: "echo",
body: []byte(""),
headers: []byte("{}"),
validate: func(res3 []byte, isAppErr bool, err error) {
assert.Error(err, "is error")
assert.False(isAppErr, "malformed body must not be application error")
err, ok := err.(tchannel.SystemError)
assert.True(ok, "malformed body must produce system error")
if !ok {
return
}
code := tchannel.GetSystemErrorCode(err)
assert.Contains(err.Error(), `failed to decode "json"`, "must mention failing to decode JSON in error message")
assert.Equal(tchannel.ErrCodeBadRequest, code, "must produce bad request error")
},
},
// TODO test invalid headers
{
name: "missing procedure",
procedure: "",
body: []byte{},
headers: []byte("{}"),
validate: func(res3 []byte, isAppErr bool, err error) {
assert.Error(err, "is error")
assert.False(isAppErr, "missing procedure must not produce an application error")
err, ok := err.(tchannel.SystemError)
assert.True(ok, "missing procedure must produce system error")
if !ok {
return
}
code := tchannel.GetSystemErrorCode(err)
assert.Equal(tchannel.ErrCodeBadRequest, code, "missing procedure must produce bad request error")
assert.Contains(err.Error(), "missing procedure", "must mention missing procedure in error message")
},
},
{
name: "invalid procedure",
procedure: "no-such-procedure",
body: []byte{},
headers: []byte("{}"),
validate: func(res3 []byte, isAppErr bool, err error) {
assert.Error(err, "is error")
assert.False(isAppErr, "no-such-procedure must not produce application error")
err, ok := err.(tchannel.SystemError)
assert.True(ok, "no-such-procedure must produce system error")
if !ok {
return
}
code := tchannel.GetSystemErrorCode(err)
assert.Equal(tchannel.ErrCodeBadRequest, code, "must produce bad request error: %v", err)
assert.Contains(err.Error(), `unrecognized procedure "no-such-procedure"`, "must mention unrecongized procedure in error message")
},
},
{
name: "bad response",
procedure: "bad-response",
body: []byte("{}"),
headers: []byte("{}"),
validate: func(res3 []byte, isAppErr bool, err error) {
assert.Error(err, "is error")
assert.False(isAppErr, "bad-response must not produce an application error")
err, ok := err.(tchannel.SystemError)
assert.True(ok, "bad-response must produce system error")
if !ok {
return
}
code := tchannel.GetSystemErrorCode(err)
assert.Equal(tchannel.ErrCodeBadRequest, code, "bad-response must produce unexpected error")
assert.Contains(err.Error(), `failed to encode "json"`, "must mention failure to encode JSON in error message")
},
},
{
name: "unexpected error",
procedure: "unexpected-error",
body: []byte("{}"),
headers: []byte("{}"),
validate: func(res3 []byte, isAppErr bool, err error) {
assert.NoError(err, "not error")
assert.True(isAppErr, "unexpected-error procedure must produce application error")
},
},
}
server := t.Param(params.Server)
serverHostPort := fmt.Sprintf("%v:%v", server, serverPort)
ch, err := tchannel.NewChannel(serviceName, nil)
fatals.NoError(err, "could not create channel")
peer := ch.Peers().Add(serverHostPort)
for _, tt := range tests {
var res2, res3 []byte
var headers map[string]string
t.Tag("case", tt.name)
t.Tag("procedure", tt.procedure)
ctx, cancel := json.NewContext(time.Second)
defer cancel()
ctx = json.WithHeaders(ctx, headers)
encoding := "json"
if tt.encoding != "" {
encoding = tt.encoding
}
call, err := peer.BeginCall(
ctx,
serviceName,
tt.procedure,
&tchannel.CallOptions{Format: tchannel.Format(encoding)},
)
fatals.NoError(err, "could not begin call")
err = tchannel.NewArgWriter(call.Arg2Writer()).Write(tt.headers)
fatals.NoError(err, "could not write request headers")
err = tchannel.NewArgWriter(call.Arg3Writer()).Write(tt.body)
fatals.NoError(err, "could not write request body")
err = tchannel.NewArgReader(call.Response().Arg2Reader()).Read(&res2)
isAppErr := call.Response().ApplicationError()
if err == nil {
err = tchannel.NewArgReader(call.Response().Arg3Reader()).Read(&res3)
}
tt.validate(res3, isAppErr, err)
}
}