/* Copyright 2016 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /* eslint-disable require-jsdoc */ const cookieParser = require('cookie-parser'); const express = require('express'); const fs = require('fs'); const glob = require('glob'); const handlebars = require('handlebars'); const path = require('path'); const serveIndex = require('serve-index'); const serveStatic = require('serve-static'); class ServerInstance { constructor() { this._server = null; this._sockets = []; this._app = express(); this._app.use(cookieParser()); // Test iframe is used by sw-testing-helpers to scope service workers this._app.get('/test/iframe/:random', function(req, res) { res.sendFile(path.join(__dirname, 'test-iframe.html')); }); this._app.get('/__echo/filename/:file', function(req, res) { res.setHeader('Cache-Control', 'max-age=' + (24 * 60 * 60)); res.send(req.params.file); }); this._app.get('/__echo/date/:file', function(req, res) { res.setHeader('Cache-Control', 'max-age=' + (24 * 60 * 60)); res.send(`${req.params.file}-${Date.now()}`); }); this._app.all('/__echo/date-with-cors/:file', function(req, res) { res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS'); res.setHeader('Cache-Control', 'max-age=' + (24 * 60 * 60)); res.setHeader('Access-Control-Allow-Origin', '*'); res.send(`${req.params.file}-${Date.now()}`); }); this._app.get('/__test/404/', function(req, res) { res.status(404).send('404'); }); this._app.get('/__test/redirect/:statusCode/', function(req, res) { const statusCode = parseInt(req.params.statusCode, 10); res.redirect( statusCode, `/__echo/filename/${statusCode}` ); }); this._app.get('/__test/cookie/:id', function(req, res) { res.send(JSON.stringify(req.cookies)); }); // Harness to kick off all the in-browser tests for a given package. // It will pick up a list of all the top-level .js files and automatically // inject them into the HTML as