Skip to content

Commit 096e448

Browse files
authored
Test server refactor (#1566)
* WIP. * More WIP. * WIP. * Fixed some URL silliness. * Integration test cleanup. * Formatting.
1 parent 885e4f6 commit 096e448

28 files changed

Lines changed: 376 additions & 275 deletions

File tree

gulp-tasks/test-integration.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const clearRequire = require('clear-require');
77

88
const constants = require('./utils/constants');
99
const logHelper = require('../infra/utils/log-helper');
10-
const testServer = require('../infra/testing/test-server');
10+
const server = require('../infra/testing/server/index');
1111

1212
const runFiles = (filePaths) => {
1313
// Mocha can't be run multiple times, which we need for NODE_ENV.
@@ -53,24 +53,19 @@ const runIntegrationTestSuite = async (testPath, nodeEnv, seleniumBrowser,
5353

5454
try {
5555
global.__workbox = {
56-
webdriver,
5756
seleniumBrowser,
58-
server: testServer,
57+
server,
58+
webdriver,
5959
};
6060

6161
const testFiles = glob.sync(path.posix.join(__dirname, '..', testPath,
6262
'*.js'));
63-
64-
testServer.reset();
6563
await runFiles(testFiles);
6664

6765
process.env.NODE_ENV = originalNodeEnv;
6866
} catch (err) {
6967
process.env.NODE_ENV = originalNodeEnv;
70-
71-
logHelper.error(err);
72-
throw new Error(
73-
`[Workbox Error Msg] 'gulp test-integration' discovered errors.`);
68+
throw new Error(`'gulp test-integration' discovered errors.`);
7469
}
7570
};
7671

@@ -115,7 +110,7 @@ gulp.task('test-integration', async () => {
115110
return;
116111
}
117112

118-
await testServer.start();
113+
await server.start();
119114

120115
try {
121116
const localBrowsers = seleniumAssistant.getLocalBrowsers();
@@ -138,9 +133,9 @@ gulp.task('test-integration', async () => {
138133
`);
139134
}
140135
}
141-
await testServer.stop();
136+
await server.stop();
142137
} catch (err) {
143-
await testServer.stop();
138+
await server.stop();
144139
throw err;
145140
}
146141

gulp-tasks/test-server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const gulp = require('gulp');
22

33
const constants = require('./utils/constants');
4-
const testServer = require('../infra/testing/test-server');
4+
const testServer = require('../infra/testing/server/index');
55

66
const handleExit = () => {
77
testServer.stop();
@@ -16,9 +16,9 @@ const startServer = () => {
1616
'SIGUSR2',
1717
'uncaughtException',
1818
];
19-
eventNames.forEach((eventName) => {
19+
for (const eventName of eventNames) {
2020
process.on(eventName, handleExit);
21-
});
21+
}
2222

2323
return testServer.start();
2424
};

infra/testing/comlink/sw-interface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
importScripts('/comlink.js');
1+
importScripts('/__WORKBOX/comlink.js');
22

33
// TODO: Standardize on naming, and move over some of the legacy uses of
44
// postMessage() to this new approach.

infra/testing/comlink/window-interface.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function initComlink() {
66
}
77

88
const scriptEl = document.createElement('script');
9-
scriptEl.src = '/comlink.js';
9+
scriptEl.src = '/__WORKBOX/comlink.js';
1010
scriptEl.addEventListener('load', () => {
1111
if (navigator.serviceWorker.controller) {
1212
initComlink();

infra/testing/server/index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const express = require('express');
2+
const path = require('path');
3+
const requireDir = require('require-dir');
4+
const serveIndex = require('serve-index');
5+
6+
const logHelper = require('../../utils/log-helper');
7+
const RequestCounter = require('./request-counter');
8+
9+
const PORT = 3004;
10+
11+
let app;
12+
let requestCounters;
13+
let server;
14+
15+
function initApp() {
16+
app = express();
17+
requestCounters = new Set();
18+
19+
app.use((req, res, next) => {
20+
for (const requestCounter of requestCounters) {
21+
requestCounter.count(req);
22+
}
23+
next();
24+
});
25+
26+
const routes = Object.values(requireDir('./routes'));
27+
for (const {match, handler} of routes) {
28+
app.get(match, handler);
29+
}
30+
31+
const staticDir = path.resolve(__dirname, '..', '..', '..');
32+
app.use(
33+
express.static(staticDir),
34+
serveIndex(staticDir, {'icons': true})
35+
);
36+
}
37+
38+
function start() {
39+
if (!app) {
40+
initApp();
41+
}
42+
43+
return new Promise((resolve, reject) => {
44+
server = app.listen(PORT, (error) => {
45+
if (error) {
46+
reject(error);
47+
} else {
48+
logHelper.log(`The test server is running at ${getAddress()}`);
49+
resolve();
50+
}
51+
});
52+
});
53+
}
54+
55+
function stop() {
56+
if (server) {
57+
server.close();
58+
}
59+
}
60+
61+
function getAddress() {
62+
return `http://localhost:${PORT}`;
63+
}
64+
65+
function startCountingRequests(headerValue) {
66+
const requestCounter = new RequestCounter(headerValue);
67+
requestCounters.add(requestCounter);
68+
return requestCounter;
69+
}
70+
71+
function stopCountingRequests(requestCounter) {
72+
requestCounters.delete(requestCounter);
73+
}
74+
75+
module.exports = {
76+
getAddress,
77+
start,
78+
startCountingRequests,
79+
stop,
80+
stopCountingRequests,
81+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class RequestCounter {
2+
constructor(headerName) {
3+
this.headerName = headerName;
4+
this.headerCount = {};
5+
this.urlCount = {};
6+
}
7+
8+
count(req) {
9+
if (this.headerName) {
10+
const headerValue = req.get(this.headerName);
11+
if (headerValue !== undefined) {
12+
if (!(headerValue in this.headerCount)) {
13+
this.headerCount[headerValue] = 0;
14+
}
15+
this.headerCount[headerValue]++;
16+
}
17+
}
18+
19+
const url = req.url;
20+
if (!(url in this.urlCount)) {
21+
this.urlCount[url] = 0;
22+
}
23+
this.urlCount[url]++;
24+
}
25+
26+
getHeaderCount(headerValue) {
27+
return this.headerCount[headerValue] || 0;
28+
}
29+
30+
getUrlCount(url) {
31+
return this.urlCount[url] || 0;
32+
}
33+
}
34+
35+
module.exports = RequestCounter;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const path = require('path');
2+
3+
const constants = require('../../../../gulp-tasks/utils/constants');
4+
5+
const match = '/__WORKBOX/buildFile/:moduleInfo';
6+
7+
async function handler(req, res) {
8+
const {moduleInfo} = req.params;
9+
const [moduleName, buildType, extension] = moduleInfo.split('.', 3);
10+
11+
const packagePath = `../../../../packages/${moduleName}`;
12+
const {main} = require(`${packagePath}/package.json`);
13+
14+
const buildPath = path.dirname(path.join(packagePath, main));
15+
let fileName = path.basename(main);
16+
17+
if (buildType) {
18+
fileName = fileName.replace('.prod.', `.${buildType}.`);
19+
} else if (process.env.NODE_ENV === constants.BUILD_TYPES.dev) {
20+
fileName = fileName.replace('.prod.', '.dev.');
21+
}
22+
23+
if (extension) {
24+
fileName = fileName.replace(/\.js$/, `.${extension}`);
25+
}
26+
27+
const filePath = path.resolve(__dirname, buildPath, fileName);
28+
res.sendFile(filePath);
29+
}
30+
31+
module.exports = {
32+
handler,
33+
match,
34+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const path = require('path');
2+
3+
const match = '/__WORKBOX/comlink.js';
4+
5+
async function handler(req, res) {
6+
const comlinkMain = require.resolve('comlinkjs');
7+
const comlinkPath = path.join(path.dirname(comlinkMain), 'umd', 'comlink.js');
8+
res.sendFile(comlinkPath);
9+
}
10+
11+
module.exports = {
12+
handler,
13+
match,
14+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const path = require('path');
2+
3+
const match = '/*/integration.html';
4+
5+
async function handler(req, res) {
6+
const filePath = path.join(__dirname, '..', 'static', 'integration.html');
7+
res.sendFile(filePath);
8+
}
9+
10+
module.exports = {
11+
handler,
12+
match,
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const match = '/__WORKBOX/uniqueETag';
2+
3+
let counter = 0;
4+
async function handler(req, res) {
5+
res.set('ETag', ++counter);
6+
res.send(`ETag is ${counter}.`);
7+
}
8+
9+
module.exports = {
10+
handler,
11+
match,
12+
};

0 commit comments

Comments
 (0)