Skip to content

Commit

Permalink
fix: fix regression double slashes added to full URL display
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanHotsiy committed May 13, 2019
1 parent 672ef56 commit f29a4fe
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
6 changes: 6 additions & 0 deletions e2e/e2e.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<script src="../bundles/redoc.standalone.js">{}</script>
<div id="redoc" />
</body>
</html>;
6 changes: 6 additions & 0 deletions e2e/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<script src="../bundles/redoc.standalone.js">{}</script>
<div id="redoc" />
</body>
</html>;
64 changes: 64 additions & 0 deletions e2e/integration/misc.e2e.ts
Original file line number Diff line number Diff line change
@@ -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',
);
});
});
});
4 changes: 2 additions & 2 deletions src/components/Endpoint/Endpoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>
Expand Down
15 changes: 11 additions & 4 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
},
];
}
Expand Down

0 comments on commit f29a4fe

Please sign in to comment.