Skip to content

Commit

Permalink
Expose serverBasePath fixes elastic#45991
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolf committed Sep 18, 2019
1 parent 265fcd7 commit 4d0ff48
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/core/server/http/base_path_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import { KibanaRequest } from './router';
import { httpServerMock } from './http_server.mocks';

describe('BasePath', () => {
describe('serverBasePath', () => {
it('defaults to an empty string', () => {
let basePath = new BasePath();
expect(basePath.serverBasePath).toBe('');

basePath = new BasePath('/server');
expect(basePath.serverBasePath).toBe('/server');
});
});

describe('#get()', () => {
it('returns base path associated with an incoming Legacy.Request request', () => {
const request = httpServerMock.createRawRequest();
Expand Down
12 changes: 7 additions & 5 deletions src/core/server/http/base_path_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import { modifyUrl } from '../../utils';

export class BasePath {
private readonly basePathCache = new WeakMap<LegacyRequest, string>();
public readonly serverBasePath: string;

constructor(private readonly serverBasePath?: string) {}
constructor(serverBasePath?: string) {
this.serverBasePath = serverBasePath || '';
}

public get = (request: KibanaRequest | LegacyRequest) => {
const requestScopePath = this.basePathCache.get(ensureRawRequest(request)) || '';
const serverBasePath = this.serverBasePath || '';
return `${serverBasePath}${requestScopePath}`;
return `${this.serverBasePath}${requestScopePath}`;
};

// should work only for KibanaRequest as soon as spaces migrate to NP
Expand All @@ -44,7 +46,7 @@ export class BasePath {
};

public prepend = (path: string): string => {
if (!this.serverBasePath) return path;
if (this.serverBasePath === '') return path;
return modifyUrl(path, parts => {
if (!parts.hostname && parts.pathname && parts.pathname.startsWith('/')) {
parts.pathname = `${this.serverBasePath}${parts.pathname}`;
Expand All @@ -53,7 +55,7 @@ export class BasePath {
};

public remove = (path: string): string => {
if (!this.serverBasePath) {
if (this.serverBasePath === '') {
return path;
}

Expand Down

0 comments on commit 4d0ff48

Please sign in to comment.