Skip to content

Commit d13ab44

Browse files
authored
Replace unmaintained mock-express-response with node-mocks-http (#13789)
* Replace unmaintained mock-express-response with node-mocks-http
1 parent 3dad66b commit d13ab44

File tree

7 files changed

+82
-181
lines changed

7 files changed

+82
-181
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,8 @@
297297
"jest-json-schema": "^6.1.0",
298298
"jest-watch-typeahead": "^2.2.2",
299299
"mini-css-extract-plugin": "^2.9.4",
300-
"mock-express-request": "^0.2.0",
301-
"mock-express-response": "^0.3.0",
302300
"node-fetch": "^3.3.2",
301+
"node-mocks-http": "^1.17.2",
303302
"nodemon": "^3.1.10",
304303
"pino-devtools": "^2.8.0",
305304
"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);

0 commit comments

Comments
 (0)