mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-26 18:43:43 +03:00
Validate that api scheme is used correctly
This commit is contained in:
parent
190b5e79f6
commit
e8f090604c
|
@ -42,17 +42,20 @@ export class SpecManager {
|
||||||
|
|
||||||
/* calculate common used values */
|
/* calculate common used values */
|
||||||
init() {
|
init() {
|
||||||
|
let urlParts = this._url ? urlParse(this._url) : {};
|
||||||
|
let schemes = this._schema.schemes;
|
||||||
let protocol;
|
let protocol;
|
||||||
let urlParts = urlParse(this._url);
|
if (!schemes || !schemes.length) {
|
||||||
if (!this._schema.schemes || !this._schema.schemes.length) {
|
// url parser incudles ':' in protocol so remove it
|
||||||
protocol = this._url ? urlParts.protocol : 'http';
|
protocol = urlParts.protocol ? urlParts.protocol.slice(0, -1) : 'http';
|
||||||
} else {
|
} else {
|
||||||
protocol = this._schema.schemes[0];
|
protocol = schemes[0];
|
||||||
if (protocol === 'http' && this._schema.schemes.indexOf('https') >= 0) {
|
if (protocol === 'http' && schemes.indexOf('https') >= 0) {
|
||||||
protocol = 'https';
|
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;
|
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);
|
||||||
|
|
|
@ -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 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', () => {
|
it('should substitute api host when spec host is undefined', () => {
|
||||||
specMgr._schema.host = undefined;
|
specMgr._schema.host = undefined;
|
||||||
specMgr._url = 'https://petstore.swagger.io/v2';
|
specMgr._url = 'https://petstore.swagger.io/v2';
|
||||||
|
|
Loading…
Reference in New Issue
Block a user