-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbdd_test.go
412 lines (374 loc) · 11.5 KB
/
bdd_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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package errors
import (
"context"
stderrors "errors"
"fmt"
"net/http"
"os"
"testing"
"github.com/cucumber/godog"
"google.golang.org/grpc/codes"
)
type typeTestError struct {
t string
e error
}
func (e typeTestError) Error() string { return e.t }
func (e typeTestError) TypeCode() string { return e.t }
func (e typeTestError) Is(err error) bool { return stderrors.Is(e.e, err) }
func (e typeTestError) As(target interface{}) bool { return stderrors.As(e.e, target) }
type httpTestError struct {
hc int
e error
}
func (e httpTestError) Error() string { return fmt.Sprintf("HTTP(%d)", e.hc) }
func (e httpTestError) HTTPCode() int { return e.hc }
func (e httpTestError) Is(err error) bool { return stderrors.Is(e.e, err) }
func (e httpTestError) As(target interface{}) bool { return stderrors.As(e.e, target) }
type grpcTestError struct {
gc codes.Code
e error
}
func (e grpcTestError) Error() string { return fmt.Sprintf("GRPC(%d)", e.gc) }
func (e grpcTestError) GRPCCode() codes.Code { return e.gc }
func (e grpcTestError) Is(err error) bool { return stderrors.Is(e.e, err) }
func (e grpcTestError) As(target interface{}) bool { return stderrors.As(e.e, target) }
func convertGRPCStringToCode(grpcCode string) codes.Code {
switch grpcCode {
case "codes.OK":
return codes.OK
case "codes.Canceled":
return codes.Canceled
case "codes.Unknown":
return codes.Unknown
case "codes.InvalidArgument":
return codes.InvalidArgument
case "codes.DeadlineExceeded":
return codes.DeadlineExceeded
case "codes.NotFound":
return codes.NotFound
case "codes.AlreadyExists":
return codes.AlreadyExists
case "codes.PermissionDenied":
return codes.PermissionDenied
case "codes.ResourceExhausted":
return codes.ResourceExhausted
case "codes.FailedPrecondition":
return codes.FailedPrecondition
case "codes.Aborted":
return codes.Aborted
case "codes.OutOfRange":
return codes.OutOfRange
case "codes.Unimplemented":
return codes.Unimplemented
case "codes.Internal":
return codes.Internal
case "codes.Unavailable":
return codes.Unavailable
case "codes.DataLoss":
return codes.DataLoss
case "codes.Unauthenticated":
return codes.Unauthenticated
default:
return codes.Unknown
}
}
func convertHTTPStringToInt(httpStatus string) int {
switch httpStatus {
case "http.StatusOK":
return http.StatusOK
case "http.StatusBadRequest":
return http.StatusBadRequest
case "http.StatusUnauthorized":
return http.StatusUnauthorized
case "http.StatusPaymentRequired":
return http.StatusPaymentRequired
case "http.StatusForbidden":
return http.StatusForbidden
case "http.StatusNotFound":
return http.StatusNotFound
case "http.StatusMethodNotAllowed":
return http.StatusMethodNotAllowed
case "http.StatusNotAcceptable":
return http.StatusNotAcceptable
case "http.StatusProxyAuthRequired":
return http.StatusProxyAuthRequired
case "http.StatusRequestTimeout":
return http.StatusRequestTimeout
case "http.StatusConflict":
return http.StatusConflict
case "http.StatusGone":
return http.StatusGone
case "http.StatusLengthRequired":
return http.StatusLengthRequired
case "http.StatusPreconditionFailed":
return http.StatusPreconditionFailed
case "http.StatusRequestEntityTooLarge":
return http.StatusRequestEntityTooLarge
case "http.StatusRequestURITooLong":
return http.StatusRequestURITooLong
case "http.StatusUnsupportedMediaType":
return http.StatusUnsupportedMediaType
case "http.StatusRequestedRangeNotSatisfiable":
return http.StatusRequestedRangeNotSatisfiable
case "http.StatusExpectationFailed":
return http.StatusExpectationFailed
case "http.StatusTeapot":
return http.StatusTeapot
case "http.StatusMisdirectedRequest":
return http.StatusMisdirectedRequest
case "http.StatusUnprocessableEntity":
return http.StatusUnprocessableEntity
case "http.StatusLocked":
return http.StatusLocked
case "http.StatusFailedDependency":
return http.StatusFailedDependency
case "http.StatusTooEarly":
return http.StatusTooEarly
case "http.StatusUpgradeRequired":
return http.StatusUpgradeRequired
case "http.StatusPreconditionRequired":
return http.StatusPreconditionRequired
case "http.StatusTooManyRequests":
return http.StatusTooManyRequests
case "http.StatusRequestHeaderFieldsTooLarge":
return http.StatusRequestHeaderFieldsTooLarge
case "http.StatusUnavailableForLegalReasons":
return http.StatusUnavailableForLegalReasons
case "http.StatusInternalServerError":
return http.StatusInternalServerError
case "http.StatusNotImplemented":
return http.StatusNotImplemented
case "http.StatusBadGateway":
return http.StatusBadGateway
case "http.StatusServiceUnavailable":
return http.StatusServiceUnavailable
case "http.StatusGatewayTimeout":
return http.StatusGatewayTimeout
case "http.StatusHTTPVersionNotSupported":
return http.StatusHTTPVersionNotSupported
case "http.StatusVariantAlsoNegotiates":
return http.StatusVariantAlsoNegotiates
case "http.StatusInsufficientStorage":
return http.StatusInsufficientStorage
case "http.StatusLoopDetected":
return http.StatusLoopDetected
case "http.StatusNotExtended":
return http.StatusNotExtended
case "http.StatusNetworkAuthenticationRequired":
return http.StatusNetworkAuthenticationRequired
default:
return http.StatusNotExtended
}
}
func convertErrNameToError(errName string) Error {
switch errName {
case "ErrOK":
return ErrOK
case "ErrCanceled":
return ErrCanceled
case "ErrUnknown":
return ErrUnknown
case "ErrInvalidArgument":
return ErrInvalidArgument
case "ErrDeadlineExceeded":
return ErrDeadlineExceeded
case "ErrNotFound":
return ErrNotFound
case "ErrAlreadyExists":
return ErrAlreadyExists
case "ErrPermissionDenied":
return ErrPermissionDenied
case "ErrResourceExhausted":
return ErrResourceExhausted
case "ErrFailedPrecondition":
return ErrFailedPrecondition
case "ErrAborted":
return ErrAborted
case "ErrOutOfRange":
return ErrOutOfRange
case "ErrUnimplemented":
return ErrUnimplemented
case "ErrInternal":
return ErrInternal
case "ErrUnavailable":
return ErrUnavailable
case "ErrDataLoss":
return ErrDataLoss
case "ErrUnauthenticated":
return ErrUnauthenticated
case "ErrBadRequest":
return ErrBadRequest
case "ErrUnauthorized":
return ErrUnauthorized
case "ErrForbidden":
return ErrForbidden
case "ErrMethodNotAllowed":
return ErrMethodNotAllowed
case "ErrRequestTimeout":
return ErrRequestTimeout
case "ErrConflict":
return ErrConflict
case "ErrImATeapot":
return ErrImATeapot
case "ErrUnprocessableEntity":
return ErrUnprocessableEntity
case "ErrTooManyRequests":
return ErrTooManyRequests
case "ErrUnavailableForLegalReasons":
return ErrUnavailableForLegalReasons
case "ErrInternalServerError":
return ErrInternalServerError
case "ErrNotImplemented":
return ErrNotImplemented
case "ErrBadGateway":
return ErrBadGateway
case "ErrServiceUnavailable":
return ErrServiceUnavailable
case "ErrGatewayTimeout":
return ErrGatewayTimeout
default:
return ErrUnknown
}
}
func TestMain(m *testing.M) {
format := "progress"
for _, arg := range os.Args[1:] {
if arg == "-test.v=true" { // go test transforms -v option
format = "pretty"
break
}
}
opts := godog.Options{
Format: format,
Paths: []string{"features"},
NoColors: true,
}
status := godog.TestSuite{
Name: "errors",
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
Options: &opts,
}.Run()
// Optional: Run `testing` package's logic besides godog.
if st := m.Run(); st > status {
status = st
}
os.Exit(status)
}
// var expectedMessage string
var expectedError error
func theErrorDoesNotImplementCoderInterfaces() error {
expectedError = fmt.Errorf("%s", expectedError)
return nil
}
func theErrorIs(errName string) error {
err := convertErrNameToError(errName)
expectedError = err
return nil
}
func theErrorIsNil() error {
expectedError = nil
return nil
}
func theErrorSentByAGRPCServerWas(errName string) error {
err := convertErrNameToError(errName)
grpcErr := SendGRPCError(err)
grpcErr = ReceiveGRPCError(grpcErr)
expectedError = grpcErr
return nil
}
func anErrorWithTheMessage(message string) error {
expectedError = Wrap(expectedError, message)
return nil
}
func anErrorWithTypeCode(typeCode string) error {
expectedError = typeTestError{t: typeCode, e: expectedError}
return nil
}
func anErrorWithHTTPStatus(httpStatus string) error {
expectedError = httpTestError{hc: convertHTTPStringToInt(httpStatus), e: expectedError}
return nil
}
func anErrorWithGRPCCode(grpcCode string) error {
expectedError = grpcTestError{gc: convertGRPCStringToCode(grpcCode), e: expectedError}
return nil
}
func wrappedWithTheMessage(message string) error {
expectedError = Wrap(expectedError, message)
return nil
}
func wrappedWithTheError(errName, message string) error {
err := convertErrNameToError(errName)
expectedError = err.Wrap(expectedError, message)
return nil
}
func theErrorIsSentOverGRPC() error {
grpcErr := SendGRPCError(expectedError)
grpcErr = ReceiveGRPCError(grpcErr)
expectedError = grpcErr
return nil
}
func theErrorMessageIs(message string) error {
if expectedError.Error() != message {
return fmt.Errorf("expected message to be `%s` but got `%s`", message, expectedError.Error())
}
return nil
}
func theHTTPStatusIs(httpStatus string) error {
got := http.StatusText(HTTPCode(expectedError))
if got != httpStatus {
return fmt.Errorf("expected HTTP status to be `%s` but got `%s`", httpStatus, got)
}
return nil
}
func theTypeCodeIs(typeCode string) error {
got := TypeCode(expectedError)
if got != typeCode {
return fmt.Errorf("expected type code to be `%s` but got `%s`", typeCode, got)
}
return nil
}
func theGRPCCodeIs(grpcCode string) error {
got := GRPCCode(expectedError).String()
if got != grpcCode {
return fmt.Errorf("expected GRPC code to be `%s` but got `%s`", grpcCode, got)
}
return nil
}
func theErrorIsA(errName string) error {
if !Is(expectedError, convertErrNameToError(errName)) {
return fmt.Errorf("expected error to be a `%s`", errName)
}
return nil
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
expectedError = ErrUnknown
})
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
expectedError = ErrUnknown
return ctx, nil
})
// Given
ctx.Step(`^the error does not implement (:?Type|HTTP|GRPC)Coder{}$`, theErrorDoesNotImplementCoderInterfaces)
ctx.Step(`^the error is "([^"]*)"$`, theErrorIs)
ctx.Step(`^the error is nil$`, theErrorIsNil)
ctx.Step(`^the error sent by a GRPC server was "([^"]*)"$`, theErrorSentByAGRPCServerWas)
ctx.Step(`^an error with the message "([^"]*)"$`, anErrorWithTheMessage)
ctx.Step(`^an error with Type code "([^"]*)"$`, anErrorWithTypeCode)
ctx.Step(`^an error with HTTP status "([^"]*)"$`, anErrorWithHTTPStatus)
ctx.Step(`^an error with GRPC code "([^"]*)"$`, anErrorWithGRPCCode)
// When
ctx.Step(`^wrapped with the error "([^"]*)" and message "([^"]*)"$`, wrappedWithTheError)
ctx.Step(`^wrapped with the message "([^"]*)"$`, wrappedWithTheMessage)
ctx.Step(`^the error is sent over GRPC$`, theErrorIsSentOverGRPC)
// Then
ctx.Step(`^the Type code is "([^"]*)"$`, theTypeCodeIs)
ctx.Step(`^the HTTP status is "([^"]*)"$`, theHTTPStatusIs)
ctx.Step(`^the GRPC code is "([^"]*)"$`, theGRPCCodeIs)
ctx.Step(`^the error message is "([^"]*)"$`, theErrorMessageIs)
ctx.Step(`^the error is a "([^"]*)"$`, theErrorIsA)
}