diff --git a/src/test-modules/express.js b/src/test-modules/express.js index 285fcf9..8c65784 100644 --- a/src/test-modules/express.js +++ b/src/test-modules/express.js @@ -16,9 +16,13 @@ export const expectMiddleware = (middleware, req, expectResponse = false) => new let response = {status: 200}; const res = {}; + res.cookie = (name, value, options) => (response.cookies = [...(response.cookies || []), {name, value, options}], res); + res.clearCookie = (name, options) => (response.clearCookies = [...(response.clearCookies || []), {name, options}], res); res.status = (code) => (response.status = code, res); res.json = (body) => (response.body = body, resolveResponse(response)); - res.send = () => resolveResponse(response); + res.redirect = (statusOrPath, path) => (Object.assign(response, path ? {status: statusOrPath, redirect: path} : {status: 302, redirect: statusOrPath}), resolveResponse(response)); + res.send = res.json; + res.end = () => resolveResponse(response); middleware(req, res, next); }); diff --git a/src/test-modules/express.test.js b/src/test-modules/express.test.js index 142e986..04f6bc3 100644 --- a/src/test-modules/express.test.js +++ b/src/test-modules/express.test.js @@ -28,6 +28,83 @@ describe('expectMiddleware', () => { message: 'Unexpected call to next()', }); }); + + test('should record cookies set on response', async () => { + const middleware = (req, res) => res.cookie('cookie1', 'value1', {secure: true}) + .cookie('cookie2', 'value2', {httpOnly: true}) + .send(); + const res = await expectMiddleware(middleware, {}, true); + expect(res).toEqual({ + status: 200, + cookies: [ + { + name: 'cookie1', + value: 'value1', + options: {secure: true}, + }, + { + name: 'cookie2', + value: 'value2', + options: {httpOnly: true}, + }, + ], + }); + }); + + test('should record cookies cleared on response', async () => { + const middleware = (req, res) => res.clearCookie('cookie1') + .clearCookie('cookie2', {httpOnly: true}) + .send(); + const res = await expectMiddleware(middleware, {}, true); + expect(res).toEqual({ + status: 200, + clearCookies: [ + { + name: 'cookie1', + options: undefined, + }, + { + name: 'cookie2', + options: {httpOnly: true}, + }, + ], + }); + }); + + test('should resolve with redirection and default status', async () => { + const middleware = (req, res) => res.redirect('/foo/bar'); + const res = await expectMiddleware(middleware, {}, true); + expect(res).toEqual({ + status: 302, + redirect: '/foo/bar', + }); + }); + + test('should resolve with redirection and custom status', async () => { + const middleware = (req, res) => res.redirect(301, '/foo/bar'); + const res = await expectMiddleware(middleware, {}, true); + expect(res).toEqual({ + status: 301, + redirect: '/foo/bar', + }); + }); + + test('should resolve with send', async () => { + const middleware = (req, res) => res.send('some body'); + const res = await expectMiddleware(middleware, {}, true); + expect(res).toEqual({ + status: 200, + body: 'some body', + }); + }); + + test('should resolve with send', async () => { + const middleware = (req, res) => res.end(); + const res = await expectMiddleware(middleware, {}, true); + expect(res).toEqual({ + status: 200, + }); + }); }); describe('givenMiddleware', () => {