Introduces src/types.d.ts and test/types.d.ts for Cloudflare Workers and test utilities. Enhances JSDoc comments and type annotations across test files for better type safety and clarity. Updates tsconfig.json to use bundler module resolution and include additional type sources. Minor test logic and assertion improvements for robustness.
62 lines
1.3 KiB
JavaScript
62 lines
1.3 KiB
JavaScript
/**
|
|
* Custom assertions and validation helpers
|
|
*/
|
|
|
|
/**
|
|
* Validate response headers for security
|
|
* @param {Response} response - Response to validate
|
|
* @returns {{passed: boolean, missing: string[], present: string[]}} Validation results
|
|
*/
|
|
export function validateSecurityHeaders(response) {
|
|
const requiredHeaders = [
|
|
'Strict-Transport-Security',
|
|
'X-Frame-Options',
|
|
'X-XSS-Protection',
|
|
'Content-Security-Policy',
|
|
'Referrer-Policy'
|
|
];
|
|
|
|
const results = {
|
|
passed: true,
|
|
/** @type {string[]} */
|
|
missing: [],
|
|
/** @type {string[]} */
|
|
present: []
|
|
};
|
|
|
|
requiredHeaders.forEach(header => {
|
|
if (response.headers.has(header)) {
|
|
results.present.push(header);
|
|
} else {
|
|
results.missing.push(header);
|
|
results.passed = false;
|
|
}
|
|
});
|
|
|
|
return results;
|
|
}
|
|
|
|
/**
|
|
* Assert that a URL is valid
|
|
* @param {string} url - URL to validate
|
|
* @returns {boolean} True if valid
|
|
*/
|
|
export function isValidUrl(url) {
|
|
try {
|
|
new URL(url);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Assert that response has security headers
|
|
* @param {Response} response - Response to check
|
|
* @returns {boolean} True if has all security headers
|
|
*/
|
|
export function hasSecurityHeaders(response) {
|
|
const validation = validateSecurityHeaders(response);
|
|
return validation.passed;
|
|
}
|