Fixing code review issues: adding a partition options and making the session option only takes Session objects.
This commit is contained in:
parent
6d7f179a9b
commit
b44d5290e2
4 changed files with 40 additions and 17 deletions
|
@ -175,16 +175,21 @@ URLRequest::~URLRequest() {
|
||||||
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
|
mate::WrappableBase* URLRequest::New(mate::Arguments* args) {
|
||||||
v8::Local<v8::Object> options;
|
v8::Local<v8::Object> options;
|
||||||
args->GetNext(&options);
|
args->GetNext(&options);
|
||||||
mate::Dictionary dict(args->isolate(), options);
|
auto isolate = args->isolate();
|
||||||
|
mate::Dictionary dict(isolate, options);
|
||||||
std::string method;
|
std::string method;
|
||||||
dict.Get("method", &method);
|
dict.Get("method", &method);
|
||||||
std::string url;
|
std::string url;
|
||||||
dict.Get("url", &url);
|
dict.Get("url", &url);
|
||||||
std::string session_name;
|
std::string partition;
|
||||||
dict.Get("session", &session_name);
|
mate::Handle<api::Session> session;
|
||||||
|
if (dict.Get("session", &session)) {
|
||||||
auto session = Session::FromPartition(args->isolate(), session_name);
|
} else if (dict.Get("partition", &partition)) {
|
||||||
|
session = Session::FromPartition(isolate, partition);
|
||||||
|
} else {
|
||||||
|
// Use the default session if not specified.
|
||||||
|
session = Session::FromPartition(isolate, "");
|
||||||
|
}
|
||||||
auto browser_context = session->browser_context();
|
auto browser_context = session->browser_context();
|
||||||
auto api_url_request = new URLRequest(args->isolate(), args->GetThis());
|
auto api_url_request = new URLRequest(args->isolate(), args->GetThis());
|
||||||
auto atom_url_request =
|
auto atom_url_request =
|
||||||
|
|
|
@ -82,8 +82,12 @@ following properties:
|
||||||
method.
|
method.
|
||||||
* `url` String (optional) - The request URL. Must be provided in the absolute
|
* `url` String (optional) - The request URL. Must be provided in the absolute
|
||||||
form with the protocol scheme specified as http or https.
|
form with the protocol scheme specified as http or https.
|
||||||
* `session` String (optional) - The name of the [`Session`](session.md)
|
* `session` Object (optional) - The [`Session`](session.md) instance with
|
||||||
instance with which the request is associated. Defaults to the empty string.
|
which the request is associated.
|
||||||
|
* `partition` String (optional) - The name of the [`partition`](session.md)
|
||||||
|
with which the request is associated. Defaults to the empty string. The
|
||||||
|
`session` option prevails on `partition`. Thus if a `session` is explicitly
|
||||||
|
specified, `partition` is ignored.
|
||||||
* `protocol` String (optional) - The protocol scheme in the form 'scheme:'.
|
* `protocol` String (optional) - The protocol scheme in the form 'scheme:'.
|
||||||
Currently supported values are 'http:' or 'https:'. Defaults to 'http:'.
|
Currently supported values are 'http:' or 'https:'. Defaults to 'http:'.
|
||||||
* `host` String (optional) - The server host provided as a concatenation of
|
* `host` String (optional) - The server host provided as a concatenation of
|
||||||
|
|
|
@ -5,6 +5,7 @@ const {EventEmitter} = require('events')
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
const {Readable} = require('stream')
|
const {Readable} = require('stream')
|
||||||
const {app} = require('electron')
|
const {app} = require('electron')
|
||||||
|
const {session} = require('electron')
|
||||||
const {net, Net} = process.atomBinding('net')
|
const {net, Net} = process.atomBinding('net')
|
||||||
const {URLRequest} = net
|
const {URLRequest} = net
|
||||||
|
|
||||||
|
@ -155,12 +156,25 @@ class ClientRequest extends EventEmitter {
|
||||||
urlStr = url.format(urlObj)
|
urlStr = url.format(urlObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionName = options.session || ''
|
let urlRequestOptions = {
|
||||||
let urlRequest = new URLRequest({
|
|
||||||
method: method,
|
method: method,
|
||||||
url: urlStr,
|
url: urlStr
|
||||||
session: sessionName
|
}
|
||||||
})
|
if (options.session) {
|
||||||
|
if (options.session instanceof session.Session) {
|
||||||
|
urlRequestOptions.session = options.session
|
||||||
|
} else {
|
||||||
|
throw new TypeError('`session` should be an instance of the Session class.')
|
||||||
|
}
|
||||||
|
} else if (options.partition) {
|
||||||
|
if (typeof options.partition === 'string') {
|
||||||
|
urlRequestOptions.partition = options.partition
|
||||||
|
} else {
|
||||||
|
throw new TypeError('`partition` should be an a string.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let urlRequest = new URLRequest(urlRequestOptions)
|
||||||
|
|
||||||
// Set back and forward links.
|
// Set back and forward links.
|
||||||
this.urlRequest = urlRequest
|
this.urlRequest = urlRequest
|
||||||
|
|
|
@ -797,10 +797,10 @@ describe('net module', function () {
|
||||||
urlRequest.end()
|
urlRequest.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should to able to create and intercept a request on a custom session', function (done) {
|
it('should to able to create and intercept a request using a custom parition name', function (done) {
|
||||||
const requestUrl = '/requestUrl'
|
const requestUrl = '/requestUrl'
|
||||||
const redirectUrl = '/redirectUrl'
|
const redirectUrl = '/redirectUrl'
|
||||||
const customSessionName = 'custom-session'
|
const customPartitionName = 'custom-partition'
|
||||||
let requestIsRedirected = false
|
let requestIsRedirected = false
|
||||||
server.on('request', function (request, response) {
|
server.on('request', function (request, response) {
|
||||||
switch (request.url) {
|
switch (request.url) {
|
||||||
|
@ -821,7 +821,7 @@ describe('net module', function () {
|
||||||
assert(false, 'Request should not be intercepted by the default session')
|
assert(false, 'Request should not be intercepted by the default session')
|
||||||
})
|
})
|
||||||
|
|
||||||
let customSession = session.fromPartition(customSessionName, {
|
let customSession = session.fromPartition(customPartitionName, {
|
||||||
cache: false
|
cache: false
|
||||||
})
|
})
|
||||||
let requestIsIntercepted = false
|
let requestIsIntercepted = false
|
||||||
|
@ -841,7 +841,7 @@ describe('net module', function () {
|
||||||
|
|
||||||
const urlRequest = net.request({
|
const urlRequest = net.request({
|
||||||
url: `${server.url}${requestUrl}`,
|
url: `${server.url}${requestUrl}`,
|
||||||
session: customSessionName
|
partition: customPartitionName
|
||||||
})
|
})
|
||||||
urlRequest.on('response', function (response) {
|
urlRequest.on('response', function (response) {
|
||||||
assert.equal(response.statusCode, 200)
|
assert.equal(response.statusCode, 200)
|
||||||
|
|
Loading…
Reference in a new issue