Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / caps / tests / unit / test_oa_partitionKey_pattern.js
blob5d0625018f2a6a3cfd24ba6740179c12e53fcd81
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 /**
5  * Tests origin attributes partitionKey pattern matching.
6  */
8 "use strict";
10 function testMatch(oa, pattern, shouldMatch = true) {
11   let msg = `Origin attributes should ${
12     shouldMatch ? "match" : "not match"
13   } pattern.`;
14   msg += ` oa: ${JSON.stringify(oa)} - pattern: ${JSON.stringify(pattern)}`;
15   Assert.equal(
16     ChromeUtils.originAttributesMatchPattern(oa, pattern),
17     shouldMatch,
18     msg
19   );
22 function getPartitionKey(scheme, baseDomain, port) {
23   if (!scheme || !baseDomain) {
24     return "";
25   }
26   return `(${scheme},${baseDomain}${port ? `,${port}` : ``})`;
29 function getOAWithPartitionKey(scheme, baseDomain, port, oa = {}) {
30   oa.partitionKey = getPartitionKey(scheme, baseDomain, port);
31   return oa;
34 /**
35  * Tests that an OriginAttributesPattern which is empty or only has an empty
36  * partitionKeyPattern matches any partitionKey.
37  */
38 add_task(async function test_empty_pattern_matches_any() {
39   let list = [
40     getOAWithPartitionKey("https", "example.com"),
41     getOAWithPartitionKey("http", "example.net", 8080),
42     getOAWithPartitionKey(),
43   ];
45   for (let oa of list) {
46     testMatch(oa, {});
47     testMatch(oa, { partitionKeyPattern: {} });
48   }
49 });
51 /**
52  * Tests that if a partitionKeyPattern is passed, but the partitionKey is
53  * invalid, the pattern match will always fail.
54  */
55 add_task(async function test_invalid_pk() {
56   let list = [
57     "()",
58     "(,,)",
59     "(https)",
60     "(https,,)",
61     "(example.com)",
62     "(http,example.com,invalid)",
63     "(http,example.com,8000,1000)",
64   ].map(partitionKey => ({ partitionKey }));
66   for (let oa of list) {
67     testMatch(oa, {});
68     testMatch(oa, { partitionKeyPattern: {} });
69     testMatch(
70       oa,
71       { partitionKeyPattern: { baseDomain: "example.com" } },
72       false
73     );
74     testMatch(oa, { partitionKeyPattern: { scheme: "https" } }, false);
75   }
76 });
78 /**
79  * Tests that if a pattern sets "partitionKey" it takes precedence over "partitionKeyPattern".
80  */
81 add_task(async function test_string_overwrites_pattern() {
82   let oa = getOAWithPartitionKey("https", "example.com", 8080, {
83     userContextId: 2,
84   });
86   testMatch(oa, { partitionKey: oa.partitionKey });
87   testMatch(oa, {
88     partitionKey: oa.partitionKey,
89     partitionKeyPattern: { baseDomain: "example.com" },
90   });
91   testMatch(oa, {
92     partitionKey: oa.partitionKey,
93     partitionKeyPattern: { baseDomain: "example.net" },
94   });
95   testMatch(
96     oa,
97     {
98       partitionKey: getPartitionKey("https", "example.net"),
99       partitionKeyPattern: { scheme: "https", baseDomain: "example.com" },
100     },
101     false
102   );
106  * Tests that we can match parts of a partitionKey by setting
107  * partitionKeyPattern.
108  */
109 add_task(async function test_pattern() {
110   let a = getOAWithPartitionKey("https", "example.com", 8080, {
111     userContextId: 2,
112   });
113   let b = getOAWithPartitionKey("https", "example.com", undefined, {
114     privateBrowsingId: 1,
115   });
117   for (let oa of [a, b]) {
118     // Match
119     testMatch(oa, { partitionKeyPattern: { scheme: "https" } });
120     testMatch(oa, {
121       partitionKeyPattern: { scheme: "https", baseDomain: "example.com" },
122     });
123     testMatch(
124       oa,
125       {
126         partitionKeyPattern: {
127           scheme: "https",
128           baseDomain: "example.com",
129           port: 8080,
130         },
131       },
132       oa == a
133     );
134     testMatch(oa, {
135       partitionKeyPattern: { baseDomain: "example.com" },
136     });
137     testMatch(
138       oa,
139       {
140         partitionKeyPattern: { port: 8080 },
141       },
142       oa == a
143     );
145     // Mismatch
146     testMatch(oa, { partitionKeyPattern: { scheme: "http" } }, false);
147     testMatch(
148       oa,
149       { partitionKeyPattern: { baseDomain: "example.net" } },
150       false
151     );
152     testMatch(oa, { partitionKeyPattern: { port: 8443 } }, false);
153     testMatch(
154       oa,
155       { partitionKeyPattern: { scheme: "https", baseDomain: "example.net" } },
156       false
157     );
158   }