-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdocument_test.go
165 lines (157 loc) · 4.72 KB
/
document_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
package openapi_test
import (
"fmt"
"net/http"
"reflect"
"strconv"
"testing"
openapi "github.com/nasa9084/go-openapi"
)
func TestDocument_Validate(t *testing.T) {
candidates := []candidate{
{"empty", openapi.Document{}, openapi.ErrRequired{Target: "openapi"}},
{"withInvalidVersion",
openapi.Document{
Version: "1.0",
Info: &openapi.Info{},
Paths: openapi.Paths{},
},
openapi.ErrFormatInvalid{Target: "openapi version", Format: "X.Y.Z"},
},
{"withVersion",
openapi.Document{
Version: "3.0.0",
},
openapi.ErrRequired{Target: "info"},
},
{"valid",
openapi.Document{
Version: "3.0.0",
Info: &openapi.Info{Title: "foo", TermsOfService: exampleCom, Version: "1.0"},
Paths: openapi.Paths{},
},
nil,
},
{"noPaths",
openapi.Document{
Version: "3.0.0",
Info: &openapi.Info{Title: "foo", TermsOfService: exampleCom, Version: "1.0"},
},
openapi.ErrRequired{Target: "paths"},
},
}
testValidater(t, candidates)
}
func TestOASVersion(t *testing.T) {
candidates := []struct {
label string
in string
err error
}{
{"empty", "", openapi.ErrRequired{Target: "openapi"}},
{"invalidVersion", "foobar", openapi.ErrFormatInvalid{Target: "openapi version", Format: "X.Y.Z"}},
{"swagger", "2.0", openapi.ErrFormatInvalid{Target: "openapi version", Format: "X.Y.Z"}},
{"valid", "3.0.0", nil},
{"unsupportedVersion", "4.0.0", openapi.ErrUnsupportedVersion},
{"invalidMajorVersion", "foo.0.0", openapi.ErrFormatInvalid{Target: "major part of openapi version"}},
{"invalidMinorVersion", "0.bar.0", openapi.ErrFormatInvalid{Target: "minor part of openapi version"}},
{"invalidPatchVersion", "0.0.baz", openapi.ErrFormatInvalid{Target: "patch part of openapi version"}},
}
for i, c := range candidates {
t.Run(strconv.Itoa(i)+"/"+c.label, func(t *testing.T) {
doc := openapi.Document{
Version: c.in,
Info: &openapi.Info{Title: "foo", Version: "1.0"},
Paths: openapi.Paths{},
}
if err := doc.Validate(); err != c.err {
if c.err != nil {
t.Error("error should be occurred, but not")
return
}
t.Errorf("error should not be occured: %s", err)
}
})
}
}
func TestDocument_Walk(t *testing.T) {
// doc is not valid openapi document but no problem in this test
rootGetOp := &openapi.Operation{OperationID: "rootGet"}
fooGetOp := &openapi.Operation{OperationID: "fooGet"}
fooPostOp := &openapi.Operation{OperationID: "fooPost"}
barGetOp := &openapi.Operation{OperationID: "barGet"}
barPostOp := &openapi.Operation{OperationID: "barPost"}
barPutOp := &openapi.Operation{OperationID: "barPut"}
barDeleteOp := &openapi.Operation{OperationID: "barDelete"}
rootPI := &openapi.PathItem{
Get: rootGetOp,
}
fooPI := &openapi.PathItem{
Get: fooGetOp,
Post: fooPostOp,
}
barPI := &openapi.PathItem{
Get: barGetOp,
Post: barPostOp,
Put: barPutOp,
Delete: barDeleteOp,
}
doc := &openapi.Document{
Version: "3.0.1",
Info: &openapi.Info{
Title: "title",
Version: "v0.0.0",
},
Paths: openapi.Paths{
"/": rootPI,
"/foo": fooPI,
"/bar": barPI,
},
}
getWalkFn := func() openapi.WalkFunc {
wants := []struct {
doc *openapi.Document
method string
path string
pathItem *openapi.PathItem
operation *openapi.Operation
}{
{doc, http.MethodGet, "/", rootPI, rootGetOp},
{doc, http.MethodDelete, "/bar", barPI, barDeleteOp},
{doc, http.MethodGet, "/bar", barPI, barGetOp},
{doc, http.MethodPost, "/bar", barPI, barPostOp},
{doc, http.MethodPut, "/bar", barPI, barPutOp},
{doc, http.MethodGet, "/foo", fooPI, fooGetOp},
{doc, http.MethodPost, "/foo", fooPI, fooPostOp},
}
var idx int
return func(doc *openapi.Document, method, path string, pathItem *openapi.PathItem, op *openapi.Operation) (err error) {
want := wants[idx]
defer func() {
if err != nil {
t.Logf("\ndoc: %+v\nmethod: %s\npath: %s\npathItem: %+v\nop: %+v\n", doc, method, path, pathItem, op)
}
}()
if !reflect.DeepEqual(doc, want.doc) {
return fmt.Errorf("unexpected document:\n got:\t%+v\n want:\t%+v", doc, want.doc)
}
if method != want.method {
return fmt.Errorf("unexpected method:\n got:\t%s\n want:\t%s", method, want.method)
}
if path != want.path {
return fmt.Errorf("unexpected path:\n got:\t%s\n want:\t%s", path, want.path)
}
if !reflect.DeepEqual(pathItem, want.pathItem) {
return fmt.Errorf("unexpected path item:\n got:\t%+v\n want:\t%+v", pathItem, want.pathItem)
}
if !reflect.DeepEqual(op, want.operation) {
return fmt.Errorf("unexpected operation:\n got:\t%+v\n want:\t%+v", op, want.operation)
}
idx++
return nil
}
}
if err := doc.Walk(getWalkFn()); err != nil {
t.Error(err)
}
}