1 | import test from 'ava';
|
2 | import tk from 'timekeeper';
|
3 | import addSeconds from 'date-fns/add_seconds';
|
4 | import expired from '../';
|
5 |
|
6 | test('expired.in is a function', t => {
|
7 | t.is(typeof expired.in, 'function');
|
8 | });
|
9 |
|
10 | test('expired.in returns positive ms for valid cache', t => {
|
11 | const date = new Date().toUTCString();
|
12 | const maxAge = 300;
|
13 | const headers = {
|
14 | date,
|
15 | age: 0,
|
16 | 'cache-control': `public, max-age=${maxAge}`
|
17 | };
|
18 | const expiredIn = maxAge * 1000;
|
19 |
|
20 | tk.freeze(date);
|
21 | t.is(expired.in(headers), expiredIn);
|
22 | tk.reset();
|
23 | });
|
24 |
|
25 | test('expired.in returns zero ms for instantly stale cache', t => {
|
26 | const date = new Date().toUTCString();
|
27 | const headers = {
|
28 | date,
|
29 | age: 0,
|
30 | 'cache-control': `public, max-age=0`
|
31 | };
|
32 | const expiredIn = 0;
|
33 |
|
34 | tk.freeze(date);
|
35 | t.is(expired.in(headers), expiredIn);
|
36 | tk.reset();
|
37 | });
|
38 |
|
39 | test('expired.in returns negative ms for stale cache', t => {
|
40 | const date = new Date().toUTCString();
|
41 | const dateOffset = -600;
|
42 | const maxAge = 300;
|
43 | const headers = {
|
44 | date: addSeconds(date, dateOffset).toUTCString(),
|
45 | age: 0,
|
46 | 'cache-control': `public, max-age=${maxAge}`
|
47 | };
|
48 | const expiredIn = (maxAge + dateOffset) * 1000;
|
49 |
|
50 | tk.freeze(date);
|
51 | t.is(expired.in(headers), expiredIn);
|
52 | tk.reset();
|
53 | });
|
54 |
|
55 | test('expired.in accepts currentDate argument', t => {
|
56 | const date = new Date(new Date().toUTCString());
|
57 | const headers = {
|
58 | date: date.toUTCString(),
|
59 | age: 0,
|
60 | 'cache-control': 'public, max-age=300'
|
61 | };
|
62 |
|
63 | t.is(expired.in(headers, date), 300000);
|
64 | t.is(expired.in(headers, addSeconds(date, 500)), -200000);
|
65 | });
|