| 1 |
'use strict' |
| 2 |
|
| 3 |
const { test } = require('node:test') |
| 4 |
const assert = require('node:assert') |
| 5 |
const { tmpdir } = require('node:os') |
| 6 |
const { join } = require('node:path') |
| 7 |
const { unlink } = require('node:fs/promises') |
| 8 |
const { SnapshotRecorder, formatRequestKey, createRequestHash, filterHeadersForMatching, filterHeadersForStorage, createHeaderFilters } = require('../lib/mock/snapshot-recorder') |
| 9 |
|
| 10 |
test('SnapshotRecorder - basic recording and retrieval', (t) => { |
| 11 |
const recorder = new SnapshotRecorder() |
| 12 |
|
| 13 |
const requestOpts = { |
| 14 |
origin: 'https://api.example.com', |
| 15 |
path: '/users/123', |
| 16 |
method: 'GET', |
| 17 |
headers: { authorization: 'Bearer token' } |
| 18 |
} |
| 19 |
|
| 20 |
const response = { |
| 21 |
statusCode: 200, |
| 22 |
headers: { 'content-type': 'application/json' }, |
| 23 |
body: Buffer.from('{"id": 123, "name": "Test User"}'), |
| 24 |
trailers: {} |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
recorder.record(requestOpts, response) |
| 29 |
|
| 30 |
|
| 31 |
assert.strictEqual(recorder.size(), 1) |
| 32 |
|
| 33 |
|
| 34 |
const snapshot = recorder.findSnapshot(requestOpts) |
| 35 |
assert(snapshot) |
| 36 |
assert.strictEqual(snapshot.request.method, 'GET') |
| 37 |
assert.strictEqual(snapshot.request.url, 'https://api.example.com/users/123') |
| 38 |
assert.strictEqual(snapshot.response.statusCode, 200) |
| 39 |
|
| 40 |
assert.strictEqual(snapshot.response.body, response.body.toString('base64')) |
| 41 |
}) |
| 42 |
|
| 43 |
test('SnapshotRecorder - request key formatting', (t) => { |
| 44 |
const requestOpts = { |
| 45 |
origin: 'https://api.example.com', |
| 46 |
path: '/search?q=test&limit=10', |
| 47 |
method: 'POST', |
| 48 |
headers: { |
| 49 |
'Content-Type': 'application/json', |
| 50 |
Authorization: 'Bearer token' |
| 51 |
}, |
| 52 |
body: '{"filter": "active"}' |
| 53 |
} |
| 54 |
|
| 55 |
const cachedSets = createHeaderFilters({}) |
| 56 |
const formatted = formatRequestKey(requestOpts, cachedSets) |
| 57 |
|
| 58 |
assert.strictEqual(formatted.method, 'POST') |
| 59 |
assert.strictEqual(formatted.url, 'https://api.example.com/search?q=test&limit=10') |
| 60 |
assert.strictEqual(formatted.headers['content-type'], 'application/json') |
| 61 |
assert.strictEqual(formatted.headers.authorization, 'Bearer token') |
| 62 |
assert.strictEqual(formatted.body, '{"filter": "active"}') |
| 63 |
}) |
| 64 |
|
| 65 |
test('SnapshotRecorder - request hashing', (t) => { |
| 66 |
const request1 = { |
| 67 |
method: 'GET', |
| 68 |
url: 'https://api.example.com/users', |
| 69 |
headers: { authorization: 'Bearer token' }, |
| 70 |
body: undefined |
| 71 |
} |
| 72 |
|
| 73 |
const request2 = { |
| 74 |
method: 'GET', |
| 75 |
url: 'https://api.example.com/users', |
| 76 |
headers: { authorization: 'Bearer token' }, |
| 77 |
body: undefined |
| 78 |
} |
| 79 |
|
| 80 |
const request3 = { |
| 81 |
method: 'POST', |
| 82 |
url: 'https://api.example.com/users', |
| 83 |
headers: { authorization: 'Bearer token' }, |
| 84 |
body: undefined |
| 85 |
} |
| 86 |
|
| 87 |
const hash1 = createRequestHash(request1) |
| 88 |
const hash2 = createRequestHash(request2) |
| 89 |
const hash3 = createRequestHash(request3) |
| 90 |
|
| 91 |
|
| 92 |
assert.strictEqual(hash1, hash2) |
| 93 |
|
| 94 |
|
| 95 |
assert.notStrictEqual(hash1, hash3) |
| 96 |
|
| 97 |
|
| 98 |
assert(hash1.match(/^[A-Za-z0-9_-]+$/)) |
| 99 |
}) |
| 100 |
|
| 101 |
test('SnapshotRecorder - header normalization', (t) => { |
| 102 |
const requestOpts1 = { |
| 103 |
origin: 'https://api.example.com', |
| 104 |
path: '/test', |
| 105 |
headers: { |
| 106 |
'Content-Type': 'application/json', |
| 107 |
AUTHORIZATION: 'Bearer token' |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
const requestOpts2 = { |
| 112 |
origin: 'https://api.example.com', |
| 113 |
path: '/test', |
| 114 |
headers: { |
| 115 |
'content-type': 'application/json', |
| 116 |
authorization: 'Bearer token' |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
const cachedSets = createHeaderFilters({}) |
| 121 |
const formatted1 = formatRequestKey(requestOpts1, cachedSets) |
| 122 |
const formatted2 = formatRequestKey(requestOpts2, cachedSets) |
| 123 |
|
| 124 |
|
| 125 |
assert.deepStrictEqual(formatted1.headers, formatted2.headers) |
| 126 |
assert.strictEqual(formatted1.headers['content-type'], 'application/json') |
| 127 |
assert.strictEqual(formatted1.headers.authorization, 'Bearer token') |
| 128 |
}) |
| 129 |
|
| 130 |
test('SnapshotRecorder - file persistence', async (t) => { |
| 131 |
const snapshotPath = join(tmpdir(), `test-recorder-${Date.now()}.json`) |
| 132 |
const recorder = new SnapshotRecorder({ snapshotPath }) |
| 133 |
|
| 134 |
t.after(() => unlink(snapshotPath).catch(() => {})) |
| 135 |
|
| 136 |
|
| 137 |
await recorder.record( |
| 138 |
{ origin: 'https://api.example.com', path: '/users', method: 'GET' }, |
| 139 |
{ statusCode: 200, headers: {}, body: Buffer.from('user data'), trailers: {} } |
| 140 |
) |
| 141 |
|
| 142 |
await recorder.record( |
| 143 |
{ origin: 'https://api.example.com', path: '/posts', method: 'GET' }, |
| 144 |
{ statusCode: 200, headers: {}, body: Buffer.from('post data'), trailers: {} } |
| 145 |
) |
| 146 |
|
| 147 |
assert.strictEqual(recorder.size(), 2) |
| 148 |
|
| 149 |
|
| 150 |
await recorder.saveSnapshots() |
| 151 |
|
| 152 |
|
| 153 |
const newRecorder = new SnapshotRecorder({ snapshotPath }) |
| 154 |
await newRecorder.loadSnapshots() |
| 155 |
|
| 156 |
assert.strictEqual(newRecorder.size(), 2) |
| 157 |
|
| 158 |
|
| 159 |
const userSnapshot = newRecorder.findSnapshot({ |
| 160 |
origin: 'https://api.example.com', |
| 161 |
path: '/users', |
| 162 |
method: 'GET' |
| 163 |
}) |
| 164 |
|
| 165 |
assert(userSnapshot) |
| 166 |
assert.strictEqual(userSnapshot.response.statusCode, 200) |
| 167 |
|
| 168 |
assert.strictEqual(userSnapshot.response.body, Buffer.from('user data').toString('base64')) |
| 169 |
}) |
| 170 |
|
| 171 |
test('SnapshotRecorder - loading non-existent file', async (t) => { |
| 172 |
const snapshotPath = join(tmpdir(), `non-existent-${Date.now()}.json`) |
| 173 |
const recorder = new SnapshotRecorder({ snapshotPath }) |
| 174 |
|
| 175 |
|
| 176 |
await recorder.loadSnapshots() |
| 177 |
assert.strictEqual(recorder.size(), 0) |
| 178 |
}) |
| 179 |
|
| 180 |
test('SnapshotRecorder - array header handling', (t) => { |
| 181 |
const requestOpts = { |
| 182 |
origin: 'https://api.example.com', |
| 183 |
path: '/test', |
| 184 |
headers: { |
| 185 |
accept: ['application/json', 'text/plain'], |
| 186 |
'x-custom': 'single-value' |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
const cachedSets = createHeaderFilters({}) |
| 191 |
const formatted = formatRequestKey(requestOpts, cachedSets) |
| 192 |
|
| 193 |
|
| 194 |
assert.strictEqual(formatted.headers.accept, 'application/json, text/plain') |
| 195 |
assert.strictEqual(formatted.headers['x-custom'], 'single-value') |
| 196 |
}) |
| 197 |
|
| 198 |
test('SnapshotRecorder - query parameter handling', (t) => { |
| 199 |
const requestOpts1 = { |
| 200 |
origin: 'https://api.example.com', |
| 201 |
path: '/search?q=test&sort=date', |
| 202 |
method: 'GET' |
| 203 |
} |
| 204 |
|
| 205 |
const requestOpts2 = { |
| 206 |
origin: 'https://api.example.com', |
| 207 |
path: '/search?sort=date&q=test', |
| 208 |
method: 'GET' |
| 209 |
} |
| 210 |
|
| 211 |
const cachedSets = createHeaderFilters({}) |
| 212 |
const formatted1 = formatRequestKey(requestOpts1, cachedSets) |
| 213 |
const formatted2 = formatRequestKey(requestOpts2, cachedSets) |
| 214 |
|
| 215 |
|
| 216 |
assert.strictEqual(formatted1.url, 'https://api.example.com/search?q=test&sort=date') |
| 217 |
|
| 218 |
|
| 219 |
const hash1 = createRequestHash(formatted1) |
| 220 |
const hash2 = createRequestHash(formatted2) |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
assert.notStrictEqual(hash1, hash2) |
| 225 |
}) |
| 226 |
|
| 227 |
test('SnapshotRecorder - clear functionality', async (t) => { |
| 228 |
const recorder = new SnapshotRecorder() |
| 229 |
|
| 230 |
|
| 231 |
await recorder.record( |
| 232 |
{ origin: 'https://api.example.com', path: '/test1' }, |
| 233 |
{ statusCode: 200, headers: {}, body: Buffer.from('data1'), trailers: {} } |
| 234 |
) |
| 235 |
|
| 236 |
await recorder.record( |
| 237 |
{ origin: 'https://api.example.com', path: '/test2' }, |
| 238 |
{ statusCode: 200, headers: {}, body: Buffer.from('data2'), trailers: {} } |
| 239 |
) |
| 240 |
|
| 241 |
assert.strictEqual(recorder.size(), 2) |
| 242 |
|
| 243 |
|
| 244 |
recorder.clear() |
| 245 |
assert.strictEqual(recorder.size(), 0) |
| 246 |
|
| 247 |
|
| 248 |
const snapshot = recorder.findSnapshot({ |
| 249 |
origin: 'https://api.example.com', |
| 250 |
path: '/test1' |
| 251 |
}) |
| 252 |
assert.strictEqual(snapshot, undefined) |
| 253 |
}) |
| 254 |
|
| 255 |
test('SnapshotRecorder - custom header matching', (t) => { |
| 256 |
const headers = { |
| 257 |
'content-type': 'application/json', |
| 258 |
authorization: 'Bearer token', |
| 259 |
'x-request-id': '123', |
| 260 |
accept: 'application/json' |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
const matchSpecificOptions = { matchHeaders: ['content-type', 'accept'] } |
| 265 |
const matchSpecificCachedSets = createHeaderFilters(matchSpecificOptions) |
| 266 |
const matchSpecific = filterHeadersForMatching(headers, matchSpecificCachedSets, matchSpecificOptions) |
| 267 |
|
| 268 |
assert.deepStrictEqual(matchSpecific, { |
| 269 |
'content-type': 'application/json', |
| 270 |
accept: 'application/json' |
| 271 |
}) |
| 272 |
|
| 273 |
|
| 274 |
const ignoreOptions = { ignoreHeaders: ['authorization', 'x-request-id'] } |
| 275 |
const ignoreCachedSets = createHeaderFilters(ignoreOptions) |
| 276 |
const ignoreAuth = filterHeadersForMatching(headers, ignoreCachedSets, ignoreOptions) |
| 277 |
|
| 278 |
assert.deepStrictEqual(ignoreAuth, { |
| 279 |
'content-type': 'application/json', |
| 280 |
accept: 'application/json' |
| 281 |
}) |
| 282 |
|
| 283 |
|
| 284 |
const excludeOptions = { excludeHeaders: ['authorization'] } |
| 285 |
const excludeCachedSets = createHeaderFilters(excludeOptions) |
| 286 |
const excludeSensitive = filterHeadersForMatching(headers, excludeCachedSets, excludeOptions) |
| 287 |
|
| 288 |
assert.deepStrictEqual(excludeSensitive, { |
| 289 |
'content-type': 'application/json', |
| 290 |
'x-request-id': '123', |
| 291 |
accept: 'application/json' |
| 292 |
}) |
| 293 |
}) |
| 294 |
|
| 295 |
test('SnapshotRecorder - header filtering for storage', (t) => { |
| 296 |
const headers = { |
| 297 |
'content-type': 'application/json', |
| 298 |
'set-cookie': 'session=secret', |
| 299 |
authorization: 'Bearer token', |
| 300 |
'cache-control': 'no-cache' |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
const filtered = filterHeadersForStorage(headers, { |
| 305 |
exclude: new Set(['set-cookie', 'authorization']) |
| 306 |
}) |
| 307 |
|
| 308 |
assert.deepStrictEqual(filtered, { |
| 309 |
'content-type': 'application/json', |
| 310 |
'cache-control': 'no-cache' |
| 311 |
}) |
| 312 |
}) |
| 313 |
|
| 314 |
test('SnapshotRecorder - case sensitivity in header filtering', (t) => { |
| 315 |
const headers = { |
| 316 |
'Content-Type': 'application/json', |
| 317 |
AUTHORIZATION: 'Bearer token', |
| 318 |
'X-Request-ID': '123' |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
const caseInsensitiveOptions = { ignoreHeaders: ['authorization', 'x-request-id'] } |
| 323 |
const caseInsensitiveCachedSets = createHeaderFilters(caseInsensitiveOptions) |
| 324 |
const caseInsensitive = filterHeadersForMatching(headers, caseInsensitiveCachedSets, caseInsensitiveOptions) |
| 325 |
|
| 326 |
assert.deepStrictEqual(caseInsensitive, { |
| 327 |
'content-type': 'application/json' |
| 328 |
}) |
| 329 |
|
| 330 |
|
| 331 |
const caseSensitiveOptions = { ignoreHeaders: ['authorization', 'x-request-id'], caseSensitive: true } |
| 332 |
const caseSensitiveCachedSets = createHeaderFilters(caseSensitiveOptions) |
| 333 |
const caseSensitive = filterHeadersForMatching(headers, caseSensitiveCachedSets, caseSensitiveOptions) |
| 334 |
|
| 335 |
|
| 336 |
assert.deepStrictEqual(caseSensitive, { |
| 337 |
'Content-Type': 'application/json', |
| 338 |
AUTHORIZATION: 'Bearer token', |
| 339 |
'X-Request-ID': '123' |
| 340 |
}) |
| 341 |
}) |
| 342 |
|
| 343 |
test('SnapshotRecorder - request formatting with match options', (t) => { |
| 344 |
const requestOpts = { |
| 345 |
origin: 'https://api.example.com', |
| 346 |
path: '/search?q=test&limit=10', |
| 347 |
method: 'POST', |
| 348 |
headers: { |
| 349 |
'Content-Type': 'application/json', |
| 350 |
Authorization: 'Bearer token', |
| 351 |
'X-Request-ID': '123' |
| 352 |
}, |
| 353 |
body: '{"filter": "active"}' |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
const matchOptions = { |
| 358 |
matchHeaders: ['content-type'], |
| 359 |
matchBody: false, |
| 360 |
matchQuery: false |
| 361 |
} |
| 362 |
const cachedSets = createHeaderFilters(matchOptions) |
| 363 |
const formatted = formatRequestKey(requestOpts, cachedSets, matchOptions) |
| 364 |
|
| 365 |
assert.strictEqual(formatted.method, 'POST') |
| 366 |
assert.strictEqual(formatted.url, 'https://api.example.com/search') |
| 367 |
assert.deepStrictEqual(formatted.headers, { |
| 368 |
'content-type': 'application/json' |
| 369 |
}) |
| 370 |
assert.strictEqual(formatted.body, '') |
| 371 |
}) |
| 372 |
|
| 373 |
test('SnapshotRecorder - redirect responses are stored correctly', (t) => { |
| 374 |
const recorder = new SnapshotRecorder() |
| 375 |
|
| 376 |
|
| 377 |
const redirectRequestOpts = { |
| 378 |
origin: 'https://api.example.com', |
| 379 |
path: '/redirect-start', |
| 380 |
method: 'GET', |
| 381 |
headers: { accept: 'application/json' } |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
const redirectResponse = { |
| 386 |
statusCode: 302, |
| 387 |
headers: { location: '/redirect-target' }, |
| 388 |
body: Buffer.from('Redirecting...'), |
| 389 |
trailers: {} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
const finalResponse = { |
| 394 |
statusCode: 200, |
| 395 |
headers: { 'content-type': 'application/json' }, |
| 396 |
body: Buffer.from('{"message": "Final destination"}'), |
| 397 |
trailers: {} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
recorder.record(redirectRequestOpts, redirectResponse) |
| 402 |
assert.strictEqual(recorder.size(), 1, 'Redirect response (302) should be stored') |
| 403 |
|
| 404 |
|
| 405 |
let snapshot = recorder.findSnapshot(redirectRequestOpts) |
| 406 |
assert(snapshot, 'Should find snapshot for redirect request') |
| 407 |
assert.strictEqual(snapshot.request.url, 'https://api.example.com/redirect-start') |
| 408 |
assert.strictEqual(snapshot.response.statusCode, 302, 'First stored response should be the 302 redirect') |
| 409 |
|
| 410 |
|
| 411 |
recorder.record(redirectRequestOpts, finalResponse) |
| 412 |
assert.strictEqual(recorder.size(), 1, 'Should still have one snapshot (same request)') |
| 413 |
|
| 414 |
|
| 415 |
snapshot = recorder.findSnapshot(redirectRequestOpts) |
| 416 |
assert(snapshot, 'Should find snapshot for redirect request') |
| 417 |
assert.strictEqual(snapshot.request.url, 'https://api.example.com/redirect-start') |
| 418 |
|
| 419 |
|
| 420 |
assert(Array.isArray(snapshot.responses), 'Should have responses array') |
| 421 |
assert.strictEqual(snapshot.responses.length, 2, 'Should have two responses') |
| 422 |
assert.strictEqual(snapshot.responses[0].statusCode, 302, 'First response should be redirect') |
| 423 |
assert.strictEqual(snapshot.responses[1].statusCode, 200, 'Second response should be final') |
| 424 |
}) |