Skip to content

Commit b2c9f41

Browse files
authored
Merge pull request #930 from swagger-api/issue-929
type checks, tests for #929
2 parents 52518c0 + 378113c commit b2c9f41

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

browser/swagger-client.js

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

browser/swagger-client.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ SwaggerClient.prototype.build = function (mock) {
211211
},
212212
on: {
213213
error: function (response) {
214-
if (self.url.substring(0, 4) !== 'http') {
214+
if (self && self.url && self.url.substring(0, 4) !== 'http') {
215215
return self.fail('Please specify the protocol for ' + self.url);
216216
} else if (response.errObj && (response.errObj.code === 'ECONNABORTED' || response.errObj.message.indexOf('timeout') !== -1)) {
217217
return self.fail('Request timed out after ' + self.fetchSpecTimeout + 'ms');
@@ -259,7 +259,7 @@ SwaggerClient.prototype.build = function (mock) {
259259
obj.timeout = this.fetchSpecTimeout;
260260
}
261261

262-
if (this.spec) {
262+
if (this.spec && typeof this.spec === 'object') {
263263
self.swaggerObject = this.spec;
264264
setTimeout(function () {
265265
new Resolver().resolve(self.spec, self.url, self.buildFromSpec, self);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
],
1010
"description": "swagger-client is a javascript client for use with swaggering APIs.",
11-
"version": "2.1.29",
11+
"version": "2.1.30",
1212
"homepage": "http://swagger.io",
1313
"repository": {
1414
"type": "git",

test/durability.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* global after, before, describe, it */
2+
3+
'use strict';
4+
5+
var expect = require('expect');
6+
var test = require('unit.js');
7+
var SwaggerClient = require('..');
8+
9+
var petstoreRaw = require('./spec/v2/petstore.json');
10+
11+
12+
describe('swagger resolver', function () {
13+
it('fails gracefully with bad spec', function(done) {
14+
new SwaggerClient({
15+
spec: 'bad',
16+
usePromise: true
17+
}).then(function(client) {
18+
done('should have failed');
19+
}).catch(function(err) {
20+
done();
21+
})
22+
});
23+
24+
it('fails gracefully with bad URL', function(done) {
25+
new SwaggerClient({
26+
url: 'http://fake.fake/swagger.json',
27+
usePromise: true
28+
}).then(function(client) {
29+
done('should have failed');
30+
}).catch(function(err) {
31+
done();
32+
})
33+
});
34+
35+
it('fails gracefully with operation', function(done) {
36+
new SwaggerClient({
37+
spec: petstoreRaw,
38+
usePromise: true
39+
}).then(function(client) {
40+
client.pet.getPetById({petId: 3})
41+
.then(function(data) {
42+
console.log('ok')
43+
console.log(data);
44+
done('should have failed');
45+
})
46+
.catch(function(error) {
47+
console.log('error')
48+
console.log(error);
49+
done();
50+
})
51+
}).catch(function(err) {
52+
done('should have failed');
53+
})
54+
});
55+
});

0 commit comments

Comments
 (0)