-
Notifications
You must be signed in to change notification settings - Fork 61
test: add modular unit test suite for core detection engine #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hey-Zayn
wants to merge
2
commits into
HTTPArchive:main
Choose a base branch
from
hey-Zayn:feat/modular-unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,4 @@ Desktop.ini | |
| *.DS_Store | ||
| *.log | ||
| .idea | ||
| /plan | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Reusable technology definitions for tests. | ||
| * Each fixture is a plain object matching the raw JSON schema | ||
| * (pre-setTechnologies format). | ||
| */ | ||
|
|
||
| const technologies = { | ||
| WordPress: { | ||
| cats: [1], | ||
| description: 'A content management system.', | ||
| icon: 'WordPress.svg', | ||
| meta: { generator: 'WordPress\\s([\\d.]+)\\;version:\\1' }, | ||
| cookies: { wp_lang: '' }, | ||
| implies: 'PHP', | ||
| url: 'wordpress\\.com', | ||
| website: 'https://wordpress.org' | ||
| }, | ||
|
|
||
| PHP: { | ||
| cats: [27], | ||
| headers: { 'X-Powered-By': 'php/([\\d.]+)\\;version:\\1' }, | ||
| website: 'https://php.net' | ||
| }, | ||
|
|
||
| jQuery: { | ||
| cats: [59], | ||
| js: { 'jQuery.fn.jquery': '' }, | ||
| scriptSrc: 'jquery-([0-9.]+)\\.js\\;version:\\1', | ||
| website: 'https://jquery.com' | ||
| }, | ||
|
|
||
| Express: { | ||
| cats: [18], | ||
| headers: { 'X-Powered-By': 'Express' }, | ||
| implies: 'Node.js', | ||
| website: 'https://expressjs.com' | ||
| }, | ||
|
|
||
| 'Node.js': { | ||
| cats: [27], | ||
| website: 'https://nodejs.org' | ||
| }, | ||
|
|
||
| Apache: { | ||
| cats: [22], | ||
| excludes: 'Nginx', | ||
| headers: { Server: 'Apache' }, | ||
| website: 'https://httpd.apache.org' | ||
| }, | ||
|
|
||
| Nginx: { | ||
| cats: [22], | ||
| excludes: 'Apache', | ||
| headers: { Server: 'Nginx' }, | ||
| website: 'https://nginx.org' | ||
| }, | ||
|
|
||
| // Technology that requires another technology | ||
| WPTheme: { | ||
| cats: [1], | ||
| requires: 'WordPress', | ||
| dom: { 'link[href*="themes/flavor"]': { exists: '' } }, | ||
| website: 'https://flavor.dev' | ||
| }, | ||
|
|
||
| // Technology that requires a category | ||
| ShopPlugin: { | ||
| cats: [1], | ||
| requiresCategory: 1, | ||
| website: 'https://shop-plugin.example.com' | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Sample category definitions matching src/categories.json structure. | ||
| */ | ||
| const categories = { | ||
| 1: { name: 'CMS', priority: 1, groups: [3] }, | ||
| 10: { name: 'Analytics', priority: 9, groups: [8] }, | ||
| 12: { name: 'JavaScript frameworks', priority: 8, groups: [9] }, | ||
| 18: { name: 'Web frameworks', priority: 7, groups: [9] }, | ||
| 22: { name: 'Web servers', priority: 8, groups: [7] }, | ||
| 27: { name: 'Programming languages', priority: 5, groups: [9] }, | ||
| 59: { name: 'JavaScript libraries', priority: 9, groups: [9] } | ||
| }; | ||
|
|
||
| /** | ||
| * Pick a subset of technologies by name. | ||
| * @param {...string} names | ||
| * @returns {object} Filtered technologies map | ||
| */ | ||
| function pickTechnologies(...names) { | ||
| return names.reduce((acc, name) => { | ||
| if (!technologies[name]) { | ||
| throw new Error(`Fixture technology "${name}" not found`); | ||
| } | ||
| acc[name] = technologies[name]; | ||
| return acc; | ||
| }, {}); | ||
| } | ||
|
|
||
| module.exports = { | ||
| technologies, | ||
| categories, | ||
| pickTechnologies | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| 'use strict'; | ||
|
|
||
| const Wappalyzer = require('../../src/js/wappalyzer'); | ||
|
|
||
| /** | ||
| * Resets all Wappalyzer state to a clean baseline. | ||
| * Call in beforeEach() to ensure test isolation. | ||
| */ | ||
| function resetWappalyzer() { | ||
| Wappalyzer.categories = []; | ||
| Wappalyzer.technologies = []; | ||
| Wappalyzer.requires = []; | ||
| Wappalyzer.categoryRequires = []; | ||
| } | ||
|
|
||
| /** | ||
| * Loads a minimal set of categories that cover the most common | ||
| * category IDs used across technology definitions. | ||
| * Call after resetWappalyzer() when tests need category resolution. | ||
| */ | ||
| function loadDefaultCategories() { | ||
| Wappalyzer.setCategories({ | ||
| 1: { name: 'CMS', priority: 1, groups: [3] }, | ||
| 12: { name: 'JavaScript frameworks', priority: 8, groups: [9] }, | ||
| 18: { name: 'Web frameworks', priority: 7, groups: [9] }, | ||
| 22: { name: 'Web servers', priority: 8, groups: [7] }, | ||
| 27: { name: 'Programming languages', priority: 5, groups: [9] }, | ||
| 59: { name: 'JavaScript libraries', priority: 9, groups: [9] } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Full environment setup: reset + load default categories. | ||
| * Convenience wrapper for most test suites. | ||
| */ | ||
| function setupTestEnv() { | ||
| resetWappalyzer(); | ||
| loadDefaultCategories(); | ||
| } | ||
|
|
||
| /** | ||
| * Helper to build a minimal parsed technology object | ||
| * suitable for analyzeOneToOne / analyzeOneToMany / analyzeManyToMany. | ||
| * | ||
| * @param {string} name - Technology name | ||
| * @param {string} type - Signal type (e.g. 'url', 'headers', 'scriptSrc') | ||
| * @param {*} patterns - Already-parsed patterns for the given type | ||
| * @returns {object} A minimal technology-like object | ||
| */ | ||
| function buildTechnology(name, type, patterns) { | ||
| return { name, [type]: patterns }; | ||
| } | ||
|
|
||
| /** | ||
| * Helper to create a parsed pattern object (matching wappalyzer internal format). | ||
| * | ||
| * @param {string|RegExp} regex - The regex to match against | ||
| * @param {object} [opts] - Optional overrides | ||
| * @param {number} [opts.confidence=100] | ||
| * @param {string} [opts.version=''] | ||
| * @returns {object} A pattern object | ||
| */ | ||
| function buildPattern(regex, { confidence = 100, version = '' } = {}) { | ||
| const re = regex instanceof RegExp ? regex : new RegExp(regex, 'i'); | ||
| return { regex: re, confidence, version, value: re.source }; | ||
| } | ||
|
|
||
| module.exports = { | ||
| Wappalyzer, | ||
| resetWappalyzer, | ||
| loadDefaultCategories, | ||
| setupTestEnv, | ||
| buildTechnology, | ||
| buildPattern | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| 'use strict'; | ||
|
|
||
| const { | ||
| Wappalyzer, | ||
| setupTestEnv, | ||
| buildTechnology, | ||
| buildPattern | ||
| } = require('../helpers/setup'); | ||
| const { pickTechnologies } = require('../helpers/fixtures'); | ||
|
|
||
| describe('Wappalyzer.analyzeOneToOne', () => { | ||
| test('detects matching pattern', () => { | ||
| const tech = buildTechnology('Test', 'url', [ | ||
| buildPattern(/example\.com/i) | ||
| ]); | ||
| const r = Wappalyzer.analyzeOneToOne(tech, 'url', 'https://example.com'); | ||
| expect(r).toHaveLength(1); | ||
| expect(r[0].technology.name).toBe('Test'); | ||
| }); | ||
|
|
||
| test('returns empty for non-match', () => { | ||
| const tech = buildTechnology('Test', 'url', [buildPattern(/nope/i)]); | ||
| expect( | ||
| Wappalyzer.analyzeOneToOne(tech, 'url', 'https://x.com') | ||
| ).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('extracts version', () => { | ||
| const tech = buildTechnology('jQ', 'scriptSrc', [ | ||
| buildPattern(/jquery-([0-9.]+)\.js/i, { version: '\\1' }) | ||
| ]); | ||
| const r = Wappalyzer.analyzeOneToOne(tech, 'scriptSrc', 'jquery-3.6.0.js'); | ||
| expect(r[0].version).toBe('3.6.0'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Wappalyzer.analyzeOneToMany', () => { | ||
| test('matches against array of values', () => { | ||
| const tech = buildTechnology('jQ', 'scriptSrc', [buildPattern(/jquery/i)]); | ||
| const r = Wappalyzer.analyzeOneToMany(tech, 'scriptSrc', [ | ||
| 'react.js', | ||
| 'jquery.min.js' | ||
| ]); | ||
| expect(r).toHaveLength(1); | ||
| }); | ||
|
|
||
| test('returns empty for no matches', () => { | ||
| const tech = buildTechnology('T', 'scriptSrc', [buildPattern(/nope/i)]); | ||
| expect( | ||
| Wappalyzer.analyzeOneToMany(tech, 'scriptSrc', ['a.js']) | ||
| ).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('handles empty items', () => { | ||
| const tech = buildTechnology('T', 'scriptSrc', [buildPattern(/x/i)]); | ||
| expect(Wappalyzer.analyzeOneToMany(tech, 'scriptSrc', [])).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Wappalyzer.analyzeManyToMany', () => { | ||
| test('matches keyed patterns against keyed values', () => { | ||
| const tech = buildTechnology('WP', 'headers', { | ||
| 'x-powered-by': [buildPattern(/wordpress/i)] | ||
| }); | ||
| const r = Wappalyzer.analyzeManyToMany(tech, 'headers', { | ||
| 'x-powered-by': ['WordPress 5.9'] | ||
| }); | ||
| expect(r).toHaveLength(1); | ||
| }); | ||
|
|
||
| test('returns empty when key missing from items', () => { | ||
| const tech = buildTechnology('T', 'headers', { | ||
| 'x-custom': [buildPattern(/val/i)] | ||
| }); | ||
| expect(Wappalyzer.analyzeManyToMany(tech, 'headers', {})).toHaveLength(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Wappalyzer.analyze (full pipeline)', () => { | ||
| beforeEach(setupTestEnv); | ||
|
|
||
| test('detects from URL pattern', () => { | ||
| Wappalyzer.setTechnologies(pickTechnologies('WordPress', 'PHP')); | ||
| const d = Wappalyzer.analyze({ url: 'https://my.wordpress.com/blog' }); | ||
| expect(d.length).toBeGreaterThanOrEqual(1); | ||
| }); | ||
|
|
||
| test('detects from meta generator with version', () => { | ||
| Wappalyzer.setTechnologies(pickTechnologies('WordPress', 'PHP')); | ||
| const d = Wappalyzer.analyze({ meta: { generator: ['WordPress 6.2'] } }); | ||
| expect(d.length).toBeGreaterThanOrEqual(1); | ||
| expect(d[0].version).toBe('6.2'); | ||
| }); | ||
|
|
||
| test('detects from headers', () => { | ||
| Wappalyzer.setTechnologies(pickTechnologies('Express', 'Node.js')); | ||
| const d = Wappalyzer.analyze({ headers: { 'x-powered-by': ['Express'] } }); | ||
| expect(d.length).toBeGreaterThanOrEqual(1); | ||
| }); | ||
|
|
||
| test('detects from cookies', () => { | ||
| Wappalyzer.setTechnologies(pickTechnologies('WordPress', 'PHP')); | ||
| const d = Wappalyzer.analyze({ cookies: { wp_lang: ['en_US'] } }); | ||
| expect(d.length).toBeGreaterThanOrEqual(1); | ||
| }); | ||
|
|
||
| test('returns empty for no matches', () => { | ||
| Wappalyzer.setTechnologies(pickTechnologies('WordPress', 'PHP')); | ||
| expect(Wappalyzer.analyze({ url: 'https://example.com' })).toHaveLength(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hey-Zayn please use v24