2017-10-23 05:17:23 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-02 20:13:26 +00:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
2017-10-23 05:17:23 +00:00
|
|
|
const http = require("http");
|
|
|
|
|
|
|
|
const testPort = 8989;
|
|
|
|
const testUrl = `http://localhost:${testPort}`;
|
|
|
|
|
|
|
|
const staticResponse = `<!DOCTYPE "html">
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Test Page</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<p>Test Content</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
|
2017-10-24 02:35:47 +00:00
|
|
|
module.exports.start = function (callback, html) {
|
2017-10-23 05:17:23 +00:00
|
|
|
|
|
|
|
const httpServer = http.createServer(function (req, res) {
|
|
|
|
|
2017-10-23 08:29:59 +00:00
|
|
|
const headers = {"Content-Type": "text/html; charset=utf-8"};
|
2017-10-23 05:17:23 +00:00
|
|
|
|
|
|
|
if (callback !== undefined) {
|
|
|
|
callback(headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.writeHead(200, headers);
|
2017-10-24 02:35:47 +00:00
|
|
|
res.write(html || staticResponse);
|
2017-10-23 05:17:23 +00:00
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
httpServer.listen(8989);
|
|
|
|
return [httpServer, testUrl];
|
|
|
|
};
|
|
|
|
|
2017-11-02 20:13:26 +00:00
|
|
|
/**
|
|
|
|
* Starts the test server, serving the provided html file from the fixtures dir.
|
|
|
|
*
|
|
|
|
* @param {string} testHtmlFileName
|
|
|
|
* The name of the fixture file the test server should serve.
|
|
|
|
* @param {function|undefined} callback
|
|
|
|
* An optional callback function that, if provided, will be called with
|
|
|
|
* a single argument, an object describing which headers will be sent from
|
|
|
|
* the server. The callback function can modify this as needed.
|
|
|
|
*
|
|
|
|
* @return {array}
|
|
|
|
* An array of length two. The first item is the server object, and the
|
|
|
|
* second item is the absolute URL that the server is serving from.
|
|
|
|
*/
|
|
|
|
module.exports.startWithFile = function (testHtmlFileName, callback) {
|
|
|
|
|
|
|
|
const pathToTestHtmlFileName = path.join(__dirname, "..", "..", "fixtures", testHtmlFileName);
|
|
|
|
const htmlFileContents = fs.readFileSync(pathToTestHtmlFileName, "utf8");
|
|
|
|
return module.exports.start(callback, htmlFileContents);
|
|
|
|
};
|
|
|
|
|
2017-10-23 05:17:23 +00:00
|
|
|
module.exports.stop = function (server) {
|
|
|
|
server.close();
|
2017-10-23 08:29:59 +00:00
|
|
|
};
|