Skip to content

Commit 28fd85b

Browse files
authored
Merge branch 'next' into jq-er
2 parents e5c682d + 265f561 commit 28fd85b

8 files changed

Lines changed: 32 additions & 35 deletions

File tree

test/nginx/mock-http-server/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ app.use('/-/', (req, res, next) => {
2525
next();
2626
});
2727

28-
app.get('/health', (req, res) => res.send('OK'));
29-
app.get('/request-log', (req, res) => res.json(requests));
30-
app.get('/reset', (req, res) => {
28+
app.get('/__mock_http_server/health', (req, res) => res.send('OK'));
29+
app.get('/__mock_http_server/request-log', (req, res) => res.json(requests));
30+
app.get('/__mock_http_server/reset', (req, res) => {
3131
requests.length = 0;
3232
res.json('OK');
3333
});

test/nginx/mock-sentry/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ app.use(express.json({
2727
'application/reports+json', // https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Security-Policy/report-to#violation_report_syntax
2828
],
2929
}));
30-
app.get('/event-log', (req, res) => res.json(events));
31-
app.get('/reset', (req, res) => {
30+
app.get('/__mock_sentry/event-log', (req, res) => res.json(events));
31+
app.get('/__mock_sentry/reset', (req, res) => {
3232
events.length = 0;
3333
res.json('OK');
3434
});

test/nginx/setup-tests.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ log "Starting test services..."
3131
docker_compose up --build --detach
3232

3333
log "Waiting for mock backend..."
34-
wait_for_http_response 5 localhost:8383/health 200
34+
wait_for_http_response 5 localhost:8383/__mock_http_server/health 200
3535
log "Waiting for mock enketo..."
36-
wait_for_http_response 5 localhost:8005/health 200
36+
wait_for_http_response 5 localhost:8005/__mock_http_server/health 200
3737
log "Waiting for nginx..."
3838
wait_for_http_response 5 localhost:9000 421
3939

test/nginx/src/lib.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
};
1414

1515
async function assertSentryReceived(...expectedRequests) {
16-
const { status, body } = await requestSentryMock({ path:'/event-log' });
16+
const { status, body } = await requestSentryMock({ path:'/__mock_sentry/event-log' });
1717
assert.equal(status, 200);
1818

1919
const actual = JSON.parse(body);
@@ -27,7 +27,7 @@ async function assertSentryReceived(...expectedRequests) {
2727
}
2828

2929
async function resetSentryMock() {
30-
const res = await requestSentryMock({ path:'/reset' });
30+
const res = await requestSentryMock({ path:'/__mock_sentry/reset' });
3131
assert.equal(res.status, 200);
3232
}
3333

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const http = require('node:http');
2+
const https = require('node:https');
3+
4+
const log = (...args) => console.log('[mocha-setup]', ...args);
5+
6+
module.exports = {
7+
mochaHooks: {
8+
afterAll() {
9+
log('Cleaning up HTTP(S) Response objects whose bodies have not been read...');
10+
http.globalAgent.destroy();
11+
https.globalAgent.destroy();
12+
13+
log('Cleanup complete.');
14+
},
15+
},
16+
};

test/nginx/src/mocha/nginx.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ function assertBackendReceived(...expectedRequests) {
11621162
}
11631163

11641164
async function assertMockHttpReceived(port, expectedRequests) {
1165-
const res = await request(`http://localhost:${port}/request-log`);
1165+
const res = await request(`http://localhost:${port}/__mock_http_server/request-log`);
11661166
assert.isTrue(res.ok);
11671167
assert.deepEqual(expectedRequests, await res.json());
11681168
}
@@ -1176,7 +1176,7 @@ function resetBackendMock() {
11761176
}
11771177

11781178
async function resetMock(port) {
1179-
const res = await request(`http://localhost:${port}/reset`);
1179+
const res = await request(`http://localhost:${port}/__mock_http_server/reset`);
11801180
assert.isTrue(res.ok);
11811181
}
11821182

test/nginx/src/mocha/request.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const { isIPv6 } = require('node:net');
2-
const { Readable } = require('node:stream');
32

43
module.exports = request;
54

@@ -16,29 +15,11 @@ function request(url, { body, ...options }={}) {
1615
const req = getProtocolImplFrom(url).request({ ...options, ...preserve(url) }, res => {
1716
res.on('error', reject);
1817

19-
const body = new Readable({ read:() => {} });
20-
res.on('error', err => body.destroy(err));
21-
res.on('data', data => body.push(data));
22-
res.on('end', () => body.push(null));
23-
24-
const text = () => new Promise((resolve, reject) => {
25-
const chunks = [];
26-
body.on('error', reject);
27-
body.on('data', data => chunks.push(data));
28-
body.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
29-
});
30-
31-
const status = res.statusCode;
32-
33-
resolve({
34-
status,
35-
ok: status >= 200 && status < 300,
36-
statusText: res.statusText,
37-
body,
38-
text,
39-
json: async () => JSON.parse(await text()),
18+
resolve(new Response(res, {
19+
status: res.statusCode,
20+
statusText: res.statusMessage,
4021
headers: new Headers(res.headers),
41-
});
22+
}));
4223
});
4324
req.on('error', reject);
4425
if(body !== undefined) req.write(body);

test/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"lint": "ESLINT_USE_FLAT_CONFIG=false npx eslint --cache .",
55
"test:github-actions": "mocha ./github-actions.spec.js",
66
"test:nginx": "npm run test:nginx:mocha && npm run test:nginx:playwright",
7-
"test:nginx:mocha": "NODE_TLS_REJECT_UNAUTHORIZED=0 mocha ./nginx/src/mocha",
7+
"test:nginx:mocha": "NODE_TLS_REJECT_UNAUTHORIZED=0 mocha './nginx/src/mocha/**/*.spec.js' --require ./nginx/src/mocha/mocha.setup.js",
88
"test:nginx:playwright": "NODE_TLS_REJECT_UNAUTHORIZED=0 playwright test",
99
"test:service": "mocha ./service.spec.js",
1010
"test": "npm run lint && npm run test:github-actions && npm run test:nginx && npm run test:service"

0 commit comments

Comments
 (0)