puppeteer/test/StaticServer.js
Andrey Lushnikov 85ecce31f9 Add Request.abort() test
This patch:
- introduces StaticServer for the testing purposes
- switches tests from navigating to file:// to http://
- adds a test to cover Request.abort() functionality

References #1
2017-06-15 08:26:55 -07:00

56 lines
1.6 KiB
JavaScript

/**
* Copyright 2017 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.
*/
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
class StaticServer {
/**
* @param {string} dirPath
* @param {number} port
*/
constructor(dirPath, port) {
this._server = http.createServer(this._onRequest.bind(this));
this._server.listen(port);
this._dirPath = dirPath;
}
stop() {
this._server.close();
}
_onRequest(request, response) {
var pathName = url.parse(request.url).path;
if (pathName === '/')
pathName = '/index.html';
pathName = path.join(this._dirPath, pathName.substring(1));
fs.readFile(pathName, function(err, data) {
if (err) {
response.statusCode = 404;
response.end(`File not found: ${pathName}`);
return;
}
response.setHeader('Content-Type', mime.lookup(pathName));
response.end(data);
});
}
}
module.exports = StaticServer;