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

new SEO audit: HTTP status code #3311

Merged
merged 12 commits into from
Sep 27, 2017
5 changes: 5 additions & 0 deletions lighthouse-cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ function requestHandler(request, response) {
} else if (filePath.endsWith('.svg')) {
headers = {'Content-Type': 'image/svg+xml'};
}

if (queryString && typeof queryString.status_code !== 'undefined') {
statusCode = parseInt(queryString.status_code, 10);
}

response.writeHead(statusCode, headers);

// Delay the response by the specified ms defaulting to 2000ms for non-numeric values
Expand Down
11 changes: 9 additions & 2 deletions lighthouse-cli/test/smokehouse/seo/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ module.exports = [
'meta-description': {
score: true,
},
'http-status-code': {
score: true,
},
},
},
{
initialUrl: 'http://localhost:10200/seo/seo-failure-cases.html',
url: 'http://localhost:10200/seo/seo-failure-cases.html',
initialUrl: 'http://localhost:10200/seo/seo-failure-cases.html?status_code=403',
url: 'http://localhost:10200/seo/seo-failure-cases.html?status_code=403',
audits: {
'viewport': {
score: false,
Expand All @@ -42,6 +45,10 @@ module.exports = [
'meta-description': {
score: false,
},
'http-status-code': {
score: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like displayValue can also be asserted on this failure?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, added!

displayValue: '403',
},
},
},
];
55 changes: 55 additions & 0 deletions lighthouse-core/audits/seo/http-status-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const Audit = require('../audit');
const HTTP_UNSUCCESSFUL_CODE_LOW = 400;
const HTTP_UNSUCCESSFUL_CODE_HIGH = 599;

class HTTPStatusCode extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Crawling and Indexing',
name: 'http-status-code',
description: 'Page has successful HTTP status code.',
failureDescription: 'Page has unsuccessful HTTP status code',
helpText: 'Pages with unsuccessful HTTP status codes may not be indexed properly. ' +
'[Learn more]' +
'(https://developers.goole.com/web/tools/lighthouse/audits/successful-http-code).',
requiredArtifacts: ['devtoolsLogs'],
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
const devtoolsLogs = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];

return artifacts.requestMainResource(devtoolsLogs)
.then(mainResource => {
const statusCode = mainResource.statusCode;

if (statusCode >= HTTP_UNSUCCESSFUL_CODE_LOW &&
statusCode <= HTTP_UNSUCCESSFUL_CODE_HIGH) {
return {
rawValue: false,
displayValue: `${statusCode}`,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

displayValue is appended to failureDescription:
screen shot 2017-09-12 at 22 39 57

};
}

return {
rawValue: true,
};
});
}
}

module.exports = HTTPStatusCode;
3 changes: 3 additions & 0 deletions lighthouse-core/closure/typedefs/ComputedArtifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ ComputedArtifacts.prototype.requestTraceOfTab;

/** @type {function(!Trace): !Promise<{timeInMs: number, timestamp: number}>} */
ComputedArtifacts.prototype.requestFirstInteractive;

/** @type {function(!DevtoolsLog): !Promise<WebInspector.NetworkRequest>} */
ComputedArtifacts.prototype.requestMainResource;
2 changes: 2 additions & 0 deletions lighthouse-core/config/seo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
}],
audits: [
'seo/meta-description',
'seo/http-status-code',
],
groups: {
'seo-mobile': {
Expand All @@ -39,6 +40,7 @@ module.exports = {
{id: 'viewport', weight: 1, group: 'seo-mobile'},
{id: 'document-title', weight: 1, group: 'seo-content'},
{id: 'meta-description', weight: 1, group: 'seo-content'},
{id: 'http-status-code', weight: 1, group: 'seo-crawl'},
],
},
},
Expand Down
49 changes: 49 additions & 0 deletions lighthouse-core/gather/computed/main-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const ComputedArtifact = require('./computed-artifact');
const HTTP_REDIRECT_CODE_LOW = 300;
const HTTP_REDIRECT_CODE_HIGH = 399;

/**
* @fileoverview This artifact identifies the main resource on the page. Current solution assumes
* that the main resource is the first non-rediected one.
*/
class MainResource extends ComputedArtifact {
get name() {
return 'MainResource';
}

/**
* @param {WebInspector.NetworkRequest} record
* @return {boolean}
*/
isMainResource(request) {
return request.statusCode < HTTP_REDIRECT_CODE_LOW ||
request.statusCode > HTTP_REDIRECT_CODE_HIGH;
}

/**
* @param {!DevtoolsLog} devtoolsLog
* @param {!ComputedArtifacts} artifacts
* @return {!WebInspector.NetworkRequest}
*/
compute_(devtoolsLog, artifacts) {
return artifacts.requestNetworkRecords(devtoolsLog)
.then(requests => {
const mainResoruce = requests.find(this.isMainResource);

if (!mainResoruce) {
throw new Error('Unable to identify the main resource');
}

return mainResoruce;
});
}
}

module.exports = MainResource;
51 changes: 51 additions & 0 deletions lighthouse-core/test/audits/seo/http-status-code-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const HTTPStatusCodeAudit = require('../../../audits/seo/http-status-code.js');
const assert = require('assert');

/* eslint-env mocha */

describe('SEO: HTTP code audit', () => {
it('fails when status code is unsuccesfull', () => {
const statusCodes = [403, 404, 500];

const allRuns = statusCodes.map(statusCode => {
const mainResource = {
statusCode,
};
const artifacts = {
devtoolsLogs: {[HTTPStatusCodeAudit.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(),
requestMainResource: () => Promise.resolve(mainResource),
};

return HTTPStatusCodeAudit.audit(artifacts).then(auditResult => {
assert.equal(auditResult.rawValue, false);
assert.ok(auditResult.displayValue.includes(statusCode), false);
});
});

return Promise.all(allRuns);
});

it('passes when status code is successful', () => {
const mainResource = {
statusCode: 200,
};

const artifacts = {
devtoolsLogs: {[HTTPStatusCodeAudit.DEFAULT_PASS]: []},
requestNetworkRecords: () => Promise.resolve(),
requestMainResource: () => Promise.resolve(mainResource),
};

return HTTPStatusCodeAudit.audit(artifacts).then(auditResult => {
assert.equal(auditResult.rawValue, true);
});
});
});
76 changes: 76 additions & 0 deletions lighthouse-core/test/gather/computed/main-resource-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @license Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/* eslint-env mocha */

const Runner = require('../../../runner.js');
const assert = require('assert');

describe('MainResource computed artifact', () => {
let computedArtifacts;

beforeEach(() => {
computedArtifacts = Runner.instantiateComputedArtifacts();
});

it('returns an artifact', () => {
const record = {
statusCode: 404,
};
const networkRecords = [
record,
];
computedArtifacts.requestNetworkRecords = _ => Promise.resolve(networkRecords);

return computedArtifacts.requestMainResource({}).then(output => {
assert.equal(output, record);
});
});

it('thows when main resource can\'t be found', () => {
const networkRecords = [
{
statusCode: 302,
},
];
computedArtifacts.requestNetworkRecords = _ => Promise.resolve(networkRecords);

return computedArtifacts.requestMainResource({}).then(() => {
assert.ok(false, 'should have thrown');
}).catch(err => {
assert.equal(err.message, 'Unable to identify the main resource');
});
});

it('should ignore redirects', () => {
const record = {
statusCode: 404,
};
const networkRecords = [
{
statusCode: 301,
},
{
statusCode: 302,
},
record,
];
computedArtifacts.requestNetworkRecords = _ => Promise.resolve(networkRecords);

return computedArtifacts.requestMainResource({}).then(output => {
assert.equal(output, record);
});
});

it('should identify correct main resource in the wikipedia fixture', () => {
const wikiDevtoolsLog = require('../../fixtures/wikipedia-redirect.devtoolslog.json');

return computedArtifacts.requestMainResource(wikiDevtoolsLog).then(output => {
assert.equal(output.url, 'https://en.m.wikipedia.org/wiki/Main_Page');
});
});
});