[ua-client-hints] Refactor UAClientHints

This commit is contained in:
Faisal Salman
2023-08-26 04:55:07 +07:00
parent f538018f8e
commit 1522691426
13 changed files with 466 additions and 311 deletions

View File

@@ -1,5 +1,5 @@
const { isFrozenUA, unfreezeUA } = require('@ua-parser-js/user-agent-helpers');
const { UACHParser } = require('@ua-parser-js/client-hints-helpers');
const { UAClientHints } = require('@ua-parser-js/ua-client-hints');
const assert = require('assert');
describe('isFrozenUA()', () => {
@@ -38,12 +38,12 @@ describe('isFrozenUA()', () => {
const headers = {
'sec-ch-ua' : '"Chromium";v="93", "Google Chrome";v="93", " Not;A Brand";v="99"',
'sec-ch-ua-full-version-list' : '"Chromium";v="93.0.1.2", "Google Chrome";v="93.0.1.2", " Not;A Brand";v="99.0.1.2"',
'sec-ch-ua-arch' : 'arm',
'sec-ch-ua-bitness' : '64',
'sec-ch-ua-arch' : '"arm"',
'sec-ch-ua-bitness' : '"64"',
'sec-ch-ua-mobile' : '?1',
'sec-ch-ua-model' : 'Pixel 99',
'sec-ch-ua-platform' : 'Linux',
'sec-ch-ua-platform-version' : '13',
'sec-ch-ua-model' : '"Pixel 99"',
'sec-ch-ua-platform' : '"Linux"',
'sec-ch-ua-platform-version' : '"13"',
'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
};
@@ -54,9 +54,13 @@ describe('unfreezeUA()', () => {
});
});
describe('UACHParser()', () => {
describe('Parse CH Headers', () => {
it('parse client hints HTTP headers (sec-ch-ua) into a client hints-like JavaScript object', () => {
assert.deepEqual(UACHParser(headers), {
assert.deepEqual(new UAClientHints().setUAData(headers).getUAData(['architecture', 'bitness']), {
"architecture": "arm",
"bitness": "64"
});
assert.deepEqual(new UAClientHints().setUAData(headers).getUAData(), {
"architecture": "arm",
"bitness": "64",
"brands": [
@@ -69,7 +73,7 @@ describe('UACHParser()', () => {
"version": "93"
},
{
"brand": " Not;A Brand",
"brand": "Not;A Brand",
"version": "99"
}
],
@@ -83,14 +87,16 @@ describe('UACHParser()', () => {
"version": "93.0.1.2"
},
{
"brand": " Not;A Brand",
"brand": "Not;A Brand",
"version": "99.0.1.2"
}
],
"formFactor": null,
"mobile": true,
"model": "Pixel 99",
"platform": "Linux",
"platformVersion": "13"
"platformVersion": "13",
"wow64": null
});
});
});