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
This commit is contained in:
parent
e274c26e8b
commit
85ecce31f9
55
test/StaticServer.js
Normal file
55
test/StaticServer.js
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* 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;
|
3
test/assets/one-style.css
Normal file
3
test/assets/one-style.css
Normal file
@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: pink;
|
||||
}
|
2
test/assets/one-style.html
Normal file
2
test/assets/one-style.html
Normal file
@ -0,0 +1,2 @@
|
||||
<link rel='stylesheet' href='./one-style.css'>
|
||||
<div>hello, world!</div>
|
41
test/test.js
41
test/test.js
@ -1,18 +1,40 @@
|
||||
/**
|
||||
* 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 path = require('path');
|
||||
var Browser = require('../lib/Browser');
|
||||
var StaticServer = require('./StaticServer');
|
||||
|
||||
var EMPTY_PAGE = 'file://' + path.join(__dirname, 'assets', 'empty.html');
|
||||
var PORT = 8907;
|
||||
var STATIC_PREFIX = 'http://localhost:' + PORT;
|
||||
var EMPTY_PAGE = STATIC_PREFIX + '/empty.html';
|
||||
|
||||
describe('Puppeteer', function() {
|
||||
var browser;
|
||||
var staticServer;
|
||||
var page;
|
||||
|
||||
beforeAll(function() {
|
||||
browser = new Browser();
|
||||
staticServer = new StaticServer(path.join(__dirname, 'assets'), PORT);
|
||||
});
|
||||
|
||||
afterAll(function() {
|
||||
browser.close();
|
||||
staticServer.stop();
|
||||
});
|
||||
|
||||
beforeEach(SX(async function() {
|
||||
@ -86,7 +108,7 @@ describe('Puppeteer', function() {
|
||||
});
|
||||
|
||||
describe('Page.setRequestInterceptor', function() {
|
||||
it('should work', SX(async function() {
|
||||
it('should intercept', SX(async function() {
|
||||
page.setRequestInterceptor(request => {
|
||||
expect(request.url()).toContain('empty.html');
|
||||
expect(request.headers()['User-Agent']).toBeTruthy();
|
||||
@ -95,6 +117,7 @@ describe('Puppeteer', function() {
|
||||
request.continue();
|
||||
});
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
}));
|
||||
it('should show extraHTTPHeaders', SX(async function() {
|
||||
await page.setExtraHTTPHeaders({
|
||||
@ -105,6 +128,20 @@ describe('Puppeteer', function() {
|
||||
request.continue();
|
||||
});
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
}));
|
||||
it('should be abortable', SX(async function() {
|
||||
page.setRequestInterceptor(request => {
|
||||
if (request.url().endsWith('.css'))
|
||||
request.abort();
|
||||
else
|
||||
request.continue();
|
||||
});
|
||||
var failedResources = 0;
|
||||
page.on('resourceloadingfailed', event => ++failedResources);
|
||||
var success = await page.navigate(STATIC_PREFIX + '/one-style.html');
|
||||
expect(success).toBe(true);
|
||||
expect(failedResources).toBe(1);
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user