Skip to content

Commit

Permalink
Update Sec-CH-UA tests: fix existing tests, and test brand size.
Browse files Browse the repository at this point in the history
As of WICG/ua-client-hints#179,
https://wicg.github.io/ua-client-hints/#user-agent-brand now requires
that a brand in a brand list not be longer than 32 bytes.

This CL adds a new test for the userAgentData.brands interface, and
modifies the existing sec-ch-ua HTTP tests.

Also, the existing sec-ch-ua HTTP tests were updated to more closely
reflect the current state of UA-CH ("UA" is not a valid token, and
"Accept-CH-Lifetime" is not shipping).

Bug: 1263180
Change-Id: Ifa7cbf18d67271e2dd1dc1d485f432d4ca9327fa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3243544
Commit-Queue: Aaron Tagliaboschi <[email protected]>
Auto-Submit: Mike Taylor <[email protected]>
Reviewed-by: Aaron Tagliaboschi <[email protected]>
Cr-Commit-Position: refs/heads/main@{#935138}
  • Loading branch information
miketaylr authored and chromium-wpt-export-bot committed Oct 26, 2021
1 parent 8c690c8 commit c1be1a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
4 changes: 1 addition & 3 deletions client-hints/resources/sec-ch-ua.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
def main(request, response):
ua = request.headers.get(b'sec-ch-ua', b'')
ua = request.headers.get(b'Sec-CH-UA', b'')
response.headers.set(b"Content-Type", b"text/html")
response.headers.set(b"Accept-CH", b"UA")
response.headers.set(b"Accept-CH-Lifetime", b"10")
response.content = b'''
<script>
window.opener.postMessage({ header: '%s' }, "*");
Expand Down
51 changes: 30 additions & 21 deletions client-hints/sec-ch-ua.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,48 @@
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
var minor = "";
promise_test(t => {
var sec_ch_ua_header = "";

function grabSECCHUAHeader(t) {
return new Promise((resolve, reject) => {
var w;
window.onmessage = e => {
try {
assert_not_equals(e.data.header, "", "The `Sec-CH-UA` header is delivered.");
minor = e.data.header;
resolve(e.data.header)
} catch (ex) {
reject(ex);
}
w.close();
resolve();
};
w = window.open("./resources/sec-ch-ua.py");
t.add_cleanup(w.close);
});
}
promise_test(t => {
return grabSECCHUAHeader(t).then(header => {
sec_ch_ua_header = header;
assert_not_equals(sec_ch_ua_header, "", "`Sec-CH-UA` is sent.");
});
}, "Open HTTPS window prior to opt-in: `Sec-CH-UA` header with minor version.")
}, "Open HTTPS window: `Sec-CH-UA` header returned by default.");

promise_test(t => {
return new Promise((resolve, reject) => {
var w;
window.onmessage = e => {
try {
assert_not_equals(e.data.header, "", "The `Sec-CH-UA` header is delivered.");
assert_equals(e.data.header, minor, "The `Sec-CH-UA` header did not change after the opt-in.");
} catch (ex) {
reject(ex);
}
w.close();
resolve();
};
w = window.open("./resources/sec-ch-ua.py");
return grabSECCHUAHeader(t).then(header => {
assert_not_equals(header, "", "The `Sec-CH-UA` header is delivered.");
assert_equals(header, sec_ch_ua_header,
"The `Sec-CH-UA` header did not change between requests.");
});
}, "Open HTTPS window: `Sec-CH-UA` header is consistent across versions.");

promise_test(t => {
return grabSECCHUAHeader(t).then(header => {
assert_true(header.split(", ").every((brand) => {
let brandEnd = brand.indexOf(";v=");
assert_true(brandEnd !== -1,
"A well-formed Sec-CH-UA header must have version (v=) params");
/* 32 + 2, becuase of the extra quotes padding the brand,
e.g. '"lol";v=22"' */
return brandEnd < 34;
}));
});
}, "Open HTTPS window post-opt-in: `Sec-CH-UA` header with minor version.")
}, "Open HTTPS window: No brand in `Sec-CH-UA` header is > than 32 chars.");
</script>
</head>
7 changes: 7 additions & 0 deletions ua-client-hints/useragentdata.https.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// META: title=tests for navigator.userAgentData

test(t => {
const brands = navigator.userAgentData.brands;
assert_true(brands.every(brand => brand.brand.length < 32),
"No brand should be longer than 32 characters.");
});

0 comments on commit c1be1a0

Please sign in to comment.