Skip to content

Commit

Permalink
Align content-type tests with updated conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed Mar 30, 2018
1 parent 3c5d93f commit 0753bff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
57 changes: 41 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ async function hyperlink({
}

const status = res.statusCode;

if (status >= 200 && status < 300) {
const contentType = res.headers['content-type'];
if (contentType && asset.type) {
Expand All @@ -202,32 +203,35 @@ async function hyperlink({
const expected = asset.contentType || `A Content-Type compatible with ${asset.type}`;
asset.contentType = matchContentType[1].toLowerCase();
const inferredType = asset._inferType();
if (!asset._isCompatibleWith(inferredType)) {
const skip = shouldSkip(asset.url);

const contentTypeMismatchReport = {
operator: 'content-type-mismatch',
name: `content-type-mismatch ${asset.urlOrDescription}`,
expected,
actual: contentType,
at: [...new Set(relations.map(r => r.debugDescription))].join('\n '),
};

if (!shouldSkip(contentTypeMismatchReport)) {
reportTest({
ok: false,
skip,
name: `${asset.urlOrDescription}: Should have the expected Content-Type`,
operator: 'error',
expected,
actual: contentType,
at: [...new Set(relations.map(r => r.debugDescription))].join('\n '),
...contentTypeMismatchReport,
ok: asset._isCompatibleWith(inferredType)
});
}
}
} else if (!contentType) {
const skip = shouldSkip(asset.url);

reportTest({
const contentTypeMisingReport = {
ok: false,
skip,
name: `${asset.urlOrDescription}: No Content-Type response header`,
operator: 'error',
name: `content-type-missing ${asset.urlOrDescription}`,
operator: 'content-type-missing',
expected: asset.contentType || `A Content-Type compatible with ${asset.type}`,
actual: contentType,
at: [...new Set(relations.map(r => r.debugDescription))].join('\n '),
});
};

if (!shouldSkip(contentTypeMisingReport)) {
reportTest(contentTypeMisingReport);
};
}
}

Expand Down Expand Up @@ -327,6 +331,27 @@ async function hyperlink({
}

function handleError(error) {
// Explicitly handle incompatible types warning
if (error.stack && error.stack.includes('_warnIncompatibleTypes')) {
const asset = error.asset;
const expected = asset.contentType || `A Content-Type compatible with ${asset.type}`;

const contentTypeMismatchReport = {
ok: false,
operator: 'content-type-mismatch',
name: `content-type-mismatch ${asset.urlOrDescription}`,
expected,
actual: error.message,
at: [...new Set(asset._incoming.map(r => r.debugDescription))].join('\n '),
};

if (!shouldSkip(contentTypeMismatchReport)) {
reportTest(contentTypeMismatchReport)
}

return;
}

const message = error.message || error;
const asset = error.asset || (error.relation && error.relation.to);
const report = {
Expand Down
14 changes: 8 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ describe('hyperlink', function () {
expect(t.push, 'to have a call satisfying', () => {
t.push(null, {
ok: false,
operator: 'error',
actual: 'https://example.com/styles.css: Asset is used as both Css and Png',
operator: 'content-type-mismatch',
name: 'content-type-mismatch https://example.com/styles.css',
expected: 'text/css',
actual: 'Asset is used as both Css and Png',
at: 'https://example.com/ (5:58) <link rel="stylesheet" href="styles.css">'
});
});
Expand Down Expand Up @@ -145,8 +147,8 @@ describe('hyperlink', function () {
expect(t.push, 'to have a call satisfying', () => {
t.push(null, {
ok: false,
operator: 'error',
name: 'https://example.com/hey.png: Should have the expected Content-Type',
operator: 'content-type-mismatch',
name: 'content-type-mismatch https://example.com/hey.png',
expected: 'image/png',
actual: 'text/plain',
at: 'https://example.com/ (6:39) <img src="hey.png">'
Expand Down Expand Up @@ -191,8 +193,8 @@ describe('hyperlink', function () {
expect(t.push, 'to have a call satisfying', () => {
t.push(null, {
ok: false,
operator: 'error',
name: 'https://example.com/hey.png: No Content-Type response header',
operator: 'content-type-missing',
name: 'content-type-missing https://example.com/hey.png',
expected: 'image/png',
at: 'https://example.com/ (6:39) <img src="hey.png">'
});
Expand Down

0 comments on commit 0753bff

Please sign in to comment.