-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_test.go
194 lines (178 loc) · 5.39 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
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
package openapi_test
import (
"os"
"path/filepath"
"testing"
"github.com/MarkRosemaker/openapi"
"github.com/go-api-libs/types"
)
func TestDocument_JSON(t *testing.T) {
t.Parallel()
// security optional via an empty security requirement
testJSON(t, []byte(`{
"openapi": "3.1.0",
"info": {
"title": "Sample Pet Store App",
"version": "1.0.0"
},
"paths": {"/": {}},
"security": [{}]
}`), &openapi.Document{})
}
func TestDocument_Validate(t *testing.T) {
t.Parallel()
doc := &openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{
Title: "Sample Pet Store App",
Summary: "A pet store manager.",
Description: "This is a sample server for a pet store.",
TermsOfService: mustParseURL("https://example.com/terms/"),
Contact: &openapi.Contact{
Name: "API Support",
URL: mustParseURL("https://www.example.com/support"),
Email: types.Email("[email protected]"),
},
License: &openapi.License{
Name: "Apache 2.0",
URL: mustParseURL("https://www.apache.org/licenses/LICENSE-2.0.html"),
},
Version: "1.0.1",
},
Paths: openapi.Paths{"/": {}},
}
if err := doc.Validate(); err != nil {
t.Fatal(err)
}
}
func TestDocument_Examples(t *testing.T) {
t.Parallel()
if err := filepath.Walk("examples", func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || filepath.Ext(path) == ".txt" {
return err
}
t.Run(path, func(t *testing.T) {
original, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
switch filepath.Ext(path) {
case ".json":
testJSON(t, original, &openapi.Document{})
case ".yaml":
doc, err := openapi.LoadFromData(original)
if err != nil {
t.Fatal(err)
}
if err := doc.Validate(); err != nil {
t.Fatal(err)
}
}
})
return nil
}); err != nil {
t.Fatal(err)
}
}
func TestDocumentValidate_Error(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
doc *openapi.Document
err string
}{
{&openapi.Document{}, "openapi is required"},
{&openapi.Document{
OpenAPI: "foo",
}, `openapi ("foo") is invalid: must be a valid version (3.0.x or 3.1.x)`},
{&openapi.Document{
OpenAPI: "3.1.0",
}, `info is required`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{},
}, `info.title is required`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
JSONSchemaDialect: mustParseURL("https://example.com"),
}, `jsonSchemaDialect ("https://example.com") is invalid, must be one of: "https://spec.openapis.org/oas/3.1/dialect/base"`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Servers: openapi.Servers{{}},
}, `servers[0].url is required`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Paths: openapi.Paths{"": {}},
}, `paths[""]: path must not be empty`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Webhooks: openapi.Webhooks{"myWebhook": {
Value: &openapi.PathItem{
Parameters: openapi.ParameterList{
{Value: &openapi.Parameter{Name: "foo"}},
},
},
}},
}, `webhooks["myWebhook"].parameters[0].in is required`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Paths: openapi.Paths{},
Webhooks: openapi.Webhooks{},
Components: openapi.Components{
Schemas: openapi.Schemas{},
},
}, openapi.ErrEmptyDocument.Error()},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Components: openapi.Components{
Schemas: openapi.Schemas{"Pet": &openapi.Schema{}},
},
}, `components.schemas["Pet"].type is required`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Paths: openapi.Paths{"/": {}},
Security: openapi.SecurityRequirements{{"": {}}},
}, `security[0][""]: empty security scheme name`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Paths: openapi.Paths{"/": {}},
Tags: openapi.Tags{{}},
}, `tags[0].name is required`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Paths: openapi.Paths{"/": {}},
Tags: openapi.Tags{{Name: "foo"}, {Name: "foo"}},
}, `tags[0].name ("foo") is invalid: must be unique
tags[1].name ("foo") is invalid: must be unique`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Paths: openapi.Paths{"/": {}},
ExternalDocs: &openapi.ExternalDocs{},
}, `externalDocs.url is required`},
{&openapi.Document{
OpenAPI: "3.1.0",
Info: &openapi.Info{Title: "Sample API", Version: "1.0.0"},
Components: openapi.Components{
Callbacks: openapi.CallbackRefs{" ": &openapi.CallbackRef{}},
},
}, `components.callbacks[" "] (" ") is invalid: must match the regular expression "^[a-zA-Z0-9\\.\\-_]+$"`},
} {
t.Run(tc.err, func(t *testing.T) {
t.Parallel()
if err := tc.doc.Validate(); err == nil {
t.Fatal("expected error")
} else if err.Error() != tc.err {
t.Fatalf("got: %v, want: %v", err, tc.err)
}
})
}
}