Validate that api scheme is used correctly

This commit is contained in:
Mike Stead 2016-08-01 10:26:10 +10:00
parent 190b5e79f6
commit e8f090604c
2 changed files with 16 additions and 6 deletions

View File

@ -42,17 +42,20 @@ export class SpecManager {
/* calculate common used values */
init() {
let urlParts = this._url ? urlParse(this._url) : {};
let schemes = this._schema.schemes;
let protocol;
let urlParts = urlParse(this._url);
if (!this._schema.schemes || !this._schema.schemes.length) {
protocol = this._url ? urlParts.protocol : 'http';
if (!schemes || !schemes.length) {
// url parser incudles ':' in protocol so remove it
protocol = urlParts.protocol ? urlParts.protocol.slice(0, -1) : 'http';
} else {
protocol = this._schema.schemes[0];
if (protocol === 'http' && this._schema.schemes.indexOf('https') >= 0) {
protocol = schemes[0];
if (protocol === 'http' && schemes.indexOf('https') >= 0) {
protocol = 'https';
}
}
let host = (!this._schema.host && urlParts.host) ? urlParts.host : this._schema.host;
let host = this._schema.host || urlParts.host;
this.apiUrl = protocol + '://' + host + this._schema.basePath;
if (this.apiUrl.endsWith('/')) {
this.apiUrl = this.apiUrl.substr(0, this.apiUrl.length - 1);

View File

@ -59,6 +59,13 @@ describe('Utils', () => {
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
});
it('should substitute api scheme when spec schemes are undefined', () => {
specMgr._schema.schemes = undefined;
specMgr._url = 'https://petstore.swagger.io/v2';
specMgr.init();
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
});
it('should substitute api host when spec host is undefined', () => {
specMgr._schema.host = undefined;
specMgr._url = 'https://petstore.swagger.io/v2';