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

Breaking: Make content-type rule use proper fonts types #425

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 24 additions & 1 deletion src/lib/rules/content-type/content-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,27 @@ const rule: IRuleBuilder = {
return null;
};

const overwriteMediaType = (mediaType: string): string => {

// TODO Temporary fix until the following issue is fixed:
// https://github.com/jshttp/mime-db/issues/77

switch (mediaType) {
case 'application/x-font-otf':
return 'font/otf';
case 'application/font-sfnt':
return 'font/sfnt';
case 'application/x-font-ttf':
return 'font/ttf';
case 'application/font-woff':
return 'font/woff';
case 'application/font-woff2':
return 'font/woff2';
default:
return mediaType;
}
};

const validate = async (fetchEnd: IFetchEnd) => {
const { element, resource, response }: { element: IAsyncHTMLElement, resource: string, response: IResponse } = fetchEnd;

Expand Down Expand Up @@ -227,11 +248,13 @@ const rule: IRuleBuilder = {

// Try to determine the media type and charset of the resource.

const mediaType: string =
let mediaType: string =
determineMediaTypeBasedOnElement(element) ||
determineMediaTypeBasedOnFileType(response.body.rawContent) ||
determineMediaTypeBasedOnFileExtension(resource);

mediaType = overwriteMediaType(mediaType);

const charset: string = determineCharset(mediaType, originalMediaType);

// Check if the determined values differ
Expand Down
Binary file added tests/lib/rules/content-type/fixtures/test.woff2
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/lib/rules/content-type/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const ruleName = getRuleName(__dirname);

const pngFileContent = fs.readFileSync(`${__dirname}/fixtures/image.png`); // eslint-disable-line no-sync
const svgFileContent = '<svg xmlns="http://www.w3.org/2000/svg"><path d="M1,1"/></svg>';
// const woff2FileContent = fs.readFileSync(`${__dirname}/fixtures/test.woff2`);

const incorrectCharsetMessage = `'content-type' header should have 'charset=utf-8' (not 'iso-8859-1')`;
const invalidMediaTypeMessage = `'content-type' header value is invalid (invalid media type)`;
Expand Down Expand Up @@ -145,6 +146,30 @@ const testsForDefaults: Array<IRuleTest> = [

// `Content-Type` value contain wrong `media type`.

// TODO: Enable when jsdom supports downloading fonts.
// {
// name: `WOFF2 font is served with 'Content-Type' header with the wrong media type`,
// reports: [{ message: generateIncorrectMediaTypeMessage('font/woff2', 'application/font-woff2') }],
// serverConfig: {
// '/': generateHTMLPageData(generateHTMLPage(`
// <style>
// @font-face {
// font-family: 'Open Sans';
// font-style: normal;
// font-weight: 400;
// src: local('Open Sans Regular'), local('OpenSans-Regular'), url(test.woff2) format('woff2');
// }
//
// body {
// font-family: 'Open Sans';
// }
// </style>`, 'a')),
// '/test.woff2': {
// content: woff2FileContent,
// headers: { 'Content-Type': 'application/font-woff2' }
// }
// }
// },
{
name: `Image is served with 'Content-Type' header with the wrong media type`,
reports: [{ message: generateIncorrectMediaTypeMessage('image/png', 'font/woff') }],
Expand Down