File size: 8,952 Bytes
94786da | 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | diff --git a/test/unit/incremental-cache/generate-cache-key.test.ts b/test/unit/incremental-cache/generate-cache-key.test.ts
new file mode 100644
index 00000000..f320376b
--- /dev/null
+++ b/test/unit/incremental-cache/generate-cache-key.test.ts
@@ -0,0 +1,252 @@
+import { IncrementalCache } from '../../../packages/next/src/server/lib/incremental-cache'
+
+function createCache() {
+ return new IncrementalCache({
+ dev: false,
+ requestHeaders: {},
+ getPrerenderManifest: () =>
+ ({
+ version: 4,
+ routes: {},
+ dynamicRoutes: {},
+ notFoundRoutes: [],
+ preview: {
+ previewModeId: 'id',
+ previewModeSigningKey: 'key',
+ previewModeEncryptionKey: 'key',
+ },
+ }) as any,
+ })
+}
+
+describe('IncrementalCache.generateCacheKey', () => {
+ const cache = createCache()
+ const url = 'https://example.com/api'
+
+ it('distinguishes binary bodies that UTF-8 decoding would collapse', async () => {
+ // 0xff and 0xfe both decode to U+FFFD as UTF-8; the key must tell them
+ // apart by their raw bytes.
+ const a = await cache.generateCacheKey(url, {
+ body: new Uint8Array([0xff]),
+ })
+ const b = await cache.generateCacheKey(url, {
+ body: new Uint8Array([0xfe]),
+ })
+ expect(a).not.toBe(b)
+ })
+
+ it('distinguishes a string body from its UTF-8 encoded bytes', async () => {
+ // A string and the bytes it encodes to are different request shapes.
+ const a = await cache.generateCacheKey(url, { body: 'hello' })
+ const b = await cache.generateCacheKey(url, {
+ body: new TextEncoder().encode('hello'),
+ })
+ expect(a).not.toBe(b)
+ })
+
+ it('uses the selected bytes of arbitrary ArrayBuffer views', async () => {
+ const backing = new Uint8Array([9, 1, 2, 3, 9])
+ const dataView = new DataView(backing.buffer, 1, 3)
+ const typedView = new Uint8Array([1, 2, 3])
+
+ const a = await cache.generateCacheKey(url, { body: dataView })
+ const b = await cache.generateCacheKey(url, { body: typedView })
+ expect(a).toBe(b)
+ })
+
+ it('distinguishes URLSearchParams, FormData, and string bodies', async () => {
+ const form = new FormData()
+ form.append('x', 'a')
+
+ const formKey = await cache.generateCacheKey(url, { body: form })
+ const paramsKey = await cache.generateCacheKey(url, {
+ body: new URLSearchParams([['x', 'a']]),
+ })
+ const stringKey = await cache.generateCacheKey(url, { body: 'x=a' })
+
+ expect(new Set([formKey, paramsKey, stringKey]).size).toBe(3)
+ })
+
+ it('distinguishes a Blob from its raw content bytes', async () => {
+ const bytes = new Uint8Array([1, 2, 3])
+ const blobKey = await cache.generateCacheKey(url, {
+ body: new Blob([bytes]),
+ })
+ const bytesKey = await cache.generateCacheKey(url, { body: bytes })
+
+ expect(blobKey).not.toBe(bytesKey)
+ })
+
+ it('is deterministic for identical bodies', async () => {
+ const a = await cache.generateCacheKey(url, {
+ body: new Uint8Array([1, 2, 3]),
+ })
+ const b = await cache.generateCacheKey(url, {
+ body: new Uint8Array([1, 2, 3]),
+ })
+ expect(a).toBe(b)
+ })
+
+ it('does not collide FormData multi-values with a comma-joined string', async () => {
+ // The two values ['a', 'b'] must not hash the same as the single 'a,b'.
+ const multi = new FormData()
+ multi.append('x', 'a')
+ multi.append('x', 'b')
+
+ const joined = new FormData()
+ joined.append('x', 'a,b')
+
+ const a = await cache.generateCacheKey(url, { body: multi })
+ const b = await cache.generateCacheKey(url, { body: joined })
+ expect(a).not.toBe(b)
+ })
+
+ it('distinguishes blobs that differ only by content type', async () => {
+ const bytes = new Uint8Array([1, 2, 3])
+ const a = await cache.generateCacheKey(url, {
+ body: new Blob([bytes], { type: 'text/plain' }),
+ })
+ const b = await cache.generateCacheKey(url, {
+ body: new Blob([bytes], { type: 'application/json' }),
+ })
+ expect(a).not.toBe(b)
+ })
+
+ it('distinguishes ArrayBuffers', async () => {
+ const a = await cache.generateCacheKey(url, {
+ body: new Uint8Array([1, 2, 3, 4]).buffer,
+ })
+ const b = await cache.generateCacheKey(url, {
+ body: new Uint8Array([1, 2, 3]).buffer,
+ })
+ expect(a).not.toBe(b)
+ })
+
+ it('produces the same key for identical bytes in different typed arrays', async () => {
+ const uint8View = new Uint8Array([1, 2, 3, 4])
+ const uint16View = new Uint16Array(
+ uint8View.buffer,
+ uint8View.byteOffset,
+ uint8View.byteLength / 2
+ )
+ const a = await cache.generateCacheKey(url, {
+ body: uint8View,
+ })
+ const b = await cache.generateCacheKey(url, {
+ body: uint16View,
+ })
+ expect(a).toBe(b)
+ })
+
+ it('does not collide FormData with differently interleaved values', async () => {
+ const a = new FormData()
+ a.append('x', 'a')
+ a.append('x', 'b')
+ a.append('y', 'a')
+
+ const b = new FormData()
+ b.append('x', 'a')
+ b.append('y', 'a')
+ b.append('x', 'b')
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+
+ it('does not collide FormData whose value forges quoted entry delimiters', async () => {
+ // User values may resemble framing text and must remain data.
+ const a = new FormData()
+ a.append('x', 'a"key:"y"str:"b')
+
+ const b = new FormData()
+ b.append('x', 'a')
+ b.append('y', 'b')
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+
+ it('does not collide FormData whose value spans an entry boundary', async () => {
+ // One value may contain text resembling a complete following entry.
+ const a = new FormData()
+ a.append('x', 'akey:ystr:b')
+
+ const b = new FormData()
+ b.append('x', 'a')
+ b.append('y', 'b')
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+
+ it('does not collide FormData files that differ only by name/type split', async () => {
+ // File name and content type are separate fetch-relevant metadata.
+ const bytes = new Uint8Array([1, 2, 3])
+
+ const a = new FormData()
+ a.append('f', new Blob([bytes], { type: 'c' }), 'ab')
+
+ const b = new FormData()
+ b.append('f', new Blob([bytes], { type: 'bc' }), 'a')
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+
+ it('does not collide a file whose content forges a following entry', async () => {
+ // File bytes may resemble framing for a following field.
+ const enc = new TextEncoder()
+
+ const a = new FormData()
+ a.append('f', new Blob([enc.encode('AAAkey:1kstr:1v')], { type: '' }), 'n')
+
+ const b = new FormData()
+ b.append('f', new Blob([enc.encode('AAA')], { type: '' }), 'n')
+ b.append('k', 'v')
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+
+ it('does not collide blobs that differ only by type/content split', async () => {
+ // Blob content types and bytes must be framed as separate values.
+ const enc = new TextEncoder()
+
+ const a = new Blob([enc.encode('y')], { type: 'bytes:x' })
+ const b = new Blob([enc.encode('xbytes:y')], { type: '' })
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+
+ it('does not collide a FormData value whose length digits absorb a forged entry', async () => {
+ // Length digits adjacent to user data must not make framing ambiguous.
+ const a = new FormData()
+ a.append('x', 'key:1astr:0')
+
+ const b = new FormData()
+ b.append('x', '1')
+ b.append('a', '')
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+
+ it('does not collide blobs whose type-length digits absorb the content', async () => {
+ // Type lengths and content beginning with digits remain distinct.
+ const enc = new TextEncoder()
+ const a = new Blob([new Uint8Array(0)], { type: 'bytes:aaaaa' })
+ const b = new Blob([enc.encode('aaaaabytes:')], { type: '1' })
+
+ const keyA = await cache.generateCacheKey(url, { body: a })
+ const keyB = await cache.generateCacheKey(url, { body: b })
+ expect(keyA).not.toBe(keyB)
+ })
+})
|