Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for parse-cache-control #8866

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions tests/builders/tests/unit/parse-cache-control-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { module, test } from 'qunit';

import { parseCacheControl } from '@ember-data/request-utils';
import { test as debug } from '@ember-data/unpublished-test-infra/test-support/test-in-debug';

module('parseCacheControl', function (hooks) {
test('should parse a single Cache-Control directive', function (assert) {
const header = 'max-age=3600';
const result = parseCacheControl(header);
assert.deepEqual(result, { 'max-age': 3600 });
});

test('should parse multiple Cache-Control directives', function (assert) {
const header = 'max-age=3600, must-revalidate';
const result = parseCacheControl(header);
assert.deepEqual(result, { 'max-age': 3600, 'must-revalidate': true });
});

test('should parse Cache-Control directives with multiple delta-seconds values', function (assert) {
const header = 'max-age=3600, s-maxage=7200';
const result = parseCacheControl(header);
assert.deepEqual(result, { 'max-age': 3600, 's-maxage': 7200 });
});

test('should parse Cache-Control directives with a single token value', function (assert) {
const header = 'no-cache';
const result = parseCacheControl(header);
assert.deepEqual(result, { 'no-cache': true });
});

test('should parse Cache-Control directives with multiple token values', function (assert) {
const header = 'no-cache, no-store';
const result = parseCacheControl(header);
assert.deepEqual(result, { 'no-cache': true, 'no-store': true });
});

test('should parse Cache-Control directives with a single byte-range-set value', function (assert) {
const header =
'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';
const result = parseCacheControl(header);
assert.deepEqual(result, {
'max-age': 3600,
'no-transform': true,
'only-if-cached': true,
public: true,
'must-revalidate': true,
'proxy-revalidate': true,
'no-cache': true,
's-maxage': 7200,
'stale-while-revalidate': 3600,
'stale-if-error': 7200,
immutable: true,
});
});

debug('throws when Cache-Control has invalid directives', async function (assert) {
await assert.expectAssertion(() => {
const header = 'max-age=,';
parseCacheControl(header);
}, /Assertion Failed: Invalid Cache-Control value, expected a value after "=" but got ","/);
});
runspired marked this conversation as resolved.
Show resolved Hide resolved

debug('throws when Cache-Control has invalid value type', async function (assert) {
await assert.expectAssertion(() => {
const header = 'max-age="3600"';
parseCacheControl(header);
}, /some useful error about why passing number in quotes is forbidden and would not work/);
});
});