Bug 1406763 - Refactor xpcshell proxy tests for fromJson and toJson. r=ato

To ensure that both methods are working as expected, they cannot be
used in combination for assertions.

MozReview-Commit-ID: 57jYmPejEq0

--HG--
extra : rebase_source : 59ffe0e20cab92535270e3ed3139a9439dc24107
This commit is contained in:
Henrik Skupin 2017-10-11 00:26:22 +02:00
Родитель b5ddfae9aa
Коммит 3fda7dc171
1 изменённых файлов: 67 добавлений и 28 удалений

Просмотреть файл

@ -187,7 +187,13 @@ add_test(function test_Proxy_toJSON() {
let p = new session.Proxy(); let p = new session.Proxy();
deepEqual(p.toJSON(), {}); deepEqual(p.toJSON(), {});
// manual // autoconfig url
p = new session.Proxy();
p.proxyType = "pac";
p.proxyAutoconfigUrl = "foo";
deepEqual(p.toJSON(), {proxyType: "pac", proxyAutoconfigUrl: "foo"});
// manual proxy
p = new session.Proxy(); p = new session.Proxy();
p.proxyType = "manual"; p.proxyType = "manual";
deepEqual(p.toJSON(), {proxyType: "manual"}); deepEqual(p.toJSON(), {proxyType: "manual"});
@ -195,36 +201,37 @@ add_test(function test_Proxy_toJSON() {
for (let proxy of ["ftpProxy", "httpProxy", "sslProxy", "socksProxy"]) { for (let proxy of ["ftpProxy", "httpProxy", "sslProxy", "socksProxy"]) {
let expected = {proxyType: "manual"} let expected = {proxyType: "manual"}
let manual = new session.Proxy(); p = new session.Proxy();
manual.proxyType = "manual"; p.proxyType = "manual";
if (proxy == "socksProxy") { if (proxy == "socksProxy") {
manual.socksVersion = 5; p.socksVersion = 5;
expected.socksVersion = 5; expected.socksVersion = 5;
} }
// without port // without port
manual[proxy] = "foo"; p[proxy] = "foo";
expected[proxy] = "foo" expected[proxy] = "foo"
deepEqual(manual.toJSON(), expected); deepEqual(p.toJSON(), expected);
// with port // with port
manual[proxy] = "foo"; p[proxy] = "foo";
manual[`${proxy}Port`] = 0; p[`${proxy}Port`] = 0;
expected[proxy] = "foo:0"; expected[proxy] = "foo:0";
deepEqual(manual.toJSON(), expected); deepEqual(p.toJSON(), expected);
manual[`${proxy}Port`] = 42; p[`${proxy}Port`] = 42;
expected[proxy] = "foo:42" expected[proxy] = "foo:42"
deepEqual(manual.toJSON(), expected); deepEqual(p.toJSON(), expected);
} }
run_next_test(); run_next_test();
}); });
add_test(function test_Proxy_fromJSON() { add_test(function test_Proxy_fromJSON() {
deepEqual({}, session.Proxy.fromJSON(undefined).toJSON()); let p = new session.Proxy();
deepEqual({}, session.Proxy.fromJSON(null).toJSON()); deepEqual(p, session.Proxy.fromJSON(undefined));
deepEqual(p, session.Proxy.fromJSON(null));
for (let typ of [true, 42, "foo", []]) { for (let typ of [true, 42, "foo", []]) {
Assert.throws(() => session.Proxy.fromJSON(typ), InvalidArgumentError); Assert.throws(() => session.Proxy.fromJSON(typ), InvalidArgumentError);
@ -235,8 +242,22 @@ add_test(function test_Proxy_fromJSON() {
Assert.throws(() => session.Proxy.fromJSON({proxyType: "foo"}), Assert.throws(() => session.Proxy.fromJSON({proxyType: "foo"}),
InvalidArgumentError); InvalidArgumentError);
// manual // autoconfig url
session.Proxy.fromJSON({proxyType: "manual"}); for (let url of [true, 42, [], {}]) {
Assert.throws(() => session.Proxy.fromJSON(
{proxyType: "pac", proxyAutoconfigUrl: url}), /InvalidArgumentError/);
}
p = new session.Proxy();
p.proxyType = "pac";
p.proxyAutoconfigUrl = "foo";
deepEqual(p,
session.Proxy.fromJSON({proxyType: "pac", proxyAutoconfigUrl: "foo"}));
// manual proxy
p = new session.Proxy();
p.proxyType = "manual";
deepEqual(p, session.Proxy.fromJSON({proxyType: "manual"}));
for (let proxy of ["httpProxy", "sslProxy", "ftpProxy", "socksProxy"]) { for (let proxy of ["httpProxy", "sslProxy", "ftpProxy", "socksProxy"]) {
let manual = {proxyType: "manual"}; let manual = {proxyType: "manual"};
@ -250,19 +271,31 @@ add_test(function test_Proxy_fromJSON() {
InvalidArgumentError); InvalidArgumentError);
} }
let expected = {"proxyType": "manual"}; p = new session.Proxy();
p.proxyType = "manual";
if (proxy == "socksProxy") { if (proxy == "socksProxy") {
manual.socksVersion = 5; manual.socksVersion = 5;
expected.socksVersion = 5; p.socksVersion = 5;
} }
// valid proxy hosts with port let host_map = {
for (let host of ["foo:1", "foo:80", "foo:443", "foo:65535", "foo:1": {hostname: "foo", port: 1},
"127.0.0.1:42", "[2001:db8::1]:42"]) { "foo:21": {hostname: "foo", port: 21},
manual[proxy] = host; "foo:80": {hostname: "foo", port: 80},
expected[proxy] = host; "foo:443": {hostname: "foo", port: 443},
"foo:65535": {hostname: "foo", port: 65535},
"127.0.0.1:42": {hostname: "127.0.0.1", port: 42},
"[2001:db8::1]:42": {hostname: "[2001:db8::1]", port: "42"},
};
deepEqual(expected, session.Proxy.fromJSON(manual).toJSON()); // valid proxy hosts with port
for (let host in host_map) {
manual[proxy] = host;
p[`${proxy}`] = host_map[host]["hostname"];
p[`${proxy}Port`] = host_map[host]["port"];
deepEqual(p, session.Proxy.fromJSON(manual));
} }
// Without a port the default port of the scheme is used // Without a port the default port of the scheme is used
@ -270,15 +303,17 @@ add_test(function test_Proxy_fromJSON() {
manual[proxy] = host; manual[proxy] = host;
// For socks no default port is available // For socks no default port is available
p[proxy] = `foo`;
if (proxy === "socksProxy") { if (proxy === "socksProxy") {
expected[proxy] = `foo`; p[`${proxy}Port`] = null;
} else { } else {
let default_ports = {"ftpProxy": 21, "httpProxy": 80, let default_ports = {"ftpProxy": 21, "httpProxy": 80,
"sslProxy": 443}; "sslProxy": 443};
expected[proxy] = `foo:${default_ports[proxy]}`; p[`${proxy}Port`] = default_ports[proxy];
} }
deepEqual(expected, session.Proxy.fromJSON(manual).toJSON());
deepEqual(p, session.Proxy.fromJSON(manual));
} }
} }
@ -296,9 +331,13 @@ add_test(function test_Proxy_fromJSON() {
} }
// valid noProxy // valid noProxy
for (let noProxy of [[], ["foo"], ["foo", "bar"]]) { p = new session.Proxy();
p.proxyType = "manual";
for (let noProxy of [[], ["foo"], ["foo", "bar"],
["127.0.0.1"], ["[2001:db8::1"]]) {
let manual = {proxyType: "manual", "noProxy": noProxy} let manual = {proxyType: "manual", "noProxy": noProxy}
deepEqual(manual, session.Proxy.fromJSON(manual).toJSON()); p.noProxy = noProxy;
deepEqual(p, session.Proxy.fromJSON(manual));
} }
run_next_test(); run_next_test();