puppeteer/third_party/phantomjs/test/module/webpage/abort-network-request.js
Andrey Lushnikov e274c26e8b Implement Page.setRequestInterceptor
This patch:
- introduces Request class.
- implements Page.setRequestInterceptor method. The method
  allows to install a callback which will be called for every request
  with a |Request| object as a single parameter. The callback is free
  to override certain request's properties and then either continue or
  abort it.
- implements request interception api for phantom-shim and unskips the
  module/webpage/abort-network-request.js phantomjs test

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

37 lines
1.2 KiB
JavaScript

var webpage = require('webpage');
async_test(function () {
var page = webpage.create();
var abortCount = 0;
var errorCount = 0;
var abortedIds = {};
var urlToBlockRegExp = /logo\.png$/i;
page.onResourceRequested = this.step_func(function(requestData, request) {
assert_type_of(request, 'object');
assert_type_of(request.abort, 'function');
if (urlToBlockRegExp.test(requestData.url)) {
request.abort();
++abortCount;
abortedIds[requestData.id] = 1;
}
});
page.onResourceError = this.step_func(function(error) {
// We can't match up errors to requests by URL because error.url will
// be the empty string in this case. FIXME.
assert_own_property(abortedIds, error.id);
++errorCount;
});
page.onResourceReceived = this.step_func(function(response) {
assert_regexp_not_match(response.url, urlToBlockRegExp);
});
page.open(TEST_HTTP_BASE + 'logo.html',
this.step_func_done(function (status) {
assert_equals(status, 'success');
assert_equals(abortCount, 1);
assert_equals(errorCount, 1);
}));
}, "can abort network requests");