Skip to content

Commit c48fea3

Browse files
authored
Merge pull request #871 from swagger-api/issue-870
Fix scheme selection
2 parents 058b2ac + f1644b7 commit c48fea3

File tree

6 files changed

+63
-12
lines changed

6 files changed

+63
-12
lines changed

browser/swagger-client.js

Lines changed: 14 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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,21 @@ SwaggerClient.prototype.buildFromSpec = function (response) {
325325
} else if (typeof this.scheme === 'undefined') {
326326
if(typeof window !== 'undefined') {
327327
var scheme = window.location.protocol.replace(':','');
328-
if(this.schemes.indexOf(scheme) !== -1) {
328+
if(scheme === 'https' && this.schemes.indexOf(scheme) === -1) {
329+
// can't call http from https served page in a browser!
330+
helpers.log('Cannot call a http server from https inside a browser!');
331+
this.scheme = 'http';
332+
}
333+
else if(this.schemes.indexOf(scheme) !== -1) {
329334
this.scheme = scheme;
330335
}
331336
else {
332-
this.scheme = 'http';
337+
if(this.schemes.indexOf('https') !== -1) {
338+
this.scheme = 'https';
339+
}
340+
else {
341+
this.scheme = 'http';
342+
}
333343
}
334344
}
335345
else {

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.21",
11+
"version": "2.2.21",
1212
"homepage": "http://swagger.io",
1313
"repository": {
1414
"type": "git",

test/client.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,7 +1409,6 @@ describe('SwaggerClient', function () {
14091409
});
14101410
});
14111411

1412-
14131412
it('should read a blob', function(done) {
14141413
var spec = {
14151414
paths: {
@@ -1440,7 +1439,6 @@ describe('SwaggerClient', function () {
14401439
}).then(function(client) {
14411440
client.test.getBlob({})
14421441
.then(function (response) {
1443-
console.log('horray');
14441442
var filename = './file.tmp';
14451443
fs.writeFile(filename, response.data, function(err) {
14461444
if(err) {
@@ -1459,4 +1457,37 @@ describe('SwaggerClient', function () {
14591457
done(exception);
14601458
});
14611459
});
1460+
1461+
1462+
it('should honor schemes', function(done) {
1463+
var spec = {
1464+
schemes: ['https'],
1465+
paths: {
1466+
'/v2/nada': {
1467+
get: {
1468+
operationId: 'getNothing',
1469+
tags: [ 'test' ],
1470+
parameters: [],
1471+
responses: {
1472+
default: {
1473+
description: 'ok'
1474+
}
1475+
}
1476+
}
1477+
}
1478+
}
1479+
};
1480+
1481+
var output = new SwaggerClient({
1482+
url: 'http://localhost:8000',
1483+
spec: spec,
1484+
usePromise: true
1485+
}).then(function(client) {
1486+
var mock = client.test.getNothing({},{mock: true});
1487+
expect(mock.url).toEqual('https://localhost:8000/v2/nada');
1488+
done();
1489+
}).catch(function(exception) {
1490+
done(exception);
1491+
});
1492+
});
14621493
});

test/spec/v2/spec1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"host": "localhost:8000",
2121
"basePath": "/v2/api",
2222
"schemes": [
23-
"http"
23+
"https"
2424
],
2525
"tags": [
2626
{

0 commit comments

Comments
 (0)