Copy and add axios-cached-dns-resolve test

This commit is contained in:
Louis Lam 2022-05-04 13:16:22 +08:00
parent d0546afe71
commit 3700b16c5b
3 changed files with 2478 additions and 125 deletions

2407
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,7 @@
"start-server": "node server/server.js",
"start-server-dev": "cross-env NODE_ENV=development node server/server.js",
"build": "vite build --config ./config/vite.config.js",
"test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --test",
"test": "ava test/axios-cached-dns-resolve-test.js -timeout=2m && node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --test",
"test-with-build": "npm run build && npm test",
"jest": "node test/prepare-jest.js && npm run jest-frontend && npm run jest-backend",
"jest-frontend": "cross-env TEST_FRONTEND=1 jest --config=./config/jest-frontend.config.js",
@ -133,15 +133,18 @@
"@vitejs/plugin-vue": "~1.9.4",
"@vue/compiler-sfc": "~3.2.31",
"aedes": "^0.46.3",
"ava": "^3.15.0",
"babel-plugin-rewire": "~1.2.0",
"concurrently": "^7.1.0",
"core-js": "~3.18.3",
"cross-env": "~7.0.3",
"delay": "^5.0.0",
"dns2": "~2.0.1",
"eslint": "~8.14.0",
"eslint-plugin-vue": "~8.7.1",
"jest": "~27.2.5",
"jest-puppeteer": "~6.0.3",
"lru-cache": "^7.7.1",
"npm-check-updates": "^12.5.9",
"postcss-html": "^1.3.1",
"puppeteer": "~13.1.3",

View File

@ -0,0 +1,191 @@
/*
* From: https://github.com/tcollinsworth/axios-cached-dns-resolve/blob/master/__tests__/test.js
* But it converted to CommonJS
*/
const ava = require("ava");
const delay = require("delay");
const LRUCache = require("lru-cache");
const axios = require("axios");
const axiosCachingDns = require("esm")(module)("axios-cached-dns-resolve");
const test = ava.serial
let axiosClient
test.beforeEach(() => {
axiosCachingDns.config.dnsTtlMs = 1000
axiosCachingDns.config.dnsIdleTtlMs = 5000
axiosCachingDns.config.cacheGraceExpireMultiplier = 2
axiosCachingDns.config.backgroundScanMs = 100
axiosCachingDns.cacheConfig.ttl = (axiosCachingDns.config.dnsTtlMs * axiosCachingDns.config.cacheGraceExpireMultiplier)
axiosCachingDns.config.cache = new LRUCache(axiosCachingDns.cacheConfig)
axiosClient = axios.create({
timeout: 5000,
// maxRedirects: 0,
})
axiosCachingDns.registerInterceptor(axiosClient)
axiosCachingDns.startBackgroundRefresh()
axiosCachingDns.startPeriodicCachePrune()
})
test.after.always(() => {
axiosCachingDns.config.cache.clear()
})
test('query google with baseURL and relative url', async (t) => {
axiosCachingDns.registerInterceptor(axios)
const { data } = await axios.get('/finance', {
baseURL: 'http://www.google.com',
// headers: { Authorization: `Basic ${basicauth}` },
})
t.truthy(data)
t.is(1, axiosCachingDns.getStats().dnsEntries)
t.is(1, axiosCachingDns.getStats().misses)
})
test('query google caches and after idle delay uncached', async (t) => {
const resp = await axiosClient.get('http://amazon.com')
t.truthy(resp.data)
t.truthy(axiosCachingDns.config.cache.get('amazon.com'))
await delay(6000)
t.falsy(axiosCachingDns.config.cache.get('amazon.com'))
const expectedStats = {
dnsEntries: 0,
// refreshed: 4, variable
hits: 0,
misses: 2,
idleExpired: 1,
errors: 0,
lastError: 0,
lastErrorTs: 0,
}
const stats = axiosCachingDns.getStats()
delete stats.refreshed
t.deepEqual(expectedStats, stats)
})
test('query google caches and refreshes', async (t) => {
await axiosClient.get('http://amazon.com')
const { updatedTs } = axiosCachingDns.config.cache.get('amazon.com')
const timeoutTime = Date.now() + 5000
// eslint-disable-next-line no-constant-condition
while (true) {
const dnsEntry = axiosCachingDns.config.cache.get('amazon.com')
if (!dnsEntry) t.fail('dnsEntry missing or expired')
// console.log(dnsEntry)
if (updatedTs !== dnsEntry.updatedTs) break
if (Date.now() > timeoutTime) t.fail()
// eslint-disable-next-line no-await-in-loop
await delay(10)
}
const expectedStats = {
dnsEntries: 1,
// refreshed: 5, variable
hits: 0,
misses: 3,
idleExpired: 1,
errors: 0,
lastError: 0,
lastErrorTs: 0,
}
const stats = axiosCachingDns.getStats()
delete stats.refreshed
t.deepEqual(expectedStats, stats)
})
test('query two services, caches and after one idle delay uncached', async (t) => {
await axiosClient.get('http://amazon.com')
await axiosClient.get('http://microsoft.com')
const { lastUsedTs } = axiosCachingDns.config.cache.get('microsoft.com')
t.is(1, axiosCachingDns.config.cache.get('microsoft.com').nextIdx)
await axiosClient.get('http://microsoft.com')
t.is(2, axiosCachingDns.config.cache.get('microsoft.com').nextIdx)
t.truthy(lastUsedTs < axiosCachingDns.config.cache.get('microsoft.com').lastUsedTs)
t.is(2, axiosCachingDns.config.cache.size)
await axiosClient.get('http://microsoft.com')
t.is(3, axiosCachingDns.config.cache.get('microsoft.com').nextIdx)
t.falsy(lastUsedTs === axiosCachingDns.config.cache.get('microsoft.com').lastUsedTs)
t.is(2, axiosCachingDns.config.cache.size)
await delay(4000)
t.is(1, axiosCachingDns.config.cache.size)
await delay(2000)
t.is(0, axiosCachingDns.config.cache.size)
const expectedStats = {
dnsEntries: 0,
// refreshed: 17, variable
hits: 2,
misses: 5,
idleExpired: 3,
errors: 0,
lastError: 0,
lastErrorTs: 0,
}
const stats = axiosCachingDns.getStats()
delete stats.refreshed
t.deepEqual(expectedStats, stats)
})
test('validate axios config not altered', async (t) => {
const baseURL = 'http://microsoft.com'
const axiosConfig = { baseURL }
const custAxiosClient = axios.create(axiosConfig)
axiosCachingDns.registerInterceptor(custAxiosClient)
await custAxiosClient.get('/')
t.is(baseURL, axiosConfig.baseURL)
await custAxiosClient.get('/')
t.is(baseURL, axiosConfig.baseURL)
})
test('validate axios get config not altered', async (t) => {
const url = 'http://microsoft.com'
const custAxiosClient = axios.create()
const reqConfig = {
method: 'get',
url,
}
axiosCachingDns.registerInterceptor(custAxiosClient)
await custAxiosClient.get(url, reqConfig)
t.is(url, reqConfig.url)
await custAxiosClient.get(url, reqConfig)
t.is(url, reqConfig.url)
})
test('validate axios request config not altered', async (t) => {
const url = 'http://microsoft.com'
const custAxiosClient = axios.create()
const reqConfig = {
method: 'get',
url,
}
axiosCachingDns.registerInterceptor(custAxiosClient)
await custAxiosClient.request(reqConfig)
t.is(url, reqConfig.url)
await custAxiosClient.request(reqConfig)
t.is(url, reqConfig.url)
})