|
| 1 | +import { Server as HttpServer } from 'http'; |
| 2 | +import HttpStatusCodes from 'http-status-codes'; |
| 3 | +import { Controller } from '../../src/decorator/Controller'; |
| 4 | +import { Get } from '../../src/decorator/Get'; |
| 5 | +import { createKoaServer, getMetadataArgsStorage } from '../../src/index'; |
| 6 | +import { axios } from '../utilities/axios'; |
| 7 | +import DoneCallback = jest.DoneCallback; |
| 8 | + |
| 9 | +describe(``, () => { |
| 10 | + let koaServer: HttpServer; |
| 11 | + |
| 12 | + describe('koa trailing slashes', () => { |
| 13 | + beforeEach((done: DoneCallback) => { |
| 14 | + getMetadataArgsStorage().reset(); |
| 15 | + |
| 16 | + @Controller('/posts') |
| 17 | + class PostController { |
| 18 | + @Get('/') |
| 19 | + getAll(): string { |
| 20 | + return '<html><body>All posts</body></html>'; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + koaServer = createKoaServer().listen(3001, done); |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach((done: DoneCallback) => { |
| 28 | + koaServer.close(done); |
| 29 | + }); |
| 30 | + |
| 31 | + it('get should respond to request without a traling slash', async () => { |
| 32 | + expect.assertions(3); |
| 33 | + const response = await axios.get('/posts'); |
| 34 | + expect(response.status).toEqual(HttpStatusCodes.OK); |
| 35 | + expect(response.headers['content-type']).toEqual('text/html; charset=utf-8'); |
| 36 | + expect(response.data).toEqual('<html><body>All posts</body></html>'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('get should respond to request with a traling slash', async () => { |
| 40 | + expect.assertions(3); |
| 41 | + const response = await axios.get('/posts/'); |
| 42 | + expect(response.status).toEqual(HttpStatusCodes.OK); |
| 43 | + expect(response.headers['content-type']).toEqual('text/html; charset=utf-8'); |
| 44 | + expect(response.data).toEqual('<html><body>All posts</body></html>'); |
| 45 | + }); |
| 46 | + }); |
| 47 | +}); |
0 commit comments