Skip to content

Commit 1611040

Browse files
authored
Add tests for parse-cache-control (#8866)
* Add happy path tests for parse-cache-control * Added assertion and test to cover
1 parent 7bb299f commit 1611040

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

packages/request-utils/src/index.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ export function parseCacheControl(header: string): CacheControlValue {
518518
let isParsingKey = true;
519519
let cacheControlValue: CacheControlValue = {};
520520

521+
function parseCacheControlValue(stringToParse: string): number {
522+
const parsedValue = Number.parseInt(stringToParse);
523+
assert(`Invalid Cache-Control value, expected a number but got - ${stringToParse}`, !Number.isNaN(parsedValue));
524+
return parsedValue;
525+
}
526+
521527
for (let i = 0; i < header.length; i++) {
522528
let char = header.charAt(i);
523529
if (char === ',') {
@@ -527,7 +533,7 @@ export function parseCacheControl(header: string): CacheControlValue {
527533
i === 0 || header.charAt(i - 1) !== '='
528534
);
529535
isParsingKey = true;
530-
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? Number.parseInt(value) : true;
536+
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
531537
key = '';
532538
value = '';
533539
continue;
@@ -543,7 +549,7 @@ export function parseCacheControl(header: string): CacheControlValue {
543549
}
544550

545551
if (i === header.length - 1) {
546-
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? Number.parseInt(value) : true;
552+
cacheControlValue[key] = NUMERIC_KEYS.has(key) ? parseCacheControlValue(value) : true;
547553
}
548554
}
549555

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { module, test } from 'qunit';
2+
3+
import { parseCacheControl } from '@ember-data/request-utils';
4+
import { test as debug } from '@ember-data/unpublished-test-infra/test-support/test-in-debug';
5+
6+
module('parseCacheControl', function (hooks) {
7+
test('should parse a single Cache-Control directive', function (assert) {
8+
const header = 'max-age=3600';
9+
const result = parseCacheControl(header);
10+
assert.deepEqual(result, { 'max-age': 3600 });
11+
});
12+
13+
test('should parse multiple Cache-Control directives', function (assert) {
14+
const header = 'max-age=3600, must-revalidate';
15+
const result = parseCacheControl(header);
16+
assert.deepEqual(result, { 'max-age': 3600, 'must-revalidate': true });
17+
});
18+
19+
test('should parse Cache-Control directives with multiple delta-seconds values', function (assert) {
20+
const header = 'max-age=3600, s-maxage=7200';
21+
const result = parseCacheControl(header);
22+
assert.deepEqual(result, { 'max-age': 3600, 's-maxage': 7200 });
23+
});
24+
25+
test('should parse Cache-Control directives with a single token value', function (assert) {
26+
const header = 'no-cache';
27+
const result = parseCacheControl(header);
28+
assert.deepEqual(result, { 'no-cache': true });
29+
});
30+
31+
test('should parse Cache-Control directives with multiple token values', function (assert) {
32+
const header = 'no-cache, no-store';
33+
const result = parseCacheControl(header);
34+
assert.deepEqual(result, { 'no-cache': true, 'no-store': true });
35+
});
36+
37+
test('should parse Cache-Control directives with a single byte-range-set value', function (assert) {
38+
const header =
39+
'max-age=3600, no-transform, only-if-cached, public, must-revalidate, proxy-revalidate, no-cache, s-maxage=7200, stale-while-revalidate=3600, stale-if-error=7200, immutable';
40+
const result = parseCacheControl(header);
41+
assert.deepEqual(result, {
42+
'max-age': 3600,
43+
'no-transform': true,
44+
'only-if-cached': true,
45+
public: true,
46+
'must-revalidate': true,
47+
'proxy-revalidate': true,
48+
'no-cache': true,
49+
's-maxage': 7200,
50+
'stale-while-revalidate': 3600,
51+
'stale-if-error': 7200,
52+
immutable: true,
53+
});
54+
});
55+
56+
debug('throws when Cache-Control has invalid directives', async function (assert) {
57+
await assert.expectAssertion(() => {
58+
const header = 'max-age=,';
59+
parseCacheControl(header);
60+
}, /Assertion Failed: Invalid Cache-Control value, expected a value after "=" but got ","/);
61+
});
62+
63+
debug('throws when Cache-Control has invalid value type', async function (assert) {
64+
await assert.expectAssertion(() => {
65+
const header = 'max-age="3600"';
66+
parseCacheControl(header);
67+
}, /Assertion Failed: Invalid Cache-Control value, expected a number but got - "3600"/);
68+
});
69+
});

0 commit comments

Comments
 (0)