forked from swagger-api/swagger-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswagger-request-spec.js
411 lines (409 loc) · 14 KB
/
swagger-request-spec.js
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
// Generated by CoffeeScript 1.6.3
(function() {
window.api_key = 'special-key';
describe('SwaggerRequest for version 1.2 spec', function() {
beforeEach(function() {
var success;
success = function() {
return console.log("success");
};
window.authorizations.add("key", new ApiKeyAuthorization("api_key", "special-key", "header"));
window.swagger = new SwaggerApi({
url: 'http://petstore.swagger.wordnik.com/api/api-docs',
success: success
});
return waitsFor(function() {
return swagger.ready != null;
});
});
describe("execute get operations", function() {
beforeEach(function() {
window.body = null;
window.response = null;
window.callback = null;
window.error = null;
window.operation = swagger.pet.operations.getPetById;
window.success_callback = function(data) {
return window.response = data;
};
return window.error_callback = function(data) {
return window.error = data;
};
});
it("fetches an object with json", function() {
var opts, params;
params = {};
opts = {
requestContentType: null,
responseContentType: "application/json"
};
new SwaggerRequest("GET", "http://petstore.swagger.wordnik.com/api/pet/1", params, opts, window.success_callback, window.error_callback, operation);
return window.args = {
petId: '1'
};
});
waitsFor(function() {
return window.response != null;
});
runs(function() {
var data, pet;
data = window.response.content.data;
pet = JSON.parse(data);
expect(pet).toBeDefined;
expect(pet.id).toBe(1);
return expect(window.error).toBe(null);
});
it("fetches an object with xml", function() {
var opts, params;
params = {};
opts = {
requestContentType: "application/xml",
responseContentType: null
};
new SwaggerRequest("GET", "http://petstore.swagger.wordnik.com/api/pet/1", params, opts, success_callback, error_callback, operation);
return window.args = {
petId: '1'
};
});
waitsFor(function() {
return window.response != null;
});
runs(function() {
var data, parser, pet;
parser = new DOMParser();
data = window.response.content.data;
pet = parser.parseFromString(data, "text/xml");
return expect(pet).toBeDefined;
});
it("fetches an object with plain text", function() {
var opts, params;
params = {};
opts = {
requestContentType: "text/plain",
responseContentType: "text/plain"
};
new SwaggerRequest("GET", "http://petstore.swagger.wordnik.com/api/pet/1", params, opts, success_callback, error_callback, operation);
return window.args = {
petId: '1'
};
});
waitsFor(function() {
return window.response != null;
});
runs(function() {
var pet;
pet = window.response.content.data;
return expect(pet).toBe("Pet(id=1, category=Category(id=2, name=Cats), name=Cat 1, photoUrls=[url1, url2], tags=[Tag(id=1, name=tag1), Tag(id=2, name=tag2)], status=available)");
});
it("fetches an object as html", function() {
var opts, params;
params = {};
opts = {
requestContentType: null,
responseContentType: "text/html"
};
new SwaggerRequest("GET", "http://petstore.swagger.wordnik.com/api/pet/1", params, opts, success_callback, error_callback, operation);
return window.args = {
petId: '1'
};
});
waitsFor(function() {
return window.response != null;
});
runs(function() {
var pet;
pet = window.response.content.data;
return console.log(pet);
});
it("handles redirects", function() {
var opts, params;
params = {};
opts = {
requestContentType: null,
responseContentType: "application/json"
};
new SwaggerRequest("GET", "http://petstore.swagger.wordnik.com/api/pet.redirect/3", params, opts, success_callback, error_callback, operation);
return window.args = {
petId: '1'
};
});
waitsFor(function() {
return window.response != null;
});
return runs(function() {
return expect(window.response.status).toBe(200);
});
});
describe("execute post operations", function() {
beforeEach(function() {
window.body = null;
window.response = null;
window.callback = function() {
return 'mock callback';
};
window.error = function() {
return 'mock error';
};
return window.operation = swagger.pet.operations.getPetById;
});
it("adds an object with json", function() {
var opts, params;
params = {
body: JSON.stringify({
id: 100,
name: "monster",
status: "dead"
})
};
opts = {
requestContentType: "application/json",
responseContentType: "application/json"
};
return new SwaggerRequest("POST", "http://petstore.swagger.wordnik.com/api/pet", params, opts, success_callback, error_callback, operation);
});
waitsFor(function() {
return window.response != null;
});
runs(function() {
var data, resp;
data = window.response.content.data;
resp = JSON.parse(data);
return expect(resp.code).toBe(200);
});
return it("adds an object with xml", function() {
var opts, params;
params = {
headers: {},
body: "<Pet><id>101</id><category><id>2</id><name>Cats</name></category><name>Cat 1</name><photoUrls>url1</photoUrls><photoUrls>url2</photoUrls><tags><id>1</id><name>tag1</name></tags><tags><id>2</id><name>tag2</name></tags><status>available</status></Pet>"
};
opts = {
requestContentType: "application/xml",
responseContentType: "application/xml"
};
new SwaggerRequest("POST", "http://petstore.swagger.wordnik.com/api/pet", params, opts, success_callback, error_callback, operation);
waitsFor(function() {
return window.response != null;
});
return runs(function() {
var resp;
return resp = window.response;
});
});
});
describe("execute put operations", function() {
beforeEach(function() {
window.body = null;
window.response = null;
window.callback = function() {
return 'mock callback';
};
window.error = function() {
return 'mock error';
};
return window.operation = swagger.pet.operations.getPetById;
});
it("updates an object with json", function() {
var opts, params;
params = {
body: JSON.stringify({
id: 1,
name: "monster",
status: "alive"
})
};
opts = {
requestContentType: "application/json",
responseContentType: "application/json"
};
return new SwaggerRequest("PUT", "http://petstore.swagger.wordnik.com/api/pet", params, opts, success_callback, error_callback, operation);
});
waitsFor(function() {
return window.response != null;
});
runs(function() {
var data, resp;
data = window.response.content.data;
resp = JSON.parse(data);
return expect(resp.code).toBe(200);
});
it("updates an object with xml", function() {
var opts, params;
params = {
headers: {},
body: "<Pet><id>1</id><category><id>2</id><name>Cats</name></category><name>Cat 1</name><photoUrls>url1</photoUrls><photoUrls>url2</photoUrls><tags><id>1</id><name>tag1</name></tags><tags><id>2</id><name>tag2</name></tags><status>available</status></Pet>"
};
opts = {
requestContentType: "application/xml",
responseContentType: "application/xml"
};
return new SwaggerRequest("PUT", "http://petstore.swagger.wordnik.com/api/pet", params, opts, success_callback, error_callback, operation);
});
waitsFor(function() {
return window.response != null;
});
return runs(function() {
var resp;
return resp = window.response;
});
});
describe("execute delete operations", function() {
beforeEach(function() {
window.body = null;
window.response = null;
window.success_callback = function(data) {
return window.response = data;
};
window.error_callback = function(data) {
return window.error = data;
};
return window.operation = swagger.pet.operations.getPetById;
});
it("deletes an object", function() {
var params;
window.success_callback = function(data) {
return window.response = "successfully deleted pet";
};
params = {};
return new SwaggerRequest("DELETE", "http://petstore.swagger.wordnik.com/api/pet/100", params, {}, success_callback, error_callback, operation);
});
waitsFor(function() {
return window.response != null;
});
return runs(function() {
var resp;
resp = window.response;
return expect(resp).toBe("successfully deleted pet");
});
});
describe("execute options call", function() {
beforeEach(function() {
window.body = null;
window.response = null;
window.success_callback = function(data) {
return window.response = data;
};
window.error_callback = function(data) {
return window.error = data;
};
return window.operation = swagger.pet.operations.getPetById;
});
it("gets options on the pet resource", function() {
var params;
window.success_callback = function(data) {
return window.response = "successfully fetched options";
};
params = {};
return new SwaggerRequest("OPTIONS", "http://petstore.swagger.wordnik.com/api/pet", params, {}, success_callback, error_callback, operation);
});
waitsFor(function() {
return window.response != null;
});
return runs(function() {
var resp;
resp = window.response;
return expect(resp).toBe("successfully fetched options");
});
});
describe("execute patch call", function() {
beforeEach(function() {
window.body = null;
window.response = null;
window.success_callback = function(data) {
return window.response = data;
};
window.error_callback = function(data) {
return window.error = data;
};
return window.operation = swagger.pet.operations.getPetById;
});
return it("merges an object", function() {
var opts, params;
window.success_callback = function(data) {
return window.response = "successfully patched pet";
};
params = {
body: JSON.stringify({
name: "ghoul"
})
};
opts = {
requestContentType: "application/json",
responseContentType: "application/json"
};
new SwaggerRequest("PATCH", "http://petstore.swagger.wordnik.com/api/pet/3", params, opts, success_callback, error_callback, operation);
waitsFor(function() {
return window.response != null;
});
return runs(function() {
var resp;
resp = window.response;
return expect(resp).toBe("successfully patched pet");
});
});
});
return describe("api key authorizations", function() {
beforeEach(function() {
window.body = null;
window.response = null;
window.callback = null;
window.error = null;
window.success_callback = function(data) {
return window.response = data;
};
return window.error_callback = function(data) {
return window.error = data;
};
});
it("applies an api key to the query string", function() {
var auth, opts, params;
params = {};
opts = {
requestContentType: null,
responseContentType: "application/json"
};
auth = new ApiKeyAuthorization("api_key", "abc123", "query");
window.authorizations.add("key", auth);
new SwaggerRequest("GET", "http://petstore.swagger.wordnik.com/api/pet/1", params, opts, window.success_callback, window.error_callback, operation);
return window.args = {
petId: '1'
};
});
waitsFor(function() {
return window.response != null;
});
runs(function() {
var data, pet;
data = window.response.content.data;
pet = JSON.parse(data);
expect(pet).toBeDefined;
expect(pet.id).toBe(1);
return expect(window.error).toBe(null);
});
it("applies an api key as a header", function() {
var auth, opts, params;
params = {};
opts = {
requestContentType: null,
responseContentType: "application/json"
};
auth = new ApiKeyAuthorization("api_key", "abc123", "header");
window.authorizations.add("key", auth);
new SwaggerRequest("GET", "http://petstore.swagger.wordnik.com/api/pet/1", params, opts, window.success_callback, window.error_callback, operation);
return window.args = {
petId: '1'
};
});
waitsFor(function() {
return window.response != null;
});
return runs(function() {
var data, pet;
data = window.response.content.data;
pet = JSON.parse(data);
expect(pet).toBeDefined;
expect(pet.id).toBe(1);
return expect(window.error).toBe(null);
});
});
});
}).call(this);