-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
audit(SEO): add http-status-code (#3311)
- Loading branch information
1 parent
f91ee78
commit 8e7695d
Showing
8 changed files
with
250 additions
and
2 deletions.
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}`, | ||
}; | ||
} | ||
|
||
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'); | ||
}); | ||
}); | ||
}); |