mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-09-27 16:08:47 +03:00
Fix #763 - Add support for Headers object
This commit is contained in:
parent
a1644b6775
commit
d5edcbae12
@ -1237,13 +1237,20 @@
|
||||
extensions = undefined;
|
||||
}
|
||||
|
||||
// Convert Headers object into a plain object
|
||||
if (headers && typeof headers.append === FUNC_TYPE) {
|
||||
var kv = {};
|
||||
headers.forEach(function (v, k) { kv[k] = v; });
|
||||
headers = kv;
|
||||
}
|
||||
|
||||
if (!(this instanceof UAParser)) {
|
||||
return new UAParser(ua, extensions, headers).getResult();
|
||||
}
|
||||
|
||||
var userAgent = typeof ua === STR_TYPE ? ua : // Passed user-agent string
|
||||
((NAVIGATOR && NAVIGATOR.userAgent) ? NAVIGATOR.userAgent : // navigator.userAgent
|
||||
(headers && headers[USER_AGENT] ? headers[USER_AGENT] : // User-Agent from passed headers
|
||||
((NAVIGATOR && NAVIGATOR.userAgent) ? NAVIGATOR.userAgent : // navigator.userAgent
|
||||
EMPTY)), // empty string
|
||||
|
||||
httpUACH = new UACHData(headers, true),
|
||||
|
@ -124,3 +124,20 @@ test.describe('withFeatureCheck() tests', () => {
|
||||
expect(uap).toHaveProperty('device.type', 'tablet');
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('request.headers can be passed in form of a Headers object', () => {
|
||||
|
||||
test('Headers automatically converted into a plain key-value object', async ({ page }) => {
|
||||
await page.addInitScript(() => {
|
||||
Object.defineProperty(window, 'req', {
|
||||
value : {
|
||||
headers: new Headers([["User-Agent", "myBrowser/1.0"]])
|
||||
}
|
||||
});
|
||||
});
|
||||
await page.goto(localHtml);
|
||||
// @ts-ignore
|
||||
const uap = await page.evaluate(() => UAParser(req.headers));
|
||||
expect(uap.ua).toBe('myBrowser/1.0');
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user