Skip to content

Commit

Permalink
test: Add user defined router middleware tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fladrif committed Aug 27, 2021
1 parent 8127895 commit bc4a05f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
25 changes: 25 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -96,6 +96,7 @@
"@types/node": "^14.0.24",
"@types/react": "^16.14.11",
"@types/react-dom": "^16.9.14",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"babel-plugin-module-resolver": "^4.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/server/api.test.ts
Expand Up @@ -1530,7 +1530,7 @@ describe('.configureRouter', () => {
const app = createApiServer({ auth, games, db, origins: false });

test('does not allow CORS', async () => {
const { res } = await request(app.callback())
const res = await request(app.callback())
.get('/games')
.set('Origin', 'https://www.example.com')
.expect('Vary', 'Origin');
Expand All @@ -1544,7 +1544,7 @@ describe('.configureRouter', () => {
const app = createApiServer({ auth, games, db, origins: origin });

test('disallows non-matching origin', async () => {
const { res } = await request(app.callback())
const res = await request(app.callback())
.get('/games')
.set('Origin', 'https://www.other.com')
.expect('Vary', 'Origin');
Expand All @@ -1567,7 +1567,7 @@ describe('.configureRouter', () => {
const app = createApiServer({ auth, games, db, origins });

test('disallows non-matching origin', async () => {
const { res } = await request(app.callback())
const res = await request(app.callback())
.get('/games')
.set('Origin', 'https://www.other.com')
.expect('Vary', 'Origin');
Expand Down
36 changes: 19 additions & 17 deletions src/server/index.test.ts
Expand Up @@ -6,6 +6,8 @@
* https://opensource.org/licenses/MIT.
*/

import request from 'supertest';

import { Server, createServerRunConfig, getPortFromServer } from '.';
import type { KoaServer } from '.';
import type { SocketIO } from './transport/socketio';
Expand Down Expand Up @@ -46,23 +48,6 @@ jest.mock('koa-socket-2', () => {
};
});

jest.mock('koa', () => {
return class {
constructor() {
(this as any).context = {};
(this as any).use = () => this;
(this as any).callback = () => {};
(this as any).listen = (port, listeningCallback?: () => void) => {
if (listeningCallback) listeningCallback();
return {
address: () => ({ port: 'mock-api-port' }),
close: () => {},
};
};
}
};
});

describe('new', () => {
test('custom db implementation', () => {
const game = {};
Expand Down Expand Up @@ -150,6 +135,23 @@ describe('run', () => {
});
expect(apiCallback).toHaveBeenCalled();
});

test('runs route middleware', async () => {
const usedMiddleware = jest.fn(async (_ctx, next) => {
await next;
});
const unusedMiddleware = jest.fn(async (_ctx, next) => {
await next;
});
server = Server({ games: [game] });
server.router.use('/games', usedMiddleware);
server.router.use('/games/unused', unusedMiddleware);
runningServer = await server.run(8888);

await request(runningServer.appServer).get('/games');
expect(usedMiddleware).toHaveBeenCalled();
expect(unusedMiddleware).not.toHaveBeenCalled();
});
});

describe('kill', () => {
Expand Down

0 comments on commit bc4a05f

Please sign in to comment.