mirror of
https://github.com/faisalman/ua-parser-js.git
synced 2025-10-01 17:57:44 +03:00
Revive the extensive list of MediaPlayers regexes by @leofiore as an Extension
(Original commit reference: 3fa1fe9f70
)
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
const fs = require('fs');
|
||||
const assert = require('assert');
|
||||
const safeRegex = require('safe-regex');
|
||||
const parseJS = require('@babel/parser').parse;
|
||||
const traverse = require('@babel/traverse').default;
|
||||
const safe = require('safe-regex');
|
||||
const UAParser = require('ua-parser-js');
|
||||
const { Bots, Emails, CLI } = require('ua-parser-js/extensions');
|
||||
const Ext = require('ua-parser-js/extensions');
|
||||
|
||||
describe('Bots', () => {
|
||||
it('Can detect bots', () => {
|
||||
@@ -14,23 +17,53 @@ describe('Bots', () => {
|
||||
const outlook = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; Microsoft Outlook 16.0.9126; Microsoft Outlook 16.0.9126; ms-office; MSOffice 16)';
|
||||
const thunderbird = 'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0';
|
||||
|
||||
const botParser = new UAParser(Bots);
|
||||
const botParser = new UAParser(Ext.Bots);
|
||||
assert.deepEqual(botParser.setUA(googleBot).getBrowser(), {name: "Googlebot-Video", version: "1.0", major: "1", type: "bot"});
|
||||
assert.deepEqual(botParser.setUA(msnBot).getBrowser(), {name: "msnbot-media", version: "1.1", major: "1", type: "bot"});
|
||||
assert.deepEqual(botParser.setUA(bingPreview).getBrowser(), {name: "BingPreview", version: "1.0b", major: "1", type: "bot"});
|
||||
assert.deepEqual(botParser.setUA(opera).getBrowser(), {name: "Opera", version: "8.5", major: "8"});
|
||||
|
||||
// try merging Bots & CLIs
|
||||
const botsAndCLIs = { browser : [...Bots.browser, ...CLI.browser]};
|
||||
const botsAndCLIs = { browser : [...Ext.Bots.browser, ...Ext.CLIs.browser]};
|
||||
const botsAndCLIsParser = new UAParser(botsAndCLIs);
|
||||
assert.deepEqual(botsAndCLIsParser.setUA(wget).getBrowser(), {name: "Wget", version: "1.21.1", major: "1", type:"cli"});
|
||||
assert.deepEqual(botsAndCLIsParser.setUA(facebookBot).getBrowser(), {name: "FacebookBot", version: "1.0", major: "1", type:"bot"});
|
||||
|
||||
const emailParser = new UAParser(Emails);
|
||||
const emailParser = new UAParser(Ext.Emails);
|
||||
assert.deepEqual(emailParser.setUA(outlook).getBrowser(), {name: "Microsoft Outlook", version: "16.0.9126", major: "16", type: "email"});
|
||||
assert.deepEqual(emailParser.setUA(thunderbird).getBrowser(), {name: "Thunderbird", version: "78.13.0", major: "78", type: "email"});
|
||||
});
|
||||
});
|
||||
|
||||
// TODO : move test spec to JSON file
|
||||
// TODO : check for safe-regex
|
||||
|
||||
describe('Testing regexes', () => {
|
||||
|
||||
let regexes;
|
||||
|
||||
before('Read main js file', () => {
|
||||
let code = fs.readFileSync('src/extensions/ua-parser-extensions.js', 'utf8').toString();
|
||||
let ast = parseJS(code, { sourceType: 'script' });
|
||||
regexes = [];
|
||||
traverse(ast, {
|
||||
RegExpLiteral: (path) => {
|
||||
regexes.push(path.node.pattern);
|
||||
}
|
||||
});
|
||||
if (regexes.length === 0) {
|
||||
throw new Error('Regexes cannot be empty!');
|
||||
}
|
||||
});
|
||||
|
||||
describe('Begin testing', () => {
|
||||
it('all regexes in extension file', () => {
|
||||
regexes.forEach(regex => {
|
||||
describe('Test against `safe-regex` : ' + regex, () => {
|
||||
it('should be safe from potentially vulnerable regex', () => {
|
||||
assert.strictEqual(safe(regex), true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user