Skip to content

Commit

Permalink
Add regex tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Beytoven committed Feb 4, 2020
1 parent e8d6a59 commit 0819a00
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lighthouse-core/audits/dobetterweb/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const UIStrings = {
const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);

const CONTENT_TYPE_HEADER = 'content-type';
const CHARSET_META_REGEX = /<meta.*charset="?.{1,}"?.*>/gm;
const CHARSET_HTML_REGEX = /<meta.*charset="?.{1,}"?.*>/gm;
const CHARSET_HTTP_REGEX = /charset=.{1,}/gm;

class CharsetDefined extends Audit {
Expand Down Expand Up @@ -70,7 +70,7 @@ class CharsetDefined extends Audit {

// Check if charset is defined within the first 1024 characters(~1024 bytes) of the HTML document
charsetIsSet = charsetIsSet ||
artifacts.MainDocumentContent.slice(0, 1024).match(CHARSET_META_REGEX) !== null;
artifacts.MainDocumentContent.slice(0, 1024).match(CHARSET_HTML_REGEX) !== null;

return {
score: Number(charsetIsSet),
Expand All @@ -80,5 +80,5 @@ class CharsetDefined extends Audit {

module.exports = CharsetDefined;
module.exports.UIStrings = UIStrings;
module.exports.CHARSET_META_REGEX = CHARSET_META_REGEX;
module.exports.CHARSET_HTML_REGEX = CHARSET_HTML_REGEX;
module.exports.CHARSET_HTTP_REGEX = CHARSET_HTTP_REGEX;
19 changes: 18 additions & 1 deletion lighthouse-core/test/audits/dobetterweb/charset-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function generateArtifacts(htmlContent, contentTypeValue = 'text/html') {
}

describe('Charset defined audit', () => {
it('succeeds when the page contains the charset meta tag', () => {
it('succeeds wheharsetDefinedAudit.CHARSET_HTTP_REGEthe page contains the charset meta tag', () => {

This comment has been minimized.

Copy link
@paulirish

paulirish Feb 5, 2020

Member

Mispaste?

This comment has been minimized.

Copy link
@connorjclark

connorjclark Feb 6, 2020

Collaborator

vscode does this too me a lot lately

const htmlContent = '<meta charset="utf-8" />';
const artifacts = generateArtifacts(htmlContent);
const context = {computedCache: new Map()};
Expand Down Expand Up @@ -107,3 +107,20 @@ describe('Charset defined audit', () => {
});
});
});

describe('Charset regex check', () => {
it('passes if html charset declaration has no quotes', () => {
const charsetHTML = '<meta charset=utf-8 />';
assert.equal(CharsetDefinedAudit.CHARSET_HTML_REGEX.test(charsetHTML), true);
});

it('passes if html charset declaration tag is left open', () => {
const charsetHTML = '<meta charset="utf-8">';
assert.equal(CharsetDefinedAudit.CHARSET_HTML_REGEX.test(charsetHTML), true);
});

it('fails if charset declaration has an empty value', () => {
const charsetHTTP = 'text/html; chartype=';
assert.equal(CharsetDefinedAudit.CHARSET_HTTP_REGEX.test(charsetHTTP), false);
});
});

0 comments on commit 0819a00

Please sign in to comment.