* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore * * @param {{ new(...any): CacheStore }} CacheStore * @param {object} [options] * @param {boolean} [options.skip]
(CacheStore, options)
| 14 | * @param {boolean} [options.skip] |
| 15 | */ |
| 16 | function 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 | */ |
no test coverage detected
searching dependent graphs…