From e8f090604ca794730ef211219b2df4b88e02a808 Mon Sep 17 00:00:00 2001 From: Mike Stead Date: Mon, 1 Aug 2016 10:26:10 +1000 Subject: [PATCH] Validate that api scheme is used correctly --- lib/utils/SpecManager.ts | 15 +++++++++------ tests/unit/SpecManager.spec.ts | 7 +++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/utils/SpecManager.ts b/lib/utils/SpecManager.ts index 02c5e92a..080e52e0 100644 --- a/lib/utils/SpecManager.ts +++ b/lib/utils/SpecManager.ts @@ -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); diff --git a/tests/unit/SpecManager.spec.ts b/tests/unit/SpecManager.spec.ts index 1a32af5e..8bdfedd6 100644 --- a/tests/unit/SpecManager.spec.ts +++ b/tests/unit/SpecManager.spec.ts @@ -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';