0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 09:16:49 +01:00
posthog/jest.setup.fetch-mock.js
Michael Matloka 1b3d208d2e
Add built-in GeoIP capabilities (#247)
* Add @maxmind/geoip2-node as dependency

* Add Server.geoIp with downloaded and cached MMDB

* Inject GeoIP extension into plugin meta

* Upgrade plugin-scaffold

* Disable MMDB fetching in tests

* Update README.md

* Fix "plugin meta has what it should have"

* Fix imports

* Use mmdb.posthog.net

* Rework MMDB approach for plugin attachments and Brotli

* Run prettier

* Improve concurrency handling and restructure files

* Add periodic in-flight MMDB staleness check with no-interrupt update

* Polish up staleness check

* Fix schedule

* Gracefully handle airgapped systems

* Update plugins.test.ts

* Update README.md

* Roll back unrelated config updates

* Add tests

* Fix plugin attachments in tests
2021-03-26 03:22:18 +01:00

28 lines
1.2 KiB
JavaScript

const { readFileSync } = require('fs')
const { DateTime } = require('luxon')
const { join } = require('path')
jest.mock('node-fetch', () => {
const responsesToUrls = {
'https://google.com/results.json?query=fetched': { count: 2, query: 'bla', results: [true, true] },
'https://mmdb.posthog.net/': readFileSync(join(__dirname, 'tests', 'assets', 'GeoLite2-City-Test.mmdb.br')),
}
const headersToUrls = {
'https://mmdb.posthog.net/': new Map([
['content-type', 'vnd.maxmind.maxmind-db'],
['content-disposition', `attachment; filename="GeoLite2-City-${DateTime.local().toISODate()}.mmdb"`],
]),
}
return jest.fn(
(url) =>
new Promise((resolve) =>
resolve({
buffer: () => new Promise((resolve) => resolve(responsesToUrls[url]) || Buffer.from('fetchmock')),
json: () => new Promise((resolve) => resolve(responsesToUrls[url]) || { fetch: 'mock' }),
text: () => new Promise((resolve) => resolve(JSON.stringify(responsesToUrls[url])) || 'fetchmock'),
headers: headersToUrls[url],
})
)
)
})