1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package loads
16
17 import (
18 "net/http"
19 "net/http/httptest"
20 "testing"
21
22 "github.com/stretchr/testify/require"
23 )
24
25 func TestLoadJSON(t *testing.T) {
26 serv := httptest.NewServer(http.HandlerFunc(jsonPestoreServer))
27 defer serv.Close()
28
29 s, err := JSONSpec(serv.URL)
30 require.NoError(t, err)
31 require.NotNil(t, s)
32
33 ts2 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
34 rw.WriteHeader(http.StatusNotFound)
35 _, _ = rw.Write([]byte("{}"))
36 }))
37 defer ts2.Close()
38 _, err = JSONSpec(ts2.URL)
39 require.Error(t, err)
40 }
41
42 var jsonPestoreServer = func(rw http.ResponseWriter, _ *http.Request) {
43 rw.WriteHeader(http.StatusOK)
44 _, _ = rw.Write([]byte(petstoreJSON))
45 }
46
47 const petstoreJSON = `{
48 "swagger": "2.0",
49 "info": {
50 "version": "1.0.0",
51 "title": "Swagger Petstore",
52 "contact": {
53 "name": "Wordnik API Team",
54 "url": "http://developer.wordnik.com"
55 },
56 "license": {
57 "name": "Creative Commons 4.0 International",
58 "url": "http://creativecommons.org/licenses/by/4.0/"
59 }
60 },
61 "host": "petstore.swagger.wordnik.com",
62 "basePath": "/api",
63 "schemes": [
64 "http"
65 ],
66 "paths": {
67 "/pets": {
68 "get": {
69 "security": [
70 {
71 "oauth2": ["read"]
72 }
73 ],
74 "tags": [ "Pet Operations" ],
75 "operationId": "getAllPets",
76 "parameters": [
77 {
78 "name": "status",
79 "in": "query",
80 "description": "The status to filter by",
81 "type": "string"
82 }
83 ],
84 "summary": "Finds all pets in the system",
85 "responses": {
86 "200": {
87 "description": "Pet response",
88 "schema": {
89 "type": "array",
90 "items": {
91 "$ref": "#/definitions/Pet"
92 }
93 }
94 },
95 "default": {
96 "description": "Unexpected error",
97 "schema": {
98 "$ref": "#/definitions/Error"
99 }
100 }
101 }
102 },
103 "post": {
104 "security": [
105 {
106 "oauth2": ["write"]
107 }
108 ],
109 "tags": [ "Pet Operations" ],
110 "operationId": "createPet",
111 "summary": "Creates a new pet",
112 "parameters": [
113 {
114 "name": "pet",
115 "in": "body",
116 "description": "The Pet to create",
117 "required": true,
118 "schema": {
119 "$ref": "#/definitions/newPet"
120 }
121 }
122 ],
123 "responses": {
124 "200": {
125 "description": "Created Pet response",
126 "schema": {
127 "$ref": "#/definitions/Pet"
128 }
129 },
130 "default": {
131 "description": "Unexpected error",
132 "schema": {
133 "$ref": "#/definitions/Error"
134 }
135 }
136 }
137 }
138 },
139 "/pets/{id}": {
140 "delete": {
141 "security": [
142 {
143 "oauth2": ["write"]
144 }
145 ],
146 "description": "Deletes the Pet by id",
147 "operationId": "deletePet",
148 "parameters": [
149 {
150 "name": "id",
151 "in": "path",
152 "description": "ID of pet to delete",
153 "required": true,
154 "type": "integer",
155 "format": "int64"
156 }
157 ],
158 "responses": {
159 "204": {
160 "description": "pet deleted"
161 },
162 "default": {
163 "description": "unexpected error",
164 "schema": {
165 "$ref": "#/definitions/Error"
166 }
167 }
168 }
169 },
170 "get": {
171 "security": [
172 {
173 "oauth2": ["read"]
174 }
175 ],
176 "tags": [ "Pet Operations" ],
177 "operationId": "getPetById",
178 "summary": "Finds the pet by id",
179 "responses": {
180 "200": {
181 "description": "Pet response",
182 "schema": {
183 "$ref": "#/definitions/Pet"
184 }
185 },
186 "default": {
187 "description": "Unexpected error",
188 "schema": {
189 "$ref": "#/definitions/Error"
190 }
191 }
192 }
193 },
194 "parameters": [
195 {
196 "name": "id",
197 "in": "path",
198 "description": "ID of pet",
199 "required": true,
200 "type": "integer",
201 "format": "int64"
202 }
203 ]
204 }
205 },
206 "definitions": {
207 "Category": {
208 "id": "Category",
209 "properties": {
210 "id": {
211 "format": "int64",
212 "type": "integer"
213 },
214 "name": {
215 "type": "string"
216 }
217 }
218 },
219 "Pet": {
220 "id": "Pet",
221 "properties": {
222 "category": {
223 "$ref": "#/definitions/Category"
224 },
225 "id": {
226 "description": "unique identifier for the pet",
227 "format": "int64",
228 "maximum": 100.0,
229 "minimum": 0.0,
230 "type": "integer"
231 },
232 "name": {
233 "type": "string"
234 },
235 "photoUrls": {
236 "items": {
237 "type": "string"
238 },
239 "type": "array"
240 },
241 "status": {
242 "description": "pet status in the store",
243 "enum": [
244 "available",
245 "pending",
246 "sold"
247 ],
248 "type": "string"
249 },
250 "tags": {
251 "items": {
252 "$ref": "#/definitions/Tag"
253 },
254 "type": "array"
255 }
256 },
257 "required": [
258 "id",
259 "name"
260 ]
261 },
262 "newPet": {
263 "allOf": [
264 {
265 "$ref": "#/definitions/Pet"
266 }
267 ],
268 "required": [
269 "name"
270 ]
271 },
272 "Tag": {
273 "id": "Tag",
274 "properties": {
275 "id": {
276 "format": "int64",
277 "type": "integer"
278 },
279 "name": {
280 "type": "string"
281 }
282 }
283 },
284 "Error": {
285 "required": [
286 "code",
287 "message"
288 ],
289 "properties": {
290 "code": {
291 "type": "integer",
292 "format": "int32"
293 },
294 "message": {
295 "type": "string"
296 }
297 }
298 }
299 },
300 "produces": [
301 "application/json",
302 "application/xml",
303 "text/plain",
304 "text/html"
305 ],
306 "securityDefinitions": {
307 "oauth2": {
308 "type": "oauth2",
309 "scopes": {
310 "read": "Read access.",
311 "write": "Write access"
312 },
313 "flow": "accessCode",
314 "authorizationUrl": "http://petstore.swagger.wordnik.com/oauth/authorize",
315 "tokenUrl": "http://petstore.swagger.wordnik.com/oauth/token"
316 }
317 }
318 }`
319
View as plain text