Skip to content

Fix: Remove trailing slash from route path in KoaDriver #1114

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

Merged
merged 7 commits into from
Apr 19, 2023
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
8 changes: 7 additions & 1 deletion src/driver/koa/KoaDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ export class KoaDriver extends BaseDriver {
const afterMiddlewares = this.prepareMiddlewares(uses.filter(use => use.afterAction));

// prepare route and route handler function
const route = ActionMetadata.appendBaseRoute(this.routePrefix, actionMetadata.fullRoute);
let route = ActionMetadata.appendBaseRoute(this.routePrefix, actionMetadata.fullRoute);

// @koa/router is strict about trailing slashes, allow accessing routes without them
if (typeof route === 'string' && route.length > 1 && route.endsWith('/')) {
route = route.substring(0, route.length - 1);
}

const routeHandler = (context: any, next: () => Promise<any>) => {
const options: Action = { request: context.request, response: context.response, context, next };
return executeCallback(options);
Expand Down
47 changes: 47 additions & 0 deletions test/functional/koa-trailing-slash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Server as HttpServer } from 'http';
import HttpStatusCodes from 'http-status-codes';
import { Controller } from '../../src/decorator/Controller';
import { Get } from '../../src/decorator/Get';
import { createKoaServer, getMetadataArgsStorage } from '../../src/index';
import { axios } from '../utilities/axios';
import DoneCallback = jest.DoneCallback;

describe(``, () => {
let koaServer: HttpServer;

describe('koa trailing slashes', () => {
beforeEach((done: DoneCallback) => {
getMetadataArgsStorage().reset();

@Controller('/posts')
class PostController {
@Get('/')
getAll(): string {
return '<html><body>All posts</body></html>';
}
}

koaServer = createKoaServer().listen(3001, done);
});

afterEach((done: DoneCallback) => {
koaServer.close(done);
});

it('get should respond to request without a traling slash', async () => {
expect.assertions(3);
const response = await axios.get('/posts');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>All posts</body></html>');
});

it('get should respond to request with a traling slash', async () => {
expect.assertions(3);
const response = await axios.get('/posts/');
expect(response.status).toEqual(HttpStatusCodes.OK);
expect(response.headers['content-type']).toEqual('text/html; charset=utf-8');
expect(response.data).toEqual('<html><body>All posts</body></html>');
});
});
});