Use api host if schema host is undefined

This commit is contained in:
Mike Stead 2016-08-01 09:46:34 +10:00
parent bc47b40466
commit 51965f94b5
2 changed files with 11 additions and 2 deletions

View File

@ -43,15 +43,17 @@ export class SpecManager {
/* calculate common used values */ /* calculate common used values */
init() { init() {
let protocol; let protocol;
const urlParts = urlParse(this._url);
if (!this._schema.schemes || !this._schema.schemes.length) { if (!this._schema.schemes || !this._schema.schemes.length) {
protocol = this._url ? urlParse(this._url).protocol : 'http'; protocol = this._url ? urlParts.protocol : 'http';
} else { } else {
protocol = this._schema.schemes[0]; protocol = this._schema.schemes[0];
if (protocol === 'http' && this._schema.schemes.indexOf('https') >= 0) { if (protocol === 'http' && this._schema.schemes.indexOf('https') >= 0) {
protocol = 'https'; protocol = 'https';
} }
} }
this.apiUrl = protocol + '://' + this._schema.host + this._schema.basePath; let host = (!this._schema.host && urlParts.host) ? urlParts.host : this._schema.host;
this.apiUrl = protocol + '://' + host + this._schema.basePath;
if (this.apiUrl.endsWith('/')) { if (this.apiUrl.endsWith('/')) {
this.apiUrl = this.apiUrl.substr(0, this.apiUrl.length - 1); 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'); 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';
specMgr.init();
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
});
describe('byPointer method', () => { describe('byPointer method', () => {
it('should return correct schema part', ()=> { it('should return correct schema part', ()=> {
let part = specMgr.byPointer('/tags/3'); let part = specMgr.byPointer('/tags/3');