From 8e7695dcbef6f0b9c4435beb558f6b4cc927df49 Mon Sep 17 00:00:00 2001 From: Konrad Dzwinel Date: Thu, 28 Sep 2017 01:39:53 +0200 Subject: [PATCH] audit(SEO): add http-status-code (#3311) --- lighthouse-cli/test/fixtures/static-server.js | 5 ++ .../test/smokehouse/seo/expectations.js | 11 ++- .../audits/seo/http-status-code.js | 55 ++++++++++++++ .../closure/typedefs/ComputedArtifacts.js | 3 + lighthouse-core/config/seo.js | 2 + .../gather/computed/main-resource.js | 49 ++++++++++++ .../test/audits/seo/http-status-code-test.js | 51 +++++++++++++ .../gather/computed/main-resource-test.js | 76 +++++++++++++++++++ 8 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 lighthouse-core/audits/seo/http-status-code.js create mode 100644 lighthouse-core/gather/computed/main-resource.js create mode 100644 lighthouse-core/test/audits/seo/http-status-code-test.js create mode 100644 lighthouse-core/test/gather/computed/main-resource-test.js diff --git a/lighthouse-cli/test/fixtures/static-server.js b/lighthouse-cli/test/fixtures/static-server.js index 814f7c61dd7f..ff12b97baec1 100644 --- a/lighthouse-cli/test/fixtures/static-server.js +++ b/lighthouse-cli/test/fixtures/static-server.js @@ -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 diff --git a/lighthouse-cli/test/smokehouse/seo/expectations.js b/lighthouse-cli/test/smokehouse/seo/expectations.js index 9aba9407e93f..831cc6951362 100644 --- a/lighthouse-cli/test/smokehouse/seo/expectations.js +++ b/lighthouse-cli/test/smokehouse/seo/expectations.js @@ -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, @@ -42,6 +45,10 @@ module.exports = [ 'meta-description': { score: false, }, + 'http-status-code': { + score: false, + displayValue: '403', + }, }, }, ]; diff --git a/lighthouse-core/audits/seo/http-status-code.js b/lighthouse-core/audits/seo/http-status-code.js new file mode 100644 index 000000000000..97ab5000ea26 --- /dev/null +++ b/lighthouse-core/audits/seo/http-status-code.js @@ -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}`, + }; + } + + return { + rawValue: true, + }; + }); + } +} + +module.exports = HTTPStatusCode; diff --git a/lighthouse-core/closure/typedefs/ComputedArtifacts.js b/lighthouse-core/closure/typedefs/ComputedArtifacts.js index 834bb2a5c7a5..2d5981d8e766 100644 --- a/lighthouse-core/closure/typedefs/ComputedArtifacts.js +++ b/lighthouse-core/closure/typedefs/ComputedArtifacts.js @@ -69,3 +69,6 @@ ComputedArtifacts.prototype.requestTraceOfTab; /** @type {function(!Trace): !Promise<{timeInMs: number, timestamp: number}>} */ ComputedArtifacts.prototype.requestFirstInteractive; + +/** @type {function(!DevtoolsLog): !Promise} */ +ComputedArtifacts.prototype.requestMainResource; diff --git a/lighthouse-core/config/seo.js b/lighthouse-core/config/seo.js index 60fedd7ead12..3f9e18f078c3 100644 --- a/lighthouse-core/config/seo.js +++ b/lighthouse-core/config/seo.js @@ -15,6 +15,7 @@ module.exports = { }], audits: [ 'seo/meta-description', + 'seo/http-status-code', ], groups: { 'seo-mobile': { @@ -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'}, ], }, }, diff --git a/lighthouse-core/gather/computed/main-resource.js b/lighthouse-core/gather/computed/main-resource.js new file mode 100644 index 000000000000..9f5f883d19ce --- /dev/null +++ b/lighthouse-core/gather/computed/main-resource.js @@ -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; diff --git a/lighthouse-core/test/audits/seo/http-status-code-test.js b/lighthouse-core/test/audits/seo/http-status-code-test.js new file mode 100644 index 000000000000..972945491baa --- /dev/null +++ b/lighthouse-core/test/audits/seo/http-status-code-test.js @@ -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); + }); + }); +}); diff --git a/lighthouse-core/test/gather/computed/main-resource-test.js b/lighthouse-core/test/gather/computed/main-resource-test.js new file mode 100644 index 000000000000..a8c745d72255 --- /dev/null +++ b/lighthouse-core/test/gather/computed/main-resource-test.js @@ -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'); + }); + }); +});