Skip to content

Commit

Permalink
Fix #763 - Add support for Headers object
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalman committed Nov 9, 2024
1 parent a1644b6 commit d5edcba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/main/ua-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1236,14 +1236,21 @@
headers = extensions; // case UAParser(ua, headers)
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),
Expand Down
19 changes: 18 additions & 1 deletion test/playwright-test-main.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,21 @@ test.describe('withFeatureCheck() tests', () => {
expect(uap).toHaveProperty('device.model', 'iPad');
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');
});
});

0 comments on commit d5edcba

Please sign in to comment.