chore: change undocumented protocol.registerProtocol to detect body type (#36595)
* feat: add protocol.registerProtocol * remove wip handleProtocol code * lint * Update shell/browser/net/electron_url_loader_factory.h Co-authored-by: Cheng Zhao <zcbenz@gmail.com> * fix --------- Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: Cheng Zhao <zcbenz@gmail.com>
This commit is contained in:
parent
a37f572388
commit
01f1522cbd
3 changed files with 485 additions and 434 deletions
197
shell/browser/net/electron_url_loader_factory.cc
Executable file → Normal file
197
shell/browser/net/electron_url_loader_factory.cc
Executable file → Normal file
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include "shell/browser/net/electron_url_loader_factory.h"
|
#include "shell/browser/net/electron_url_loader_factory.h"
|
||||||
|
|
||||||
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
@ -81,6 +82,19 @@ bool ResponseMustBeObject(ProtocolType type) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool LooksLikeStream(v8::Isolate* isolate, v8::Local<v8::Value> v) {
|
||||||
|
// the stream loader can handle null and undefined as "empty body". Could
|
||||||
|
// probably be more efficient here but this works.
|
||||||
|
if (v->IsNullOrUndefined())
|
||||||
|
return true;
|
||||||
|
if (!v->IsObject())
|
||||||
|
return false;
|
||||||
|
gin_helper::Dictionary dict(isolate, v.As<v8::Object>());
|
||||||
|
v8::Local<v8::Value> method;
|
||||||
|
return dict.Get("on", &method) && method->IsFunction() &&
|
||||||
|
dict.Get("removeListener", &method) && method->IsFunction();
|
||||||
|
}
|
||||||
|
|
||||||
// Helper to convert value to Dictionary.
|
// Helper to convert value to Dictionary.
|
||||||
gin::Dictionary ToDict(v8::Isolate* isolate, v8::Local<v8::Value> value) {
|
gin::Dictionary ToDict(v8::Isolate* isolate, v8::Local<v8::Value> value) {
|
||||||
if (!value->IsFunction() && value->IsObject())
|
if (!value->IsFunction() && value->IsObject())
|
||||||
|
@ -390,36 +404,94 @@ void ElectronURLLoaderFactory::StartLoading(
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
// DEPRECATED: Soon only |kFree| will be supported!
|
||||||
case ProtocolType::kBuffer:
|
case ProtocolType::kBuffer:
|
||||||
StartLoadingBuffer(std::move(client), std::move(head), dict);
|
if (response->IsArrayBufferView())
|
||||||
break;
|
StartLoadingBuffer(std::move(client), std::move(head),
|
||||||
case ProtocolType::kString:
|
response.As<v8::ArrayBufferView>());
|
||||||
StartLoadingString(std::move(client), std::move(head), dict,
|
else if (v8::Local<v8::Value> data; !dict.IsEmpty() &&
|
||||||
args->isolate(), response);
|
dict.Get("data", &data) &&
|
||||||
break;
|
data->IsArrayBufferView())
|
||||||
case ProtocolType::kFile:
|
StartLoadingBuffer(std::move(client), std::move(head),
|
||||||
StartLoadingFile(std::move(loader), request, std::move(client),
|
data.As<v8::ArrayBufferView>());
|
||||||
std::move(head), dict, args->isolate(), response);
|
else
|
||||||
break;
|
|
||||||
case ProtocolType::kHttp:
|
|
||||||
StartLoadingHttp(std::move(loader), request, std::move(client),
|
|
||||||
traffic_annotation, dict);
|
|
||||||
break;
|
|
||||||
case ProtocolType::kStream:
|
|
||||||
StartLoadingStream(std::move(loader), std::move(client), std::move(head),
|
|
||||||
dict);
|
|
||||||
break;
|
|
||||||
case ProtocolType::kFree:
|
|
||||||
ProtocolType protocol_type;
|
|
||||||
if (!gin::ConvertFromV8(args->isolate(), response, &protocol_type)) {
|
|
||||||
OnComplete(std::move(client), request_id,
|
OnComplete(std::move(client), request_id,
|
||||||
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
||||||
return;
|
|
||||||
}
|
|
||||||
StartLoading(std::move(loader), request_id, options, request,
|
|
||||||
std::move(client), traffic_annotation,
|
|
||||||
std::move(target_factory), protocol_type, args);
|
|
||||||
break;
|
break;
|
||||||
|
case ProtocolType::kString: {
|
||||||
|
std::string data;
|
||||||
|
if (gin::ConvertFromV8(args->isolate(), response, &data))
|
||||||
|
SendContents(std::move(client), std::move(head), data);
|
||||||
|
else if (!dict.IsEmpty() && dict.Get("data", &data))
|
||||||
|
SendContents(std::move(client), std::move(head), data);
|
||||||
|
else
|
||||||
|
OnComplete(std::move(client), request_id,
|
||||||
|
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ProtocolType::kFile: {
|
||||||
|
base::FilePath path;
|
||||||
|
if (gin::ConvertFromV8(args->isolate(), response, &path))
|
||||||
|
StartLoadingFile(std::move(client), std::move(loader), std::move(head),
|
||||||
|
request, path, dict);
|
||||||
|
else if (!dict.IsEmpty() && dict.Get("path", &path))
|
||||||
|
StartLoadingFile(std::move(client), std::move(loader), std::move(head),
|
||||||
|
request, path, dict);
|
||||||
|
else
|
||||||
|
OnComplete(std::move(client), request_id,
|
||||||
|
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ProtocolType::kHttp:
|
||||||
|
if (GURL url; !dict.IsEmpty() && dict.Get("url", &url) && url.is_valid())
|
||||||
|
StartLoadingHttp(std::move(client), std::move(loader), request,
|
||||||
|
traffic_annotation, dict);
|
||||||
|
else
|
||||||
|
OnComplete(std::move(client), request_id,
|
||||||
|
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
||||||
|
break;
|
||||||
|
case ProtocolType::kStream:
|
||||||
|
StartLoadingStream(std::move(client), std::move(loader), std::move(head),
|
||||||
|
dict);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ProtocolType::kFree: {
|
||||||
|
// Infer the type based on the object given
|
||||||
|
v8::Local<v8::Value> data;
|
||||||
|
if (!dict.IsEmpty() && dict.Has("data"))
|
||||||
|
dict.Get("data", &data);
|
||||||
|
else
|
||||||
|
data = response;
|
||||||
|
|
||||||
|
// |data| can be either a string, a buffer or a stream.
|
||||||
|
if (data->IsArrayBufferView()) {
|
||||||
|
StartLoadingBuffer(std::move(client), std::move(head),
|
||||||
|
data.As<v8::ArrayBufferView>());
|
||||||
|
} else if (data->IsString()) {
|
||||||
|
SendContents(std::move(client), std::move(head),
|
||||||
|
gin::V8ToString(args->isolate(), data));
|
||||||
|
} else if (LooksLikeStream(args->isolate(), data)) {
|
||||||
|
StartLoadingStream(std::move(client), std::move(loader),
|
||||||
|
std::move(head), dict);
|
||||||
|
} else if (!dict.IsEmpty()) {
|
||||||
|
// |data| wasn't specified, so look for |response.url| or
|
||||||
|
// |response.path|.
|
||||||
|
if (GURL url; dict.Get("url", &url))
|
||||||
|
StartLoadingHttp(std::move(client), std::move(loader), request,
|
||||||
|
traffic_annotation, dict);
|
||||||
|
else if (base::FilePath path; dict.Get("path", &path))
|
||||||
|
StartLoadingFile(std::move(client), std::move(loader),
|
||||||
|
std::move(head), request, path, dict);
|
||||||
|
else
|
||||||
|
// Don't know what kind of response this is, so fail.
|
||||||
|
OnComplete(std::move(client), request_id,
|
||||||
|
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
||||||
|
} else {
|
||||||
|
OnComplete(std::move(client), request_id,
|
||||||
|
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -427,68 +499,25 @@ void ElectronURLLoaderFactory::StartLoading(
|
||||||
void ElectronURLLoaderFactory::StartLoadingBuffer(
|
void ElectronURLLoaderFactory::StartLoadingBuffer(
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
network::mojom::URLResponseHeadPtr head,
|
network::mojom::URLResponseHeadPtr head,
|
||||||
const gin_helper::Dictionary& dict) {
|
v8::Local<v8::ArrayBufferView> buffer) {
|
||||||
v8::Local<v8::Value> buffer = dict.GetHandle();
|
SendContents(std::move(client), std::move(head),
|
||||||
dict.Get("data", &buffer);
|
std::string(node::Buffer::Data(buffer.As<v8::Value>()),
|
||||||
if (!node::Buffer::HasInstance(buffer)) {
|
node::Buffer::Length(buffer.As<v8::Value>())));
|
||||||
mojo::Remote<network::mojom::URLLoaderClient> client_remote(
|
|
||||||
std::move(client));
|
|
||||||
client_remote->OnComplete(
|
|
||||||
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SendContents(
|
|
||||||
std::move(client), std::move(head),
|
|
||||||
std::string(node::Buffer::Data(buffer), node::Buffer::Length(buffer)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void ElectronURLLoaderFactory::StartLoadingString(
|
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
||||||
network::mojom::URLResponseHeadPtr head,
|
|
||||||
const gin_helper::Dictionary& dict,
|
|
||||||
v8::Isolate* isolate,
|
|
||||||
v8::Local<v8::Value> response) {
|
|
||||||
std::string contents;
|
|
||||||
if (response->IsString()) {
|
|
||||||
contents = gin::V8ToString(isolate, response);
|
|
||||||
} else if (!dict.IsEmpty()) {
|
|
||||||
dict.Get("data", &contents);
|
|
||||||
} else {
|
|
||||||
mojo::Remote<network::mojom::URLLoaderClient> client_remote(
|
|
||||||
std::move(client));
|
|
||||||
client_remote->OnComplete(
|
|
||||||
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SendContents(std::move(client), std::move(head), std::move(contents));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void ElectronURLLoaderFactory::StartLoadingFile(
|
void ElectronURLLoaderFactory::StartLoadingFile(
|
||||||
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
||||||
network::ResourceRequest request,
|
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
||||||
network::mojom::URLResponseHeadPtr head,
|
network::mojom::URLResponseHeadPtr head,
|
||||||
const gin_helper::Dictionary& dict,
|
const network::ResourceRequest& original_request,
|
||||||
v8::Isolate* isolate,
|
const base::FilePath& path,
|
||||||
v8::Local<v8::Value> response) {
|
const gin_helper::Dictionary& opts) {
|
||||||
base::FilePath path;
|
network::ResourceRequest request = original_request;
|
||||||
if (gin::ConvertFromV8(isolate, response, &path)) {
|
request.url = net::FilePathToFileURL(path);
|
||||||
request.url = net::FilePathToFileURL(path);
|
if (!opts.IsEmpty()) {
|
||||||
} else if (!dict.IsEmpty()) {
|
opts.Get("referrer", &request.referrer);
|
||||||
dict.Get("referrer", &request.referrer);
|
opts.Get("method", &request.method);
|
||||||
dict.Get("method", &request.method);
|
|
||||||
if (dict.Get("path", &path))
|
|
||||||
request.url = net::FilePathToFileURL(path);
|
|
||||||
} else {
|
|
||||||
mojo::Remote<network::mojom::URLLoaderClient> client_remote(
|
|
||||||
std::move(client));
|
|
||||||
client_remote->OnComplete(
|
|
||||||
network::URLLoaderCompletionStatus(net::ERR_FAILED));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add header to ignore CORS.
|
// Add header to ignore CORS.
|
||||||
|
@ -499,9 +528,9 @@ void ElectronURLLoaderFactory::StartLoadingFile(
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void ElectronURLLoaderFactory::StartLoadingHttp(
|
void ElectronURLLoaderFactory::StartLoadingHttp(
|
||||||
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
||||||
const network::ResourceRequest& original_request,
|
const network::ResourceRequest& original_request,
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
||||||
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
||||||
const gin_helper::Dictionary& dict) {
|
const gin_helper::Dictionary& dict) {
|
||||||
auto request = std::make_unique<network::ResourceRequest>();
|
auto request = std::make_unique<network::ResourceRequest>();
|
||||||
|
@ -543,8 +572,8 @@ void ElectronURLLoaderFactory::StartLoadingHttp(
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void ElectronURLLoaderFactory::StartLoadingStream(
|
void ElectronURLLoaderFactory::StartLoadingStream(
|
||||||
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
||||||
network::mojom::URLResponseHeadPtr head,
|
network::mojom::URLResponseHeadPtr head,
|
||||||
const gin_helper::Dictionary& dict) {
|
const gin_helper::Dictionary& dict) {
|
||||||
v8::Local<v8::Value> stream;
|
v8::Local<v8::Value> stream;
|
||||||
|
|
|
@ -135,33 +135,27 @@ class ElectronURLLoaderFactory : public network::SelfDeletingURLLoaderFactory {
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
int32_t request_id,
|
int32_t request_id,
|
||||||
const network::URLLoaderCompletionStatus& status);
|
const network::URLLoaderCompletionStatus& status);
|
||||||
|
|
||||||
static void StartLoadingBuffer(
|
static void StartLoadingBuffer(
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
network::mojom::URLResponseHeadPtr head,
|
network::mojom::URLResponseHeadPtr head,
|
||||||
const gin_helper::Dictionary& dict);
|
v8::Local<v8::ArrayBufferView> buffer);
|
||||||
static void StartLoadingString(
|
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
||||||
network::mojom::URLResponseHeadPtr head,
|
|
||||||
const gin_helper::Dictionary& dict,
|
|
||||||
v8::Isolate* isolate,
|
|
||||||
v8::Local<v8::Value> response);
|
|
||||||
static void StartLoadingFile(
|
static void StartLoadingFile(
|
||||||
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
||||||
network::ResourceRequest request,
|
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
||||||
network::mojom::URLResponseHeadPtr head,
|
network::mojom::URLResponseHeadPtr head,
|
||||||
const gin_helper::Dictionary& dict,
|
const network::ResourceRequest& original_request,
|
||||||
v8::Isolate* isolate,
|
const base::FilePath& path,
|
||||||
v8::Local<v8::Value> response);
|
const gin_helper::Dictionary& opts);
|
||||||
static void StartLoadingHttp(
|
static void StartLoadingHttp(
|
||||||
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
||||||
const network::ResourceRequest& original_request,
|
const network::ResourceRequest& original_request,
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
||||||
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
||||||
const gin_helper::Dictionary& dict);
|
const gin_helper::Dictionary& dict);
|
||||||
static void StartLoadingStream(
|
static void StartLoadingStream(
|
||||||
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
|
||||||
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
||||||
|
mojo::PendingReceiver<network::mojom::URLLoader> loader,
|
||||||
network::mojom::URLResponseHeadPtr head,
|
network::mojom::URLResponseHeadPtr head,
|
||||||
const gin_helper::Dictionary& dict);
|
const gin_helper::Dictionary& dict);
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ const fixturesPath = path.resolve(__dirname, 'fixtures');
|
||||||
const registerStringProtocol = protocol.registerStringProtocol;
|
const registerStringProtocol = protocol.registerStringProtocol;
|
||||||
const registerBufferProtocol = protocol.registerBufferProtocol;
|
const registerBufferProtocol = protocol.registerBufferProtocol;
|
||||||
const registerFileProtocol = protocol.registerFileProtocol;
|
const registerFileProtocol = protocol.registerFileProtocol;
|
||||||
const registerHttpProtocol = protocol.registerHttpProtocol;
|
|
||||||
const registerStreamProtocol = protocol.registerStreamProtocol;
|
const registerStreamProtocol = protocol.registerStreamProtocol;
|
||||||
const interceptStringProtocol = protocol.interceptStringProtocol;
|
const interceptStringProtocol = protocol.interceptStringProtocol;
|
||||||
const interceptBufferProtocol = protocol.interceptBufferProtocol;
|
const interceptBufferProtocol = protocol.interceptBufferProtocol;
|
||||||
|
@ -146,366 +145,395 @@ describe('protocol module', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('protocol.registerStringProtocol', () => {
|
for (const [registerStringProtocol, name] of [
|
||||||
it('sends string as response', async () => {
|
[protocol.registerStringProtocol, 'protocol.registerStringProtocol'] as const,
|
||||||
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
[(protocol as any).registerProtocol as typeof protocol.registerStringProtocol, 'protocol.registerProtocol'] as const
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
]) {
|
||||||
expect(r.data).to.equal(text);
|
describe(name, () => {
|
||||||
});
|
it('sends string as response', async () => {
|
||||||
|
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
|
});
|
||||||
|
|
||||||
it('sets Access-Control-Allow-Origin', async () => {
|
it('sets Access-Control-Allow-Origin', async () => {
|
||||||
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
registerStringProtocol(protocolName, (request, callback) => callback(text));
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.equal(text);
|
expect(r.data).to.equal(text);
|
||||||
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends object as response', async () => {
|
it('sends object as response', async () => {
|
||||||
registerStringProtocol(protocolName, (request, callback) => {
|
registerStringProtocol(protocolName, (request, callback) => {
|
||||||
callback({
|
callback({
|
||||||
data: text,
|
data: text,
|
||||||
mimeType: 'text/html'
|
mimeType: 'text/html'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
});
|
});
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(text);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails when sending object other than string', async () => {
|
it('fails when sending object other than string', async () => {
|
||||||
const notAString = () => {};
|
const notAString = () => {};
|
||||||
registerStringProtocol(protocolName, (request, callback) => callback(notAString as any));
|
registerStringProtocol(protocolName, (request, callback) => callback(notAString as any));
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
describe('protocol.registerBufferProtocol', () => {
|
for (const [registerBufferProtocol, name] of [
|
||||||
const buffer = Buffer.from(text);
|
[protocol.registerBufferProtocol, 'protocol.registerBufferProtocol'] as const,
|
||||||
it('sends Buffer as response', async () => {
|
[(protocol as any).registerProtocol as typeof protocol.registerBufferProtocol, 'protocol.registerProtocol'] as const
|
||||||
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
]) {
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
describe(name, () => {
|
||||||
expect(r.data).to.equal(text);
|
const buffer = Buffer.from(text);
|
||||||
});
|
it('sends Buffer as response', async () => {
|
||||||
|
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
|
});
|
||||||
|
|
||||||
it('sets Access-Control-Allow-Origin', async () => {
|
it('sets Access-Control-Allow-Origin', async () => {
|
||||||
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
registerBufferProtocol(protocolName, (request, callback) => callback(buffer));
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
expect(r.data).to.equal(text);
|
expect(r.data).to.equal(text);
|
||||||
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends object as response', async () => {
|
it('sends object as response', async () => {
|
||||||
registerBufferProtocol(protocolName, (request, callback) => {
|
registerBufferProtocol(protocolName, (request, callback) => {
|
||||||
callback({
|
callback({
|
||||||
data: buffer,
|
data: buffer,
|
||||||
mimeType: 'text/html'
|
mimeType: 'text/html'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
expect(r.data).to.equal(text);
|
||||||
expect(r.data).to.equal(text);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails when sending string', async () => {
|
|
||||||
registerBufferProtocol(protocolName, (request, callback) => callback(text as any));
|
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('protocol.registerFileProtocol', () => {
|
|
||||||
const filePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'file1');
|
|
||||||
const fileContent = fs.readFileSync(filePath);
|
|
||||||
const normalPath = path.join(fixturesPath, 'pages', 'a.html');
|
|
||||||
const normalContent = fs.readFileSync(normalPath);
|
|
||||||
|
|
||||||
afterEach(closeAllWindows);
|
|
||||||
|
|
||||||
it('sends file path as response', async () => {
|
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(String(fileContent));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sets Access-Control-Allow-Origin', async () => {
|
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(String(fileContent));
|
|
||||||
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sets custom headers', async () => {
|
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback({
|
|
||||||
path: filePath,
|
|
||||||
headers: { 'X-Great-Header': 'sogreat' }
|
|
||||||
}));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(String(fileContent));
|
|
||||||
expect(r.headers).to.have.property('x-great-header', 'sogreat');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can load iframes with custom protocols', (done) => {
|
|
||||||
registerFileProtocol('custom', (request, callback) => {
|
|
||||||
const filename = request.url.substring(9);
|
|
||||||
const p = path.join(__dirname, 'fixtures', 'pages', filename);
|
|
||||||
callback({ path: p });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const w = new BrowserWindow({
|
if (name !== 'protocol.registerProtocol') {
|
||||||
show: false,
|
it('fails when sending string', async () => {
|
||||||
webPreferences: {
|
registerBufferProtocol(protocolName, (request, callback) => callback(text as any));
|
||||||
nodeIntegration: true,
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
contextIsolation: false
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [registerFileProtocol, name] of [
|
||||||
|
[protocol.registerFileProtocol, 'protocol.registerFileProtocol'] as const,
|
||||||
|
[(protocol as any).registerProtocol as typeof protocol.registerFileProtocol, 'protocol.registerProtocol'] as const
|
||||||
|
]) {
|
||||||
|
describe(name, () => {
|
||||||
|
const filePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'file1');
|
||||||
|
const fileContent = fs.readFileSync(filePath);
|
||||||
|
const normalPath = path.join(fixturesPath, 'pages', 'a.html');
|
||||||
|
const normalContent = fs.readFileSync(normalPath);
|
||||||
|
|
||||||
|
afterEach(closeAllWindows);
|
||||||
|
|
||||||
|
if (name === 'protocol.registerFileProtocol') {
|
||||||
|
it('sends file path as response', async () => {
|
||||||
|
registerFileProtocol(protocolName, (request, callback) => callback(filePath));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(String(fileContent));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
it('sets Access-Control-Allow-Origin', async () => {
|
||||||
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: filePath }));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(String(fileContent));
|
||||||
|
expect(r.headers).to.have.property('access-control-allow-origin', '*');
|
||||||
});
|
});
|
||||||
|
|
||||||
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'iframe-protocol.html'));
|
it('sets custom headers', async () => {
|
||||||
ipcMain.once('loaded-iframe-custom-protocol', () => done());
|
registerFileProtocol(protocolName, (request, callback) => callback({
|
||||||
});
|
|
||||||
|
|
||||||
it.skip('throws an error when custom headers are invalid', (done) => {
|
|
||||||
registerFileProtocol(protocolName, (request, callback) => {
|
|
||||||
expect(() => callback({
|
|
||||||
path: filePath,
|
path: filePath,
|
||||||
headers: { 'X-Great-Header': (42 as any) }
|
headers: { 'X-Great-Header': 'sogreat' }
|
||||||
})).to.throw(Error, 'Value of \'X-Great-Header\' header has to be a string');
|
}));
|
||||||
done();
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(String(fileContent));
|
||||||
|
expect(r.headers).to.have.property('x-great-header', 'sogreat');
|
||||||
});
|
});
|
||||||
ajax(protocolName + '://fake-host').catch(() => {});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sends object as response', async () => {
|
it('can load iframes with custom protocols', (done) => {
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback({ path: filePath }));
|
registerFileProtocol('custom', (request, callback) => {
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
const filename = request.url.substring(9);
|
||||||
expect(r.data).to.equal(String(fileContent));
|
const p = path.join(__dirname, 'fixtures', 'pages', filename);
|
||||||
});
|
callback({ path: p });
|
||||||
|
|
||||||
it('can send normal file', async () => {
|
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(normalPath));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(String(normalContent));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails when sending unexist-file', async () => {
|
|
||||||
const fakeFilePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'not-exist');
|
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(fakeFilePath));
|
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails when sending unsupported content', async () => {
|
|
||||||
registerFileProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('protocol.registerHttpProtocol', () => {
|
|
||||||
it('sends url as response', async () => {
|
|
||||||
const server = http.createServer((req, res) => {
|
|
||||||
expect(req.headers.accept).to.not.equal('');
|
|
||||||
res.end(text);
|
|
||||||
server.close();
|
|
||||||
});
|
|
||||||
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
||||||
|
|
||||||
const port = (server.address() as AddressInfo).port;
|
|
||||||
const url = 'http://127.0.0.1:' + port;
|
|
||||||
registerHttpProtocol(protocolName, (request, callback) => callback({ url }));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(text);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails when sending invalid url', async () => {
|
|
||||||
registerHttpProtocol(protocolName, (request, callback) => callback({ url: 'url' }));
|
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('fails when sending unsupported content', async () => {
|
|
||||||
registerHttpProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
|
||||||
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('works when target URL redirects', async () => {
|
|
||||||
const server = http.createServer((req, res) => {
|
|
||||||
if (req.url === '/serverRedirect') {
|
|
||||||
res.statusCode = 301;
|
|
||||||
res.setHeader('Location', `http://${req.rawHeaders[1]}`);
|
|
||||||
res.end();
|
|
||||||
} else {
|
|
||||||
res.end(text);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
after(() => server.close());
|
|
||||||
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
||||||
|
|
||||||
const port = (server.address() as AddressInfo).port;
|
|
||||||
const url = `${protocolName}://fake-host`;
|
|
||||||
const redirectURL = `http://127.0.0.1:${port}/serverRedirect`;
|
|
||||||
registerHttpProtocol(protocolName, (request, callback) => callback({ url: redirectURL }));
|
|
||||||
|
|
||||||
const r = await ajax(url);
|
|
||||||
expect(r.data).to.equal(text);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can access request headers', (done) => {
|
|
||||||
protocol.registerHttpProtocol(protocolName, (request) => {
|
|
||||||
try {
|
|
||||||
expect(request).to.have.property('headers');
|
|
||||||
done();
|
|
||||||
} catch (e) {
|
|
||||||
done(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
ajax(protocolName + '://fake-host').catch(() => {});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('protocol.registerStreamProtocol', () => {
|
|
||||||
it('sends Stream as response', async () => {
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => callback(getStream()));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(text);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sends object as response', async () => {
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => callback({ data: getStream() }));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(text);
|
|
||||||
expect(r.status).to.equal(200);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sends custom response headers', async () => {
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => callback({
|
|
||||||
data: getStream(3),
|
|
||||||
headers: {
|
|
||||||
'x-electron': ['a', 'b']
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.equal(text);
|
|
||||||
expect(r.status).to.equal(200);
|
|
||||||
expect(r.headers).to.have.property('x-electron', 'a, b');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sends custom status code', async () => {
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => callback({
|
|
||||||
statusCode: 204,
|
|
||||||
data: null as any
|
|
||||||
}));
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.be.empty('data');
|
|
||||||
expect(r.status).to.equal(204);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('receives request headers', async () => {
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => {
|
|
||||||
callback({
|
|
||||||
headers: {
|
|
||||||
'content-type': 'application/json'
|
|
||||||
},
|
|
||||||
data: getStream(5, JSON.stringify(Object.assign({}, request.headers)))
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
const r = await ajax(protocolName + '://fake-host', { headers: { 'x-return-headers': 'yes' } });
|
|
||||||
expect(JSON.parse(r.data)['x-return-headers']).to.equal('yes');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns response multiple response headers with the same name', async () => {
|
const w = new BrowserWindow({
|
||||||
registerStreamProtocol(protocolName, (request, callback) => {
|
show: false,
|
||||||
callback({
|
webPreferences: {
|
||||||
headers: {
|
nodeIntegration: true,
|
||||||
header1: ['value1', 'value2'],
|
contextIsolation: false
|
||||||
header2: 'value3'
|
|
||||||
},
|
|
||||||
data: getStream()
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
// SUBTLE: when the response headers have multiple values it
|
|
||||||
// separates values by ", ". When the response headers are incorrectly
|
|
||||||
// converting an array to a string it separates values by ",".
|
|
||||||
expect(r.headers).to.have.property('header1', 'value1, value2');
|
|
||||||
expect(r.headers).to.have.property('header2', 'value3');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can handle large responses', async () => {
|
|
||||||
const data = Buffer.alloc(128 * 1024);
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => {
|
|
||||||
callback(getStream(data.length, data));
|
|
||||||
});
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.have.lengthOf(data.length);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can handle a stream completing while writing', async () => {
|
|
||||||
function dumbPassthrough () {
|
|
||||||
return new stream.Transform({
|
|
||||||
async transform (chunk, encoding, cb) {
|
|
||||||
cb(null, chunk);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => {
|
|
||||||
callback({
|
|
||||||
statusCode: 200,
|
|
||||||
headers: { 'Content-Type': 'text/plain' },
|
|
||||||
data: getStream(1024 * 1024, Buffer.alloc(1024 * 1024 * 2)).pipe(dumbPassthrough())
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const r = await ajax(protocolName + '://fake-host');
|
|
||||||
expect(r.data).to.have.lengthOf(1024 * 1024 * 2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('can handle next-tick scheduling during read calls', async () => {
|
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'iframe-protocol.html'));
|
||||||
const events = new EventEmitter();
|
ipcMain.once('loaded-iframe-custom-protocol', () => done());
|
||||||
function createStream () {
|
|
||||||
const buffers = [
|
|
||||||
Buffer.alloc(65536),
|
|
||||||
Buffer.alloc(65537),
|
|
||||||
Buffer.alloc(39156)
|
|
||||||
];
|
|
||||||
const e = new stream.Readable({ highWaterMark: 0 });
|
|
||||||
e.push(buffers.shift());
|
|
||||||
e._read = function () {
|
|
||||||
process.nextTick(() => this.push(buffers.shift() || null));
|
|
||||||
};
|
|
||||||
e.on('end', function () {
|
|
||||||
events.emit('end');
|
|
||||||
});
|
|
||||||
return e;
|
|
||||||
}
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => {
|
|
||||||
callback({
|
|
||||||
statusCode: 200,
|
|
||||||
headers: { 'Content-Type': 'text/plain' },
|
|
||||||
data: createStream()
|
|
||||||
});
|
|
||||||
});
|
|
||||||
const hasEndedPromise = emittedOnce(events, 'end');
|
|
||||||
ajax(protocolName + '://fake-host').catch(() => {});
|
|
||||||
await hasEndedPromise;
|
|
||||||
});
|
|
||||||
|
|
||||||
it('destroys response streams when aborted before completion', async () => {
|
|
||||||
const events = new EventEmitter();
|
|
||||||
registerStreamProtocol(protocolName, (request, callback) => {
|
|
||||||
const responseStream = new stream.PassThrough();
|
|
||||||
responseStream.push('data\r\n');
|
|
||||||
responseStream.on('close', () => {
|
|
||||||
events.emit('close');
|
|
||||||
});
|
|
||||||
callback({
|
|
||||||
statusCode: 200,
|
|
||||||
headers: { 'Content-Type': 'text/plain' },
|
|
||||||
data: responseStream
|
|
||||||
});
|
|
||||||
events.emit('respond');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasRespondedPromise = emittedOnce(events, 'respond');
|
it.skip('throws an error when custom headers are invalid', (done) => {
|
||||||
const hasClosedPromise = emittedOnce(events, 'close');
|
registerFileProtocol(protocolName, (request, callback) => {
|
||||||
ajax(protocolName + '://fake-host').catch(() => {});
|
expect(() => callback({
|
||||||
await hasRespondedPromise;
|
path: filePath,
|
||||||
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'fetch.html'));
|
headers: { 'X-Great-Header': (42 as any) }
|
||||||
await hasClosedPromise;
|
})).to.throw(Error, 'Value of \'X-Great-Header\' header has to be a string');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends object as response', async () => {
|
||||||
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: filePath }));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(String(fileContent));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can send normal file', async () => {
|
||||||
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: normalPath }));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(String(normalContent));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails when sending unexist-file', async () => {
|
||||||
|
const fakeFilePath = path.join(fixturesPath, 'test.asar', 'a.asar', 'not-exist');
|
||||||
|
registerFileProtocol(protocolName, (request, callback) => callback({ path: fakeFilePath }));
|
||||||
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails when sending unsupported content', async () => {
|
||||||
|
registerFileProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
||||||
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
for (const [registerHttpProtocol, name] of [
|
||||||
|
[protocol.registerHttpProtocol, 'protocol.registerHttpProtocol'] as const,
|
||||||
|
[(protocol as any).registerProtocol as typeof protocol.registerHttpProtocol, 'protocol.registerProtocol'] as const
|
||||||
|
]) {
|
||||||
|
describe(name, () => {
|
||||||
|
it('sends url as response', async () => {
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
expect(req.headers.accept).to.not.equal('');
|
||||||
|
res.end(text);
|
||||||
|
server.close();
|
||||||
|
});
|
||||||
|
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||||
|
|
||||||
|
const port = (server.address() as AddressInfo).port;
|
||||||
|
const url = 'http://127.0.0.1:' + port;
|
||||||
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url }));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails when sending invalid url', async () => {
|
||||||
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url: 'url' }));
|
||||||
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails when sending unsupported content', async () => {
|
||||||
|
registerHttpProtocol(protocolName, (request, callback) => callback(new Date() as any));
|
||||||
|
await expect(ajax(protocolName + '://fake-host')).to.be.eventually.rejected();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works when target URL redirects', async () => {
|
||||||
|
const server = http.createServer((req, res) => {
|
||||||
|
if (req.url === '/serverRedirect') {
|
||||||
|
res.statusCode = 301;
|
||||||
|
res.setHeader('Location', `http://${req.rawHeaders[1]}`);
|
||||||
|
res.end();
|
||||||
|
} else {
|
||||||
|
res.end(text);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
after(() => server.close());
|
||||||
|
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||||
|
|
||||||
|
const port = (server.address() as AddressInfo).port;
|
||||||
|
const url = `${protocolName}://fake-host`;
|
||||||
|
const redirectURL = `http://127.0.0.1:${port}/serverRedirect`;
|
||||||
|
registerHttpProtocol(protocolName, (request, callback) => callback({ url: redirectURL }));
|
||||||
|
|
||||||
|
const r = await ajax(url);
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can access request headers', (done) => {
|
||||||
|
protocol.registerHttpProtocol(protocolName, (request) => {
|
||||||
|
try {
|
||||||
|
expect(request).to.have.property('headers');
|
||||||
|
done();
|
||||||
|
} catch (e) {
|
||||||
|
done(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [registerStreamProtocol, name] of [
|
||||||
|
[protocol.registerStreamProtocol, 'protocol.registerStreamProtocol'] as const,
|
||||||
|
[(protocol as any).registerProtocol as typeof protocol.registerStreamProtocol, 'protocol.registerProtocol'] as const
|
||||||
|
]) {
|
||||||
|
describe(name, () => {
|
||||||
|
it('sends Stream as response', async () => {
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => callback(getStream()));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends object as response', async () => {
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => callback({ data: getStream() }));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
|
expect(r.status).to.equal(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends custom response headers', async () => {
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => callback({
|
||||||
|
data: getStream(3),
|
||||||
|
headers: {
|
||||||
|
'x-electron': ['a', 'b']
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.equal(text);
|
||||||
|
expect(r.status).to.equal(200);
|
||||||
|
expect(r.headers).to.have.property('x-electron', 'a, b');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends custom status code', async () => {
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => callback({
|
||||||
|
statusCode: 204,
|
||||||
|
data: null as any
|
||||||
|
}));
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.be.empty('data');
|
||||||
|
expect(r.status).to.equal(204);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('receives request headers', async () => {
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => {
|
||||||
|
callback({
|
||||||
|
headers: {
|
||||||
|
'content-type': 'application/json'
|
||||||
|
},
|
||||||
|
data: getStream(5, JSON.stringify(Object.assign({}, request.headers)))
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const r = await ajax(protocolName + '://fake-host', { headers: { 'x-return-headers': 'yes' } });
|
||||||
|
expect(JSON.parse(r.data)['x-return-headers']).to.equal('yes');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns response multiple response headers with the same name', async () => {
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => {
|
||||||
|
callback({
|
||||||
|
headers: {
|
||||||
|
header1: ['value1', 'value2'],
|
||||||
|
header2: 'value3'
|
||||||
|
},
|
||||||
|
data: getStream()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
// SUBTLE: when the response headers have multiple values it
|
||||||
|
// separates values by ", ". When the response headers are incorrectly
|
||||||
|
// converting an array to a string it separates values by ",".
|
||||||
|
expect(r.headers).to.have.property('header1', 'value1, value2');
|
||||||
|
expect(r.headers).to.have.property('header2', 'value3');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle large responses', async () => {
|
||||||
|
const data = Buffer.alloc(128 * 1024);
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => {
|
||||||
|
callback(getStream(data.length, data));
|
||||||
|
});
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.have.lengthOf(data.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle a stream completing while writing', async () => {
|
||||||
|
function dumbPassthrough () {
|
||||||
|
return new stream.Transform({
|
||||||
|
async transform (chunk, encoding, cb) {
|
||||||
|
cb(null, chunk);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => {
|
||||||
|
callback({
|
||||||
|
statusCode: 200,
|
||||||
|
headers: { 'Content-Type': 'text/plain' },
|
||||||
|
data: getStream(1024 * 1024, Buffer.alloc(1024 * 1024 * 2)).pipe(dumbPassthrough())
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const r = await ajax(protocolName + '://fake-host');
|
||||||
|
expect(r.data).to.have.lengthOf(1024 * 1024 * 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle next-tick scheduling during read calls', async () => {
|
||||||
|
const events = new EventEmitter();
|
||||||
|
function createStream () {
|
||||||
|
const buffers = [
|
||||||
|
Buffer.alloc(65536),
|
||||||
|
Buffer.alloc(65537),
|
||||||
|
Buffer.alloc(39156)
|
||||||
|
];
|
||||||
|
const e = new stream.Readable({ highWaterMark: 0 });
|
||||||
|
e.push(buffers.shift());
|
||||||
|
e._read = function () {
|
||||||
|
process.nextTick(() => this.push(buffers.shift() || null));
|
||||||
|
};
|
||||||
|
e.on('end', function () {
|
||||||
|
events.emit('end');
|
||||||
|
});
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => {
|
||||||
|
callback({
|
||||||
|
statusCode: 200,
|
||||||
|
headers: { 'Content-Type': 'text/plain' },
|
||||||
|
data: createStream()
|
||||||
|
});
|
||||||
|
});
|
||||||
|
const hasEndedPromise = emittedOnce(events, 'end');
|
||||||
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
|
await hasEndedPromise;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('destroys response streams when aborted before completion', async () => {
|
||||||
|
const events = new EventEmitter();
|
||||||
|
registerStreamProtocol(protocolName, (request, callback) => {
|
||||||
|
const responseStream = new stream.PassThrough();
|
||||||
|
responseStream.push('data\r\n');
|
||||||
|
responseStream.on('close', () => {
|
||||||
|
events.emit('close');
|
||||||
|
});
|
||||||
|
callback({
|
||||||
|
statusCode: 200,
|
||||||
|
headers: { 'Content-Type': 'text/plain' },
|
||||||
|
data: responseStream
|
||||||
|
});
|
||||||
|
events.emit('respond');
|
||||||
|
});
|
||||||
|
|
||||||
|
const hasRespondedPromise = emittedOnce(events, 'respond');
|
||||||
|
const hasClosedPromise = emittedOnce(events, 'close');
|
||||||
|
ajax(protocolName + '://fake-host').catch(() => {});
|
||||||
|
await hasRespondedPromise;
|
||||||
|
await contents.loadFile(path.join(__dirname, 'fixtures', 'pages', 'fetch.html'));
|
||||||
|
await hasClosedPromise;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe('protocol.isProtocolRegistered', () => {
|
describe('protocol.isProtocolRegistered', () => {
|
||||||
it('returns false when scheme is not registered', () => {
|
it('returns false when scheme is not registered', () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue