Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
59 changes: 59 additions & 0 deletions utils/cacheControl.empty-fallback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it } from 'vitest';
import { buildCacheControlHeader } from './cacheControl';

describe('buildCacheControlHeader - Edge Cases & Empty/Missing Inputs Verification', () => {
it('1. falls back to default cache control when options object is completely omitted', () => {
// Calling the function with no arguments should fallback gracefully without throwing
expect(buildCacheControlHeader()).toBe('s-maxage=3600, stale-while-revalidate=86400');
});

it('2. falls back to default cache control when options is an empty object', () => {
expect(buildCacheControlHeader({})).toBe('s-maxage=3600, stale-while-revalidate=86400');
});

it('3. falls back to default cache control when options object is null', () => {
// Casting to any to test JavaScript-level null input resilience
expect(buildCacheControlHeader(null as any)).toBe(
's-maxage=3600, stale-while-revalidate=86400'
);
});

it('4. falls back to default cache control when options contain only undefined or null values', () => {
expect(
buildCacheControlHeader({
bypass: undefined,
secondsToMidnight: undefined,
isHistoricalYear: undefined,
})
).toBe('s-maxage=3600, stale-while-revalidate=86400');

expect(
buildCacheControlHeader({
bypass: null as any,
secondsToMidnight: null as any,
isHistoricalYear: null as any,
})
).toBe('s-maxage=3600, stale-while-revalidate=86400');
});

it('5. prioritizes bypass=true even if other options are invalid/null/undefined', () => {
expect(
buildCacheControlHeader({
bypass: true,
secondsToMidnight: null as any,
isHistoricalYear: undefined,
})
).toBe('no-cache, no-store, must-revalidate');
});

it('6. prioritizes isHistoricalYear=true when bypass is falsy and secondsToMidnight is null/undefined', () => {
expect(
buildCacheControlHeader({
bypass: false,
secondsToMidnight: undefined,
isHistoricalYear: true,
})
).toBe('public, s-maxage=31536000, immutable');
});
});
9 changes: 3 additions & 6 deletions utils/cacheControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ interface CacheControlOptions {
isHistoricalYear?: boolean;
}

export function buildCacheControlHeader({
bypass,
secondsToMidnight,
isHistoricalYear,
}: CacheControlOptions): string {
export function buildCacheControlHeader(options: CacheControlOptions = {}): string {
const { bypass, secondsToMidnight, isHistoricalYear } = options || {};
if (bypass) {
return 'no-cache, no-store, must-revalidate';
}
Expand All @@ -17,7 +14,7 @@ export function buildCacheControlHeader({
return 'public, s-maxage=31536000, immutable';
}

if (secondsToMidnight !== undefined) {
if (secondsToMidnight !== undefined && secondsToMidnight !== null) {
return `public, s-maxage=${secondsToMidnight}, stale-while-revalidate=86400`;
}

Expand Down
Loading