forked from Genaker/CloudFlare_FPC_Worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfpc.test.js
More file actions
629 lines (570 loc) · 23.5 KB
/
Copy pathfpc.test.js
File metadata and controls
629 lines (570 loc) · 23.5 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// To run the tests set: export TEST_URL="http://site.com/" and run: npm test
//import fetch from 'node-fetch'
const fetch = require('node-fetch')
// Make sure the last worker changes is deployed to the domain you are using
// To run tests: npm install && npm test
/**
* CF Page rule must be applied
*
* Disable Security, Browser Integrity Check: Off
*
*/
const URL = process.env.TEST_URL;
const DYNAMIC = "DYNAMIC";
const HIT = "HIT";
jest.setTimeout(60000);
let beforeTestVersion = null;
let preheatedUrl = null;
describe("FPC TESTS", () => {
let uniqueParam = "?test-param=" + Date.now();
test('New Request Test', async () => {
const response = await fetch(URL + uniqueParam);
const headers = response.headers;
console.log(response);
console.log(headers);
if (headers.get('cf-mitigated') !== null) {
throw new Error('Disable CF WAF protection');
process.exit(1);
}
expect(response.status).toEqual(200);
if (response.status !== 200) {
process.exit("Server Response Error - Fix Server first");
}
expect(headers.get('x-html-edge-cache-version')).not.toEqual(null);
beforeTestVersion = headers.get('x-html-edge-cache-version');
expect(headers.get('cf-cache-status')).toEqual(DYNAMIC);
expect(headers.get('x-html-edge-cache-status')).toContain(",Miss,FetchedOrigin,CachingAsync,");
});
test('Repeated Requests Test', async () => {
const url = URL + uniqueParam;
let response = await fetch(url);
await new Promise((r) => setTimeout(r, 1000));
response = await fetch(url);
response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
const cacheVersion = parseInt(headers.get('x-html-edge-cache-version'));
expect(response.status).toEqual(200);
expect(headers.get('age')).not.toBeNull();
expect(headers.get('js-time')).not.toBeNull();
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('key')).toEqual(url + "&cf_edge_cache_ver=" + cacheVersion);
expect(headers.get('x-html-edge-cache-status')).toContain("Hit,");
});
test('Repeated Requests Test #2', async () => {
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
let response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
const cacheVersion = parseInt(headers.get('x-html-edge-cache-version'));
expect(response.status).toEqual(200);
expect(headers.get('age')).not.toBeNull();
expect(headers.get('js-time')).not.toBeNull();
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('key')).toEqual(url + "&cf_edge_cache_ver=" + cacheVersion);
expect(headers.get('x-html-edge-cache-status')).toContain("Hit,");
});
test('Repeated Requests Pass CDN', async () => {
// add bypass CDN get parameter
const bypassCDN = "&cf-cdn=false"
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
let response = await fetch(url + bypassCDN);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
const cacheVersion = parseInt(headers.get('x-html-edge-cache-version'));
expect(response.status).toEqual(200);
expect(headers.get('age')).not.toBeNull();
expect(headers.get('js-time')).not.toBeNull();
expect(headers.get('r2-time')).not.toBeNull();
expect(headers.get('r2')).toEqual("true");
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('key')).toEqual(url + "&cf_edge_cache_ver=" + cacheVersion);
expect(headers.get('x-html-edge-cache-status')).toContain("Hit,");
});
test('Repeated Requests Test #3', async () => {
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
let response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
const cacheVersion = parseInt(headers.get('x-html-edge-cache-version'));
expect(response.status).toEqual(200);
expect(headers.get('age')).not.toBeNull();
expect(headers.get('js-time')).not.toBeNull();
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('key')).toEqual(url + "&cf_edge_cache_ver=" + cacheVersion);
expect(headers.get('x-html-edge-cache-status')).toContain("Hit,");
});
test('Repeated Requests Pass CDN', async () => {
// add bypass CDN get parameter
const bypassCDN = "&cf-cdn=false"
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
let response = await fetch(url + bypassCDN);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
const cacheVersion = parseInt(headers.get('x-html-edge-cache-version'));
expect(response.status).toEqual(200);
expect(headers.get('age')).not.toBeNull();
expect(headers.get('js-time')).not.toBeNull();
expect(headers.get('r2-time')).not.toBeNull();
expect(headers.get('r2')).toEqual("true");
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('key')).toEqual(url + "&cf_edge_cache_ver=" + cacheVersion);
expect(headers.get('x-html-edge-cache-status')).toContain("Hit,");
});
test('Delete Page from CDN', async () => {
// add bypass CDN get parameter
const deleteFromCDN = "&cf-delete=true"
const url = URL + uniqueParam;
let response = await fetch(url + deleteFromCDN);
const headers = response.headers;
console.log(url + deleteFromCDN);
console.log(response);
console.log(headers);
const cacheVersion = parseInt(headers.get('x-html-edge-cache-version'));
expect(response.status).toEqual(211);
expect(headers.get('deleted')).toEqual("true");
expect(headers.get('delete-status')).toEqual("true");
});
test('Fetch after Delete', async () => {
// CF Clears it not right away
await new Promise((r) => setTimeout(r, 5000));
const url = URL + uniqueParam;
const response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(headers.get('cf-cache-status')).toEqual(HIT);
// ToDO: This rule works randomly ->
// expect(headers.get('r2')).toEqual("true");
});
uniqueParam = "?test-param=" + Date.now();
test('Fetch New Page', async () => {
// Clears it not right away
const url = URL + uniqueParam;
const response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
});
let previousCacheVersion = null;
test('Change Version', async () => {
// Clears it not right away
const changeVersionPurgeParameter = "&cf-purge=true"
const url = URL + uniqueParam;
const response = await fetch(url + changeVersionPurgeParameter);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(222);
expect(headers.get('Cache-Version')).not.toEqual(null);
previousCacheVersion = parseInt(headers.get('Cache-Version')) - 1;
});
test('Fetch after Change Version', async () => {
// Clears it not right away
const url = URL + uniqueParam;
//Backend Revalidation happens slow
await new Promise((r) => setTimeout(r, 1000));
const response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
let currentVersion = headers.get('x-html-edge-cache-version')
expect(parseInt(currentVersion)).not.toEqual(null);
let status = "Hit,Stale,Refreshed";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
expect(parseInt(headers.get('stale-version'))).toEqual(parseInt(currentVersion) - 1);
});
// Randomly fails. It is ok.
test('Fetch second time after Change Version #2', async () => {
// Clears it not right away
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 10000));
const response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
let currentVersion = headers.get('x-html-edge-cache-version')
expect(parseInt(currentVersion)).not.toEqual(null);
expect(headers.get('key')).toContain(currentVersion);
let status = "Hit,Stale_";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
});
test('Test ignored GET parameters', async () => {
// Clears it not right away
const ignoredGET = "&add=fdgdfg";
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
const response = await fetch(url + ignoredGET);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
let currentVersion = headers.get('x-html-edge-cache-version')
expect(parseInt(currentVersion)).not.toEqual(null);
expect(headers.get('key')).toContain(currentVersion);
expect(headers.get('key')).not.toContain(ignoredGET);
let status = "Hit,Stale_";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
});
test('Test ignored GET parameters multiple', async () => {
// Clears it not right away
const ignoredGET = "&add=fdgdfg&gclsrc=sfsd";
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
const response = await fetch(url + ignoredGET);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
let currentVersion = headers.get('x-html-edge-cache-version')
expect(parseInt(currentVersion)).not.toEqual(null);
expect(headers.get('key')).toContain(currentVersion);
expect(headers.get('key')).not.toContain(ignoredGET);
let status = "Hit,Stale";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
});
test('Test ignored GET parameters different', async () => {
// Clears it not right away
const ignoredGET = "&add=fdgdfg&gclsrc=sfsd";
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
const response = await fetch(url + ignoredGET + "&mustbepresent=6666");
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(DYNAMIC);
let currentVersion = headers.get('x-html-edge-cache-version')
//TODO: Add key to all responses
//expect(headers.get('key')).toContain('mustbepresent');
//expect(headers.get('key')).not.toContain(ignoredGET);
let status = "Miss,FetchedOrigin";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
});
test('Test bypass URL', async () => {
// Clears it not right away
const bypassGET = "&ajax";
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
const response = await fetch(url + bypassGET);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual("BYPASS,WORKER,MISS");
expect(headers.get('bypass-worker')).toEqual("true");
});
test('Test bypass and ignore parameters in URL', async () => {
// Clears it not right away
const bypassGET = "&ajax=123&gclsrc=123";
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 1000));
const response = await fetch(url + bypassGET);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual("BYPASS,WORKER,MISS");
expect(headers.get('bypass-worker')).toEqual("true");
});
})
describe("ASYNC revalidation Logic", () => {
let localUniqueValue = Date.now();
// TTL set to 1 after 2 second delay it must be revalidated
let GET = "&sfsdfsd=" + localUniqueValue + "&add=123";
let uniqueParam = "?test-param=" + Date.now();
let url = URL + uniqueParam;
test('Pre Fetch', async () => {
//warm up cache
let response = await fetch(url + GET);
preheatedUrl = url + GET;
let headers = response.headers;
console.log(url + GET);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(DYNAMIC);
expect(headers.get('x-html-edge-cache-status')).toContain("Miss,FetchedOrigin,CachingAsync");
});
test("Fetch with expired AGE", async () => {
GET = "&sfsdfsd=" + localUniqueValue + "&add=123&cf-ttl=1";
//Wait 12 Seconds
await new Promise((r) => setTimeout(r, 5000));
response = await fetch(url + GET);
headers = response.headers;
console.log(url + GET);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
let status1 = "Hit";
let status2 = "Refreshed";
expect(headers.get('x-html-edge-cache-status')).toContain(status1);
expect(headers.get('x-html-edge-cache-status')).toContain(status2);
expect(headers.get('custom-ttl')).toContain("1");
});
test("Fetch Without CDN ", async () => {
// Check if R2 is updates Age must be around delay time less than 10
await new Promise((r) => setTimeout(r, 6000));
response = await fetch(url + GET + "&cf-cdn=false");
headers = response.headers;
console.log(url + GET);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(parseInt(headers.get('age'))).toBeLessThan(13);
expect(headers.get('x-html-edge-cache-status')).toContain("FromR2");
expect(headers.get('r2')).toEqual("true");
});
});
describe("Test R2 Stale", () => {
let uniqueParam = "?test-params=" + Date.now() + "&sdfsdf=sfsdf";
let previousCacheVersion = null;
test('Pre Fetch', async () => {
// Clears it not right away
const url = URL + uniqueParam;
const response = await fetch(url);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('x-html-edge-cache-status')).toContain("Miss,FetchedOrigin,CachingAsync");
expect(headers.get('cf-cache-status')).toEqual(DYNAMIC);
});
test('Change Version', async () => {
// Clears it not right away
const changeVersionPurgeParameter = "&cf-purge=true"
const url = URL + uniqueParam;
const response = await fetch(url + changeVersionPurgeParameter);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(222);
expect(headers.get('Cache-Version')).not.toEqual(null);
previousCacheVersion = parseInt(headers.get('Cache-Version')) - 1;
expect(headers.get('Cache-Version')).toEqual((previousCacheVersion + 1).toString());
});
test('Fetch Changed Version without CDN - R2 Stale', async () => {
const cdnMissParameter = "&cf-cdn=false"
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 4000));
const response = await fetch(url + cdnMissParameter);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('r2')).toEqual("true");
expect(headers.get('r2-cache-version')).toEqual(previousCacheVersion.toString());
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('stale-version')).toEqual(previousCacheVersion.toString());
expect(headers.get('r2-stale')).toEqual("true");
expect(headers.get('key')).toContain((previousCacheVersion + 1).toString());
expect(headers.get('r2-stale-url')).toContain((previousCacheVersion).toString());
expect(headers.get('x-html-edge-cache-version')).toContain((previousCacheVersion + 1).toString());
expect(headers.get('x-html-edge-cache-status')).toContain("Hit,Stale,Refreshed");
expect(headers.get('x-html-edge-cache-status')).toContain("FromR2,R2Stale");
});
test('Fetch Changed Version without CDN #2 revalidated', async () => {
const changeVersionPurgeParameter = "&cf-cdn=false"
const url = URL + uniqueParam;
await new Promise((r) => setTimeout(r, 6000));
const response = await fetch(url + changeVersionPurgeParameter);
const headers = response.headers;
console.log(url);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('r2')).toEqual("true");
expect(headers.get('r2-cache-version')).toEqual((previousCacheVersion + 1).toString());
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('x-html-edge-cache-status')).toContain("FromR2");
expect(headers.get('x-html-edge-cache-status')).toContain("Hit");
expect(headers.get('x-html-edge-cache-status')).toContain("SavedCDNasync");
});
});
describe("Restore CF Version", () => {
// restore version
test("Set Version Back", async () => {
const restoreVersionURL = URL + "?cf-version=" + beforeTestVersion;
let response = await fetch(URL + restoreVersionURL);
expect(response.status).toEqual(223);
expect(response.headers.get('cache-version')).toContain(beforeTestVersion);
});
test("Check Version", async () => {
response = await fetch(URL + "?dfghgdfhgfh=" + Date.now());
expect(response.headers.get('x-html-edge-cache-version')).toEqual(beforeTestVersion);
})
})
describe("Speculation Rules Test", () => {
// restore version
test("Check no return if not cached", async () => {
let testHeaders = { 'Sec-Purpose': "prerender" };
const url = URL + "?sdsd=" + Date.now();
let response = await fetch(url, { headers: testHeaders });
expect(response.status).toEqual(406);
});
test("Check if not cache response is not cached", async () => {
let testHeaders = { 'Sec-Purpose': "prerender" };
const url = URL + "?sdsd=" + Date.now();
let response = await fetch(url);
expect(response.status).toEqual(200);
});
test("Check if cached", async () => {
response = await fetch(preheatedUrl);
expect(response.status).toEqual(200);
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(response.headers.get('x-html-edge-cache-version')).toEqual(beforeTestVersion);
})
})
describe("GOD_MOD tests", () => {
test('PreFetch Url', () => { });
})
describe("Test Logged-In", () => {
test("Test Logged-In Bypass cookies", async () => {
let testHeaders = { 'cookie': "X-Magento-Vary=sdfsdfdsfdsf34234dsfsdfsdf;test=test;" };
const url = URL + "?sdsd=" + Date.now() + "&cf-cdn=false";
let response = await fetch(url, { headers: testHeaders });
expect(response.status).toEqual(200);
expect(response.headers.get('cf-cache-status')).toEqual(DYNAMIC);
});
})
describe("Test HASH", () => {
GET = "?randome=" + Date.now();
let hash = "";
let r2Time = null;
test('Pre Fetch', async () => {
//warm up cache
let response = await fetch(URL + GET);
let headers = response.headers;
console.log(URL + GET);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('r2-hash')).toBeNull();
expect(headers.get('r2-promise')).toEqual("1");
expect(headers.get('cf-cache-status')).toEqual(DYNAMIC);
expect(headers.get('x-html-edge-cache-status')).toContain("Miss,FetchedOrigin,CachingAsync");
});
test('Pre Fetch without Race ', async () => {
//warm up cache
let response = await fetch(URL + GET + "&aa=bb&r2-race=0");
let headers = response.headers;
console.log(URL + GET);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
expect(headers.get('r2-hash')).toBeNull();
expect(headers.get('r2-promise')).toBeNull();
expect(headers.get('cf-cache-status')).toEqual(DYNAMIC);
expect(headers.get('x-html-edge-cache-status')).toContain("Miss,FetchedOrigin,CachingAsync");
});
test("Read hash", async () => {
await new Promise((r) => setTimeout(r, 4000));
response = await fetch(URL + GET + "&cf-cdn=false&r2-race=false");
headers = response.headers;
console.log(URL + GET);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
let status = "FromR2,SavedCDNasync,Hit";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
hash = headers.get('r2-hash');
r2Time = headers.get('r2-time');
expect(headers.get('r2-hash')).not.toBeNull();
expect(headers.get('cf-cache-status')).toEqual(HIT);
});
test("Fetch with expired AGE and check new hash", async () => {
GET = GET + "&cf-ttl=1&cf-cdn=false&r2-race=false";
//Wait 12 Seconds
await new Promise((r) => setTimeout(r, 5000));
response = await fetch(URL + GET);
headers = response.headers;
console.log(URL + GET);
console.log(response);
console.log(headers);
expect(response.status).toEqual(200);
// If from server it doesn't have hash
expect(headers.get('r2-hash')).not.toBeNull();
expect(headers.get('r2-hash')).toEqual(hash);
expect(headers.get('r2-time')).toEqual(r2Time);
expect(headers.get('cf-cache-status')).toEqual(HIT);
expect(headers.get('x-html-edge-cache-status')).toContain('Hit');
expect(headers.get('x-html-edge-cache-status')).toContain('Refreshed');
expect(headers.get('custom-ttl')).toContain("1");
});
})
describe("301 redirect test", () => {
GET2 = "/about-tilebar/?r2-race=false&dfsd=" + Date.now();
test('Pre Fetch 301', async () => {
//warm up cache
let response = await fetch(URL + GET2, { redirect: "manual" });
let headers = response.headers;
console.log(URL + GET2);
console.log(response);
console.log(headers);
expect(response.status).toEqual(301);
expect(headers.get('cf-cache-status')).toEqual(DYNAMIC);
expect(headers.get('x-html-edge-cache-status')).toContain("Miss,FetchedOrigin,CachingAsync");
});
test("301 redirect ", async () => {
await new Promise((r) => setTimeout(r, 4000));
response = await fetch(URL + GET2, { redirect: "manual" });
headers = response.headers;
console.log(URL + GET2);
console.log(response);
console.log(headers);
expect(response.status).toEqual(301);
expect(headers.get('cf-cache-status')).toEqual(HIT);
let status = "Hit,Stale";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
});
test("301 redirect R2", async () => {
await new Promise((r) => setTimeout(r, 4000));
response = await fetch(URL + GET2 + "&cf-cdn=false", { redirect: "manual" });
headers = response.headers;
console.log(URL + GET2);
console.log(response);
console.log(headers);
expect(response.status).toEqual(301);
expect(headers.get('r2')).toEqual("true");
expect(headers.get('cf-cache-status')).toEqual(HIT);
let status = "FromR2,SavedCDNasync,Hit";
expect(headers.get('x-html-edge-cache-status')).toContain(status);
});
})