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