Merge pull request #74 from mikestead/fix/host-substitution

Use api host if schema host is undefined
This commit is contained in:
Roman Hotsiy 2016-08-01 06:58:05 +03:00 committed by GitHub
commit c24668938e
2 changed files with 24 additions and 5 deletions

View File

@ -42,16 +42,21 @@ export class SpecManager {
/* calculate common used values */
init() {
let urlParts = this._url ? urlParse(this._url) : {};
let schemes = this._schema.schemes;
let protocol;
if (!this._schema.schemes || !this._schema.schemes.length) {
protocol = this._url ? urlParse(this._url).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';
}
}
this.apiUrl = protocol + '://' + this._schema.host + this._schema.basePath;
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,20 @@ 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';
specMgr.init();
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
});
describe('byPointer method', () => {
it('should return correct schema part', ()=> {
let part = specMgr.byPointer('/tags/3');