Skip to content

Commit f1c40ae

Browse files
committed
Replace unmaintained mock-express-response with node-mocks-http
1 parent 8344224 commit f1c40ae

File tree

7 files changed

+71
-43
lines changed

7 files changed

+71
-43
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@
300300
"mock-express-request": "^0.2.0",
301301
"mock-express-response": "^0.3.0",
302302
"node-fetch": "^3.3.2",
303+
"node-mocks-http": "1.17.2",
303304
"nodemon": "^3.1.10",
304305
"pino-devtools": "^2.8.0",
305306
"pino-pretty": "^13.1.1",

tests/unit/amo/middleware/test_HstsMiddleware.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import MockExpressRequest from 'mock-express-request';
2-
import MockExpressResponse from 'mock-express-response';
1+
import httpMocks from 'node-mocks-http';
32

43
import { hsts } from 'amo/middleware';
54

65
describe(__filename, () => {
76
it('provides the expected HSTS headers', () => {
87
const middleware = hsts();
98
const nextSpy = sinon.stub();
10-
const req = new MockExpressRequest();
11-
const res = new MockExpressResponse();
9+
const req = httpMocks.createRequest();
10+
const res = httpMocks.createResponse();
1211

1312
middleware(req, res, nextSpy);
1413

tests/unit/amo/middleware/test_cspMiddleware.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import MockExpressRequest from 'mock-express-request';
2-
import MockExpressResponse from 'mock-express-response';
1+
import httpMocks from 'node-mocks-http';
32
import parse from 'content-security-policy-parser';
43

54
import { csp } from 'amo/middleware';
@@ -122,8 +121,8 @@ describe(__filename, () => {
122121
const config = require('config');
123122
const middleware = csp({ _config: config });
124123
const nextSpy = sinon.stub();
125-
const req = new MockExpressRequest();
126-
const res = new MockExpressResponse();
124+
const req = httpMocks.createRequest();
125+
const res = httpMocks.createResponse();
127126
middleware(req, res, nextSpy);
128127
const cspHeader = res.get('content-security-policy');
129128
const policy = parse(cspHeader);
@@ -140,8 +139,8 @@ describe(__filename, () => {
140139
const middleware = csp({ _config: getFakeConfig({ CSP: false }), _log });
141140

142141
const nextSpy = sinon.stub();
143-
const req = new MockExpressRequest();
144-
const res = new MockExpressResponse();
142+
const req = httpMocks.createRequest();
143+
const res = httpMocks.createResponse();
145144

146145
middleware(req, res, nextSpy);
147146

@@ -157,8 +156,8 @@ describe(__filename, () => {
157156
const middleware = csp({ _config: getFakeConfig({ CSP: false }), _log });
158157

159158
const nextSpy = sinon.stub();
160-
const req = new MockExpressRequest();
161-
const res = new MockExpressResponse();
159+
const req = httpMocks.createRequest();
160+
const res = httpMocks.createResponse();
162161

163162
middleware(req, res, nextSpy);
164163

tests/unit/amo/middleware/test_frameguardMiddleware.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import MockExpressRequest from 'mock-express-request';
2-
import MockExpressResponse from 'mock-express-response';
1+
import httpMocks from 'node-mocks-http';
32

43
import { frameguard } from 'amo/middleware';
54

@@ -17,8 +16,8 @@ describe(__filename, () => {
1716
const conf = require('config');
1817
const middleware = frameguard({ _config: conf });
1918
const nextSpy = sinon.stub();
20-
const req = new MockExpressRequest();
21-
const res = new MockExpressResponse();
19+
const req = httpMocks.createRequest();
20+
const res = httpMocks.createResponse();
2221
middleware(req, res, nextSpy);
2322
expect(res.get('x-frame-options')).toEqual('DENY');
2423
sinon.assert.calledOnce(nextSpy);

tests/unit/amo/middleware/test_requestId.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
/**
22
* @jest-environment node
33
*/
4-
import MockExpressRequest from 'mock-express-request';
5-
import MockExpressResponse from 'mock-express-response';
4+
import httpMocks from 'node-mocks-http';
65

76
import { AMO_REQUEST_ID_HEADER } from 'amo/constants';
87
import requestId from 'amo/middleware/requestId';
98

109
describe(__filename, () => {
1110
function _requestId({
12-
req = new MockExpressRequest(),
13-
res = new MockExpressResponse(),
11+
req = httpMocks.createRequest(),
12+
res = httpMocks.createResponse(),
1413
next = sinon.stub(),
1514
_httpContext = {
1615
set: sinon.stub(),
@@ -20,7 +19,7 @@ describe(__filename, () => {
2019
}
2120

2221
it('adds a generated request ID to the HTTP context and response', () => {
23-
const res = new MockExpressResponse();
22+
const res = httpMocks.createResponse();
2423
const next = sinon.stub();
2524
const _httpContext = {
2625
set: sinon.stub(),
@@ -38,8 +37,8 @@ describe(__filename, () => {
3837
});
3938

4039
it('uses the request ID from the request when available', () => {
41-
const req = new MockExpressRequest();
42-
const res = new MockExpressResponse();
40+
const req = httpMocks.createRequest();
41+
const res = httpMocks.createResponse();
4342
const _httpContext = {
4443
set: sinon.stub(),
4544
};
@@ -54,7 +53,7 @@ describe(__filename, () => {
5453
});
5554

5655
it('ensures the request has AMO_REQUEST_ID_HEADER', () => {
57-
const req = new MockExpressRequest();
56+
const req = httpMocks.createRequest();
5857
expect(req.get(AMO_REQUEST_ID_HEADER)).not.toBeDefined();
5958

6059
_requestId({ req });

tests/unit/amo/utils/test_server.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import path from 'path';
2+
import EventEmitter from 'events';
23

34
import fs from 'fs-extra';
4-
import MockExpressResponse from 'mock-express-response';
5+
import httpMocks from 'node-mocks-http';
56

67
import {
78
viewFrontendVersionHandler,
@@ -19,16 +20,16 @@ describe(__filename, () => {
1920
const _config = getFakeConfig({ basePath });
2021
const handler = viewFrontendVersionHandler({ _config });
2122

22-
const res = new MockExpressResponse();
23+
const res = httpMocks.createResponse({
24+
eventEmitter: EventEmitter,
25+
});
2326
handler(null, res);
2427

25-
res.on('finish', () => {
28+
res.on('end', () => {
2629
expect(res.statusCode).toEqual(200);
27-
expect(res.get('content-type')).toEqual(
28-
'application/json; charset=utf-8',
29-
);
30+
expect(res.get('content-type')).toEqual('application/json');
3031
expect(res.get('access-control-allow-origin')).toEqual('*');
31-
expect(res._getJSON()).toMatchObject(versionJson);
32+
expect(res._getJSONData()).toMatchObject(versionJson);
3233

3334
done();
3435
});
@@ -50,11 +51,13 @@ describe(__filename, () => {
5051
);
5152
const handler = viewFrontendVersionHandler({ _config });
5253

53-
const res = new MockExpressResponse();
54+
const res = httpMocks.createResponse({
55+
eventEmitter: EventEmitter,
56+
});
5457
handler(null, res);
5558

56-
res.on('finish', () => {
57-
expect(res._getJSON()).toMatchObject({
59+
res.on('end', () => {
60+
expect(res._getJSONData()).toMatchObject({
5861
...versionJson,
5962
experiments,
6063
feature_flags: {
@@ -73,10 +76,12 @@ describe(__filename, () => {
7376

7477
const handler = viewFrontendVersionHandler({ _config, _log });
7578

76-
const res = new MockExpressResponse();
79+
const res = httpMocks.createResponse({
80+
eventEmitter: EventEmitter,
81+
});
7782
handler(null, res);
7883

79-
res.on('finish', () => {
84+
res.on('end', () => {
8085
expect(res.statusCode).toEqual(415);
8186
sinon.assert.calledOnce(_log.error);
8287

@@ -94,7 +99,7 @@ describe(__filename, () => {
9499
const _fetch = jest.fn().mockResolvedValue({ status: 200 });
95100
const handler = viewHeartbeatHandler({ _config, _fetch });
96101

97-
const res = new MockExpressResponse();
102+
const res = httpMocks.createResponse();
98103
await handler(null, res);
99104

100105
expect(_fetch).toHaveBeenCalledWith(
@@ -107,7 +112,7 @@ describe(__filename, () => {
107112
const _fetch = jest.fn().mockResolvedValue({ status: 400 });
108113
const handler = viewHeartbeatHandler({ _fetch });
109114

110-
const res = new MockExpressResponse();
115+
const res = httpMocks.createResponse();
111116
await handler(null, res);
112117

113118
expect(res.statusCode).toEqual(500);
@@ -117,7 +122,7 @@ describe(__filename, () => {
117122
const _fetch = jest.fn().mockRejectedValue();
118123
const handler = viewHeartbeatHandler({ _fetch });
119124

120-
const res = new MockExpressResponse();
125+
const res = httpMocks.createResponse();
121126
await handler(null, res);
122127

123128
expect(res.statusCode).toEqual(500);

yarn.lock

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,7 @@ abort-controller@^3.0.0:
21822182
dependencies:
21832183
event-target-shim "^5.0.0"
21842184

2185-
accepts@^1.3.4:
2185+
accepts@^1.3.4, accepts@^1.3.7:
21862186
version "1.3.8"
21872187
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
21882188
integrity sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==
@@ -3309,7 +3309,7 @@ confusing-browser-globals@^1.0.10:
33093309
resolved "https://registry.yarnpkg.com/confusing-browser-globals/-/confusing-browser-globals-1.0.11.tgz#ae40e9b57cdd3915408a2805ebd3a5585608dc81"
33103310
integrity sha512-JsPKdmh8ZkmnHxDk55FZ1TqVLvEQTvoByJZRN9jzI0UjxK/QgAmsphz7PGtqgPieQZ/CQcHWXCR7ATDNhGe+YA==
33113311

3312-
content-disposition@^0.5.2:
3312+
content-disposition@^0.5.2, content-disposition@^0.5.3:
33133313
version "0.5.4"
33143314
resolved "https://registry.yarnpkg.com/content-disposition/-/content-disposition-0.5.4.tgz#8b82b4efac82512a02bb0b1dcec9d2c5e8eb5bfe"
33153315
integrity sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==
@@ -3818,7 +3818,7 @@ [email protected], depd@^2.0.0, depd@~2.0.0:
38183818
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
38193819
integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==
38203820

3821-
depd@^1.1.1, depd@~1.1.2:
3821+
depd@^1.1.0, depd@^1.1.1, depd@~1.1.2:
38223822
version "1.1.2"
38233823
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
38243824
integrity sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==
@@ -7438,6 +7438,11 @@ meow@^10.1.5:
74387438
type-fest "^1.2.2"
74397439
yargs-parser "^20.2.9"
74407440

7441+
merge-descriptors@^1.0.1:
7442+
version "1.0.3"
7443+
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.3.tgz#d80319a65f3c7935351e5cfdac8f9318504dbed5"
7444+
integrity sha512-gaNvAS7TZ897/rVaZ0nMtAyxNyi/pdbjbAwUpFQpN70GqnVfOiXpeUUMKRBmzXaSQ8DdTX4/0ms62r2K+hE6mQ==
7445+
74417446
merge-descriptors@^2.0.0:
74427447
version "2.0.0"
74437448
resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-2.0.0.tgz#ea922f660635a2249ee565e0449f951e6b603808"
@@ -7500,6 +7505,11 @@ [email protected]:
75007505
resolved "https://registry.yarnpkg.com/mime/-/mime-2.6.0.tgz#a2a682a95cd4d0cb1d6257e28f83da7e35800367"
75017506
integrity sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==
75027507

7508+
mime@^1.3.4:
7509+
version "1.6.0"
7510+
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
7511+
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
7512+
75037513
mimic-fn@^2.1.0:
75047514
version "2.1.0"
75057515
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
@@ -7714,6 +7724,22 @@ node-int64@^0.4.0:
77147724
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
77157725
integrity sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw==
77167726

7727+
7728+
version "1.17.2"
7729+
resolved "https://registry.yarnpkg.com/node-mocks-http/-/node-mocks-http-1.17.2.tgz#e947d9b94defb13e3775414a8200c848f6b2fc74"
7730+
integrity sha512-HVxSnjNzE9NzoWMx9T9z4MLqwMpLwVvA0oVZ+L+gXskYXEJ6tFn3Kx4LargoB6ie7ZlCLplv7QbWO6N+MysWGA==
7731+
dependencies:
7732+
accepts "^1.3.7"
7733+
content-disposition "^0.5.3"
7734+
depd "^1.1.0"
7735+
fresh "^0.5.2"
7736+
merge-descriptors "^1.0.1"
7737+
methods "^1.1.2"
7738+
mime "^1.3.4"
7739+
parseurl "^1.3.3"
7740+
range-parser "^1.2.0"
7741+
type-is "^1.6.18"
7742+
77177743
node-releases@^2.0.19:
77187744
version "2.0.19"
77197745
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.19.tgz#9e445a52950951ec4d177d843af370b411caf314"
@@ -10848,7 +10874,7 @@ type-fest@^3.0.0:
1084810874
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-3.4.0.tgz#64a35b7748ab4a96b3e0c324475ea66643c5f9df"
1084910875
integrity sha512-PEPg6RHlB9cFwoTMNENNrQFL0cXX04voWr2UPwQBJ3pVs7Mt8Y1oLWdUeMdGEwZE8HFFlujq8gS9enmyiQ8pLg==
1085010876

10851-
type-is@^1.6.15:
10877+
type-is@^1.6.15, type-is@^1.6.18:
1085210878
version "1.6.18"
1085310879
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
1085410880
integrity sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==

0 commit comments

Comments
 (0)