| 1 |
'use strict' |
| 2 |
|
| 3 |
const { test, after, describe } = require('node:test') |
| 4 |
const { strictEqual } = require('node:assert') |
| 5 |
const { createServer } = require('node:http') |
| 6 |
const { once } = require('node:events') |
| 7 |
const { request, Client, interceptors } = require('../../index') |
| 8 |
const FakeTimers = require('@sinonjs/fake-timers') |
| 9 |
const { setTimeout } = require('node:timers/promises') |
| 10 |
|
| 11 |
test('revalidates the request when the response is stale', async () => { |
| 12 |
const clock = FakeTimers.install({ |
| 13 |
now: 1 |
| 14 |
}) |
| 15 |
after(() => clock.uninstall()) |
| 16 |
|
| 17 |
let count = 0 |
| 18 |
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
| 19 |
res.sendDate = false |
| 20 |
res.setHeader('Date', new Date(clock.now).toUTCString()) |
| 21 |
res.setHeader('Cache-Control', 'public, max-age=1') |
| 22 |
res.end('hello world ' + count++) |
| 23 |
}) |
| 24 |
|
| 25 |
server.listen(0) |
| 26 |
await once(server, 'listening') |
| 27 |
|
| 28 |
const dispatcher = new Client(`http://localhost:${server.address().port}`) |
| 29 |
.compose(interceptors.cache()) |
| 30 |
|
| 31 |
after(async () => { |
| 32 |
server.close() |
| 33 |
await dispatcher.close() |
| 34 |
}) |
| 35 |
|
| 36 |
const url = `http://localhost:${server.address().port}` |
| 37 |
|
| 38 |
{ |
| 39 |
const res = await request(url, { dispatcher }) |
| 40 |
strictEqual(await res.body.text(), 'hello world 0') |
| 41 |
} |
| 42 |
|
| 43 |
clock.tick(999) |
| 44 |
|
| 45 |
{ |
| 46 |
const res = await request(url, { dispatcher }) |
| 47 |
strictEqual(await res.body.text(), 'hello world 0') |
| 48 |
} |
| 49 |
|
| 50 |
clock.tick(1) |
| 51 |
|
| 52 |
{ |
| 53 |
const res = await request(url, { dispatcher }) |
| 54 |
strictEqual(await res.body.text(), 'hello world 1') |
| 55 |
} |
| 56 |
|
| 57 |
clock.tick(999) |
| 58 |
|
| 59 |
{ |
| 60 |
const res = await request(url, { dispatcher }) |
| 61 |
strictEqual(await res.body.text(), 'hello world 1') |
| 62 |
} |
| 63 |
|
| 64 |
clock.tick(1) |
| 65 |
|
| 66 |
{ |
| 67 |
const res = await request(url, { dispatcher }) |
| 68 |
strictEqual(await res.body.text(), 'hello world 2') |
| 69 |
} |
| 70 |
}) |
| 71 |
|
| 72 |
describe('revalidates the request, handles 304s during stale-while-revalidate', async () => { |
| 73 |
function isStale (res) { |
| 74 |
return res.headers.warning === '110 - "response is stale"' |
| 75 |
} |
| 76 |
|
| 77 |
async function revalidateTest (useEtag = false) { |
| 78 |
const clock = FakeTimers.install({ |
| 79 |
now: 1 |
| 80 |
}) |
| 81 |
after(() => clock.uninstall()) |
| 82 |
|
| 83 |
let count200 = 0 |
| 84 |
let count304 = 0 |
| 85 |
|
| 86 |
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
| 87 |
res.sendDate = false |
| 88 |
res.setHeader('Date', new Date(clock.now).toUTCString()) |
| 89 |
res.setHeader('Cache-Control', 'public, max-age=10, stale-while-revalidate=3600') |
| 90 |
if (useEtag) { |
| 91 |
res.setHeader('ETag', '"xxx"') |
| 92 |
} |
| 93 |
|
| 94 |
// revalidation response. |
| 95 |
if (req.headers['if-none-match'] || req.headers['if-modified-since']) { |
| 96 |
count304++ |
| 97 |
res.statusCode = 304 |
| 98 |
res.end() |
| 99 |
} else { |
| 100 |
res.end('hello world ' + count200++) |
| 101 |
} |
| 102 |
}) |
| 103 |
|
| 104 |
server.listen(0) |
| 105 |
await once(server, 'listening') |
| 106 |
|
| 107 |
const dispatcher = new Client(`http://localhost:${server.address().port}`) |
| 108 |
.compose(interceptors.cache()) |
| 109 |
|
| 110 |
after(async () => { |
| 111 |
server.close() |
| 112 |
await dispatcher.close() |
| 113 |
}) |
| 114 |
|
| 115 |
const url = `http://localhost:${server.address().port}` |
| 116 |
|
| 117 |
// initial request, cache the response |
| 118 |
{ |
| 119 |
const res = await request(url, { dispatcher }) |
| 120 |
strictEqual(await res.body.text(), 'hello world 0') |
| 121 |
strictEqual(isStale(res), false) |
| 122 |
strictEqual(res.statusCode, 200) |
| 123 |
} |
| 124 |
|
| 125 |
// wait nearly a second, still fresh |
| 126 |
{ |
| 127 |
clock.tick(900) |
| 128 |
const res = await request(url, { dispatcher }) |
| 129 |
strictEqual(await res.body.text(), 'hello world 0') |
| 130 |
strictEqual(isStale(res), false) |
| 131 |
strictEqual(res.statusCode, 200) |
| 132 |
} |
| 133 |
|
| 134 |
// within stale-while-revalidate window, still return stale cached response, revalidate in background |
| 135 |
{ |
| 136 |
clock.tick(12000) |
| 137 |
const res = await request(url, { dispatcher }) |
| 138 |
strictEqual(await res.body.text(), 'hello world 0') |
| 139 |
strictEqual(isStale(res), true) |
| 140 |
strictEqual(res.statusCode, 200) |
| 141 |
await setTimeout(100) // wait for revalidation to be complete. |
| 142 |
} |
| 143 |
|
| 144 |
// should get revalidated content, not stale. |
| 145 |
{ |
| 146 |
clock.tick(10) |
| 147 |
const res = await request(url, { dispatcher }) |
| 148 |
strictEqual(await res.body.text(), 'hello world 0') |
| 149 |
strictEqual(isStale(res), false) |
| 150 |
strictEqual(res.statusCode, 200) |
| 151 |
} |
| 152 |
|
| 153 |
strictEqual(count200, 1) |
| 154 |
strictEqual(count304, 1) |
| 155 |
} |
| 156 |
|
| 157 |
test('using if-none-match', async () => await revalidateTest(true)) |
| 158 |
test('using if-modified-since', async () => await revalidateTest(false)) |
| 159 |
}) |