|
| 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