Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest): correctly handle basePath set via basePath() API #3266

Merged
merged 1 commit into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ describe('RequestContext', () => {

expect(observedCtx.basePath).to.equal('/api/v1');
});

it('honors basePath from server config', async () => {
await givenRunningAppWithClient({basePath: '/api'});
await client.get('/api/products').expect(200);
expect(observedCtx.basePath).to.equal('/api');
});

it('honors basePath set via basePath() method', async () => {
await givenRunningAppWithClient({}, a => {
a.restServer.basePath('/api');
});
await client.get('/api/products').expect(200);
expect(observedCtx.basePath).to.equal('/api');
});
});

describe('requestedBaseUrl', () => {
Expand Down Expand Up @@ -142,12 +156,16 @@ async function teardown() {
if (app) await app.stop();
}

async function givenRunningAppWithClient(restOptions?: RestServerConfig) {
async function givenRunningAppWithClient(
restOptions?: RestServerConfig,
setupFn: (app: RestApplication) => void = () => {},
) {
const options: ApplicationConfig = {
rest: givenHttpServerConfig(restOptions),
};
app = new RestApplication(options);
app.handler(contextObservingHandler);
setupFn(app);
await app.start();
client = createRestAppClient(app);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,14 @@ paths:
expect(response.body.servers).to.containEql({url: '/api'});
});

it('controls server urls even when set via server.basePath() API', async () => {
server.basePath('/v2');
const response = await createClientForHandler(server.requestHandler).get(
'/openapi.json',
);
expect(response.body.servers).to.containEql({url: '/v2'});
});

it('controls redirect locations', async () => {
server.controller(DummyController);
server.redirect('/page/html', '/html');
Expand Down
7 changes: 6 additions & 1 deletion packages/rest/src/request-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export class RequestContext extends Context implements HandlerContext {
const request = this.request;
let basePath = this.serverConfig.basePath || '';
if (request.baseUrl && request.baseUrl !== '/') {
basePath = request.baseUrl + basePath;
if (!basePath || request.baseUrl.endsWith(basePath)) {
// Express has already applied basePath to baseUrl
basePath = request.baseUrl;
} else {
basePath = request.baseUrl + basePath;
}
}
return basePath;
}
Expand Down
1 change: 1 addition & 0 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ export class RestServer extends Context implements Server, HttpServerLike {
path = path.replace(/(^\/)|(\/$)/, '');
if (path) path = '/' + path;
this._basePath = path;
this.config.basePath = path;
}

/**
Expand Down