MCPcopy
hub / github.com/nodejs/undici / cacheStoreTests

Function cacheStoreTests

test/cache-interceptor/cache-store-test-utils.js:16–390  ·  view source on GitHub ↗

* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore * * @param {{ new(...any): CacheStore }} CacheStore * @param {object} [options] * @param {boolean} [options.skip]

(CacheStore, options)

Source from the content-addressed store, hash-verified

14 * @param {boolean} [options.skip]
15 */
16function cacheStoreTests (CacheStore, options) {
17 describe(CacheStore.prototype.constructor.name, () => {
18 test('matches interface', options, () => {
19 equal(typeof CacheStore.prototype.get, 'function')
20 equal(typeof CacheStore.prototype.createWriteStream, 'function')
21 equal(typeof CacheStore.prototype.delete, 'function')
22 })
23
24 test('caches request', options, async () => {
25 /**
26 * @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
27 */
28 const key = {
29 origin: 'localhost',
30 path: '/',
31 method: 'GET',
32 headers: {}
33 }
34
35 /**
36 * @type {import('../../types/cache-interceptor.d.ts').default.CacheValue}
37 */
38 const value = {
39 statusCode: 200,
40 statusMessage: '',
41 headers: { foo: 'bar' },
42 cacheControlDirectives: {},
43 cachedAt: Date.now(),
44 staleAt: Date.now() + 10000,
45 deleteAt: Date.now() + 20000
46 }
47
48 const body = [Buffer.from('asd'), Buffer.from('123')]
49
50 const store = new CacheStore()
51
52 // Sanity check
53 equal(await store.get(key), undefined)
54
55 // Write response to store
56 {
57 const writable = store.createWriteStream(key, value)
58 notEqual(writable, undefined)
59 writeBody(writable, body)
60 }
61
62 // Now let's try fetching the response from the store
63 {
64 const result = await store.get(structuredClone(key))
65 notEqual(result, undefined)
66 await compareGetResults(result, value, body)
67 }
68
69 /**
70 * Let's try out a request to a different resource to make sure it can
71 * differentiate between the two
72 * @type {import('../../types/cache-interceptor.d.ts').default.CacheKey}
73 */

Calls 4

getMethod · 0.95
createWriteStreamMethod · 0.95
writeBodyFunction · 0.85
compareGetResultsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…