Skip to content

Commit

Permalink
tests(smokehouse): index for static server. print address (#9541)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Aug 13, 2019
1 parent fa878cb commit e4a1e1a
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lighthouse-cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const http = require('http');
const zlib = require('zlib');
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const mime = require('mime-types');
const parseQueryString = require('querystring').parse;
const parseURL = require('url').parse;
Expand All @@ -26,6 +27,21 @@ function requestHandler(request, response) {
const queryString = requestUrl.search && parseQueryString(requestUrl.search.slice(1));
let absoluteFilePath = path.join(__dirname, filePath);

// Create an index page that lists the available test pages.
if (filePath === '/') {
const fixturePaths = glob.sync('**/*.html', {cwd: __dirname});
const html = `
<html>
<h1>Smoke test fixtures</h1>
${fixturePaths.map(p => `<a href=${encodeURI(p)}>${escape(p)}</a>`).join('<br>')}
`;
response.writeHead(200, {
'Content-Security-Policy': `default-src 'none';`,
});
sendResponse(200, html);
return;
}

if (filePath.startsWith('/dist/viewer')) {
// Rewrite lighthouse-viewer paths to point to that location.
absoluteFilePath = path.join(__dirname, '/../../../', filePath);
Expand Down Expand Up @@ -142,12 +158,15 @@ const serverForOffline = http.createServer(requestHandler);
serverForOnline.on('error', e => console.error(e.code, e));
serverForOffline.on('error', e => console.error(e.code, e));


// If called via `node static-server.js` then start listening, otherwise, just expose the servers
if (require.main === module) {
// Start listening
serverForOnline.listen(10200, 'localhost');
serverForOffline.listen(10503, 'localhost');
const onlinePort = 10200;
const offlinePort = 10503;
serverForOnline.listen(onlinePort, 'localhost');
serverForOffline.listen(offlinePort, 'localhost');
console.log(`online: listening on http://localhost:${onlinePort}`);
console.log(`offline: listening on http://localhost:${offlinePort}`);
} else {
module.exports = {
server: serverForOnline,
Expand Down

0 comments on commit e4a1e1a

Please sign in to comment.