fix: fix regression double slashes added to full URL display

This commit is contained in:
Roman Hotsiy 2019-05-13 22:15:53 +03:00
parent 672ef56507
commit f29a4fe2ee
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 89 additions and 6 deletions

6
e2e/e2e.html Normal file
View File

@ -0,0 +1,6 @@
<html>
<body>
<script src="../bundles/redoc.standalone.js">{}</script>
<div id="redoc" />
</body>
</html>;

6
e2e/index.html Normal file
View File

@ -0,0 +1,6 @@
<html>
<body>
<script src="../bundles/redoc.standalone.js">{}</script>
<div id="redoc" />
</body>
</html>;

View File

@ -0,0 +1,64 @@
// tslint:disable:no-implicit-dependencies
import * as yaml from 'yaml-js';
async function loadSpec(url: string): Promise<any> {
const spec = await (await fetch(url)).text();
return yaml.load(spec);
}
function initReDoc(win, spec, options = {}) {
(win as any).Redoc.init(spec, options, win.document.getElementById('redoc'));
}
describe('Servers', () => {
beforeEach(() => {
cy.visit('e2e/');
});
it('should have valid server', () => {
cy.window().then(async win => {
const spec = await loadSpec('/demo/openapi.yaml');
initReDoc(win, spec, {});
// TODO add cy-data attributes
cy.get('[data-section-id="operation/addPet"]').should(
'contain',
'http://petstore.swagger.io/v2/pet',
);
cy.get('[data-section-id="operation/addPet"]').should(
'contain',
'http://petstore.swagger.io/sandbox/pet',
);
});
});
it('should have valid server for when servers not provided', () => {
cy.window().then(async win => {
const spec = await loadSpec('/demo/openapi.yaml');
delete spec.servers;
initReDoc(win, spec, {});
// TODO add cy-data attributes
cy.get('[data-section-id="operation/addPet"]').should(
'contain',
'http://localhost:' + win.location.port + '/e2e/pet',
);
});
});
it('should have valid server for when servers not provided at .html pages', () => {
cy.visit('e2e/e2e.html');
cy.window().then(async win => {
const spec = await loadSpec('/demo/openapi.yaml');
delete spec.servers;
initReDoc(win, spec, {});
// TODO add cy-data attributes
cy.get('[data-section-id="operation/addPet"]').should(
'contain',
'http://localhost:' + win.location.port + '/e2e/pet',
);
});
});
});

View File

@ -5,7 +5,7 @@ import { Markdown } from '../Markdown/Markdown';
import { OptionsContext } from '../OptionsProvider';
import { SelectOnClick } from '../SelectOnClick/SelectOnClick';
import {getBasePath, removeQueryString} from '../../utils';
import { getBasePath } from '../../utils';
import {
EndpointInfo,
HttpVerb,
@ -68,7 +68,7 @@ export class Endpoint extends React.Component<EndpointProps, EndpointState> {
<span>
{hideHostname || options.hideHostname
? getBasePath(server.url)
: removeQueryString(server.url)}
: server.url}
</span>
{operation.path}
</ServerUrl>

View File

@ -11,7 +11,7 @@ import {
Referenced,
} from '../types';
import { IS_BROWSER } from './dom';
import { isNumeric, resolveUrl } from './helpers';
import { isNumeric, removeQueryString, resolveUrl, stripTrailingSlash } from './helpers';
function isWildcardStatusCode(statusCode: string | number): statusCode is string {
return typeof statusCode === 'string' && /\dxx/i.test(statusCode);
@ -367,13 +367,20 @@ export function normalizeServers(
specUrl: string | undefined,
servers: OpenAPIServer[],
): OpenAPIServer[] {
const baseUrl =
specUrl === undefined ? (IS_BROWSER ? window.location.href : '') : dirname(specUrl);
const getHref = () => {
if (!IS_BROWSER) {
return '';
}
const href = window.location.href;
return href.endsWith('.html') ? dirname(href) : href;
};
const baseUrl = specUrl === undefined ? removeQueryString(getHref()) : dirname(specUrl);
if (servers.length === 0) {
return [
{
url: baseUrl,
url: stripTrailingSlash(baseUrl),
},
];
}