-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoperation_test.go
60 lines (56 loc) · 2.28 KB
/
operation_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
package openapi_test
import (
"reflect"
"strconv"
"testing"
openapi "github.com/nasa9084/go-openapi"
)
func TestOperation_Validate(t *testing.T) {
candidates := []candidate{
{"empty", openapi.Operation{}, openapi.ErrRequired{Target: "operation.responses"}},
{"duplicatedParameter", openapi.Operation{Responses: openapi.Responses{}, Parameters: []*openapi.Parameter{&openapi.Parameter{Name: "foo", In: "query"}, &openapi.Parameter{Name: "foo", In: "query"}}}, openapi.ErrParameterDuplicated},
{"valid", openapi.Operation{Responses: openapi.Responses{}}, nil},
}
testValidater(t, candidates)
}
func TestOperation_SuccessResponse(t *testing.T) {
candidates := []struct {
label string
in *openapi.Operation
resp *openapi.Response
status int
ok bool
}{
{"nil", nil, nil, -1, false},
{"empty", &openapi.Operation{}, nil, -1, false},
{"haveInvalid", &openapi.Operation{Responses: openapi.Responses{"foo": &openapi.Response{}}}, nil, 0, false},
{"haveNilResp", &openapi.Operation{Responses: openapi.Responses{"200": nil}}, nil, 0, false},
{"have100", &openapi.Operation{Responses: openapi.Responses{"100": &openapi.Response{}}}, nil, 0, false},
{"have200", &openapi.Operation{Responses: openapi.Responses{"200": &openapi.Response{}}}, &openapi.Response{}, 200, true},
{"haveDefault", &openapi.Operation{Responses: openapi.Responses{"default": &openapi.Response{}}}, &openapi.Response{}, 0, true},
{"have200andDefault", &openapi.Operation{Responses: openapi.Responses{"200": &openapi.Response{}, "default": &openapi.Response{}}}, &openapi.Response{}, 200, true},
{"have2XX", &openapi.Operation{Responses: openapi.Responses{"2XX": &openapi.Response{}}}, &openapi.Response{}, 0, true},
{"have1XX", &openapi.Operation{Responses: openapi.Responses{"1XX": &openapi.Response{}}}, nil, 0, false},
}
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
resp, status, ok := c.in.SuccessResponse()
if c.resp != nil && resp == nil {
t.Error("resp should not be nil")
return
}
if !reflect.DeepEqual(c.resp, resp) {
t.Errorf("%+v != %+v", c.resp, resp)
return
}
if status != c.status {
t.Errorf("%d != %d", status, c.status)
return
}
if ok != c.ok {
t.Errorf("%t != %t", ok, c.ok)
return
}
})
}
}