-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
brendankenny
merged 12 commits into
GoogleChrome:master
from
kdzwinel:seo-http-status-code
Sep 27, 2017
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9e672e4
SEO: HTTP Status Code Audit - WIP
kdzwinel 4c1b723
Add tests.
kdzwinel 3c17dc1
Add status code audit.
kdzwinel 7fa834c
Validate copy in tests, remove redundant data check.
kdzwinel 8c5943d
Update copy
kdzwinel 23373de
Add smoke tests.
kdzwinel 7535b5e
Allow returning custom HTTP code from static-server. Update smokehous…
kdzwinel 0a2ca8c
Replace status code gatherer with a main resource computed artifact.
kdzwinel b16b37a
Make linter happy.
kdzwinel dbb5f51
Remove abandoned smoke test.
kdzwinel 711ce65
Code review fixes.
kdzwinel 8fe5acd
Throw when main resource can't be identified. Ajust tests. Add test b…
kdzwinel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}; | ||
} | ||
|
||
return { | ||
rawValue: true, | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = HTTPStatusCode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
lighthouse-core/test/gather/computed/main-resource-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, added!