Skip to content

Commit

Permalink
Add test for multiple RouterCondition
Browse files Browse the repository at this point in the history
This CL add the test to ensure multiple RouterConditions in one
`condition` work with "and" condition. All RouterConditions should be
matched.

We don't have RunningStatus, those are tested separately because that
needs an internal API.

Bug: 40943429
Change-Id: Ieeb2ccc03baffe2ea2cfd0a88d4ea091de708ef9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5379636
Reviewed-by: Yoshisato Yanagisawa <yyanagisawa@chromium.org>
Commit-Queue: Shunya Shishido <sisidovski@chromium.org>
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1276082}
  • Loading branch information
sisidovski authored and chromium-wpt-export-bot committed Mar 21, 2024
1 parent b6d4a0e commit 847ab63
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ const routerRules = {
source: 'race-network-and-fetch-handler'
},
],
'multiple-conditions-network': {
condition: {
urlPattern: new URLPattern({search: 'test'}),
requestMode: 'cors',
requestMethod: 'post',
},
source: 'network'
},
'multiple-conditions-with-destination-network' : {
condition: {
urlPattern: new URLPattern({search: 'test'}),
requestDestination: 'style'
},
source: 'network'
}
};

export {routerRules, TEST_CACHE_NAME as cacheName};
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>
Static Router: routers are evaluated with the request method condition.
</title>
<script src="/common/get-host-info.sub.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js">
</script>
<script src="resources/static-router-helpers.sub.js">
</script>
<body>
<script>
const ROUTER_KEY = 'multiple-conditions-network';
const ROUTER_KEY_WITH_DESTINATION =
'multiple-conditions-with-destination-network';
const HTML_FILE = 'resources/simple.html';
const REQUEST_SRC = 'resources/direct.py';

const is_matched = async (worker) => {
const {requests} = await get_info_from_worker(worker);
return requests.length == 0;
}

const appendCSS = async (iwin, src) => {
const promise = new Promise(resolve => {
const link = iwin.document.createElement('link');
link.rel = 'stylesheet';
link.href = src;
iwin.document.head.appendChild(link);
link.onload = () => {
resolve(link);
};
});

return promise;
};

const appendScript = async (iwin, src) => {
const promise = new Promise(resolve => {
const script = iwin.document.createElement('script');
script.src = src;
iwin.document.body.appendChild(script);
script.onload = () => {
resolve(script);
};
});

return promise;
};

iframeTest(HTML_FILE, ROUTER_KEY, async (t, iwin, worker) => {
// Reset the fetch count created by the setup process.
await reset_info_in_worker(worker);
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 0);

// Expected condtion:
// - urlPattern: { search: 'test' }
// - mode: 'cors'
// - method: POST

// Expect match.
let response = await iwin.fetch(`../${REQUEST_SRC}?test`, {mode: 'cors', method: 'post'});
assert_equals(response.status, 200);
assert_true(await is_matched(worker));
await reset_info_in_worker(worker);

// mode: 'no-cors' won't match.
response = await iwin.fetch(`../${REQUEST_SRC}?test`, {mode: 'no-cors', method: 'post'});
assert_false(await is_matched(worker));
await reset_info_in_worker(worker);

// method: GET won't match.
response = await iwin.fetch(`../${REQUEST_SRC}?test`, {mode: 'cors', method: 'get'});
assert_false(await is_matched(worker));
await reset_info_in_worker(worker);

// No seqarch query won't match.
response = await iwin.fetch(`../${REQUEST_SRC}`, {mode: 'cors', method: 'post'});
assert_false(await is_matched(worker));
await reset_info_in_worker(worker);
}, 'Multiple conditions work with `and` operation');

iframeTest(HTML_FILE, ROUTER_KEY_WITH_DESTINATION, async (t, iwin, worker) => {
// Reset the fetch count created by the setup process.
await reset_info_in_worker(worker);
const {requests} = await get_info_from_worker(worker);
assert_equals(requests.length, 0);

// Expected condtion:
// - urlPattern: { search: 'test' }
// - destination: style

// Expect match.
await appendCSS(iwin, `../${REQUEST_SRC}?test`);
assert_true(await is_matched(worker));
await reset_info_in_worker(worker);

// Other request destination won't match.
await appendScript(iwin, `../${REQUEST_SRC}?test`);
assert_false(await is_matched(worker));
await reset_info_in_worker(worker);

// No seqarch query won't match.
await appendCSS(iwin, `../${REQUEST_SRC}`);
assert_false(await is_matched(worker));
await reset_info_in_worker(worker);
}, 'Multiple conditions including requestDestination work with `and` operation');
</script>
</body>

0 comments on commit 847ab63

Please sign in to comment.