puppeteer/lib/Request.js

127 lines
2.8 KiB
JavaScript
Raw Normal View History

/**
* 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.
*/
class Request {
2017-06-21 20:51:06 +00:00
/**
* @param {!Connection} client
* @param {string} interceptionId
* @param {!Object} payload
*/
2017-06-21 20:51:06 +00:00
constructor(client, interceptionId, payload) {
this._client = client;
this._interceptionId = interceptionId;
this._url = payload.url;
this._method = payload.method;
this._headers = payload.headers;
this._postData = payload.postData;
2017-06-21 20:51:06 +00:00
this._urlOverride = undefined;
this._methodOverride = undefined;
this._postDataOverride = undefined;
2017-06-21 20:51:06 +00:00
this._handled = false;
}
2017-06-21 20:51:06 +00:00
/**
* @return {string}
*/
2017-06-21 20:51:06 +00:00
url() {
return this._urlOverride || this._url;
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} url
*/
2017-06-21 20:51:06 +00:00
setUrl(url) {
this._urlOverride = url;
}
2017-06-21 20:51:06 +00:00
/**
* @return {string}
*/
2017-06-21 20:51:06 +00:00
method() {
return this._methodOverride || this._method;
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} method
*/
2017-06-21 20:51:06 +00:00
setMethod(method) {
this._methodOverride = method;
}
2017-06-21 20:51:06 +00:00
/**
* @return {!Object}
*/
2017-06-21 20:51:06 +00:00
headers() {
return Object.assign({}, this._headersOverride || this._headers);
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} key
* @param {string} value
*/
2017-06-21 20:51:06 +00:00
setHeader(key, value) {
if (!this._headersOverride)
this._headersOverride = Object.assign({}, this._headers);
this._headersOverride[key] = value;
}
2017-06-21 20:51:06 +00:00
/**
* @return {(string|undefined)}
*/
2017-06-21 20:51:06 +00:00
postData() {
return this._postDataOverride || this._postData;
}
2017-06-21 20:51:06 +00:00
/**
* @return {(string|undefined)}
*/
2017-06-21 20:51:06 +00:00
setPostData(data) {
this._postDataOverride = data;
}
2017-06-21 20:51:06 +00:00
abort() {
console.assert(!this._handled, 'This request is already handled!');
this._handled = true;
this._client.send('Network.continueInterceptedRequest', {
interceptionId: this._interceptionId,
errorReason: 'Aborted'
});
}
2017-06-21 20:51:06 +00:00
continue() {
console.assert(!this._handled, 'This request is already handled!');
this._handled = true;
this._client.send('Network.continueInterceptedRequest', {
interceptionId: this._interceptionId,
url: this._urlOverride,
method: this._methodOverride,
postData: this._postDataOverride,
headers: this._headersOverride
});
}
2017-06-21 20:51:06 +00:00
/**
* @return {boolean}
*/
2017-06-21 20:51:06 +00:00
handled() {
return this._handled;
}
}
module.exports = Request;