forked from brave/browser-laptop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't register a visit if page returns a non-successful HTTP status code
Fixes brave#3759 Auditors: @mrose17, @bridiver Test Plan: 1. Visit https://clifton.io 2. Stay on page for a while 3. Confirm time spent is incremented 4. Visit https://clifton.io/404 5. Stay on page for a while 6. Confirm time spent is not incremented Udpated w/ review feedback from @bridiver
- Loading branch information
Showing
7 changed files
with
175 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
/** | ||
* Returns true if HTTP response code is one we want to collect usage for | ||
* @param {number} responseCode - HTTP response code to be evaluated | ||
* @return {boolean} true if the page should be included, false if not | ||
*/ | ||
module.exports.shouldTrackResponseCode = (responseCode) => { | ||
switch (responseCode) { | ||
case 200: // ok | ||
case 203: // non-authoritative | ||
case 206: // partial content | ||
case 304: // not modified (cached) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* Is page an actual page being viewed by the user? (not an error page, etc) | ||
* If the page is invalid, we don't want to collect usage info. | ||
* @param {Object} view - an entry from page_view (from EventStore) | ||
* @param {Object} responseList - full page_response array (from EventStore) | ||
* @return {boolean} true if page should have usage collected, false if not | ||
*/ | ||
module.exports.shouldTrackView = (view, responseList) => { | ||
if (!view || !view.url || !view.tabId) { | ||
return false | ||
} | ||
if (!responseList || !Array.isArray(responseList) || !responseList.length) { | ||
return false | ||
} | ||
|
||
const tabId = view.tabId | ||
const url = view.url | ||
|
||
for (let i = responseList.length; i > -1; i--) { | ||
const response = responseList[i] | ||
|
||
if (!response) continue | ||
|
||
const responseUrl = response && response.details | ||
? response.details.originalURL || response.details.newURL | ||
: null | ||
|
||
if (url === responseUrl && response.tabId === tabId) { | ||
return module.exports.shouldTrackResponseCode(response.details.httpResponseCode) | ||
} | ||
} | ||
return false | ||
} |
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
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,74 @@ | ||
/* global describe, it */ | ||
const ledgerUtil = require('../../../app/common/lib/ledgerUtil') | ||
const assert = require('assert') | ||
|
||
require('../braveUnit') | ||
|
||
describe('ledgerUtil test', function () { | ||
describe('shouldTrackResponseCode', function () { | ||
describe('expected success codes', function () { | ||
it('returns true for various success responses (200, 203, 206)', function () { | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(200), true) | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(203), true) | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(206), true) | ||
}) | ||
it('returns true for a cached response (304)', function () { | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(304), true) | ||
}) | ||
}) | ||
|
||
describe('expected failure codes', function () { | ||
it('returns false for non-content success codes (used for REST apis, etc)', function () { | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(201), false) // created | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(202), false) // accepted | ||
}) | ||
it('returns false for various server side error responses (500-504)', function () { | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(500), false) // internal server error | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(501), false) // not implemented | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(502), false) // bad gateway | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(503), false) // service unavailable | ||
assert.equal(ledgerUtil.shouldTrackResponseCode(504), false) // gateway timeout | ||
}) | ||
}) | ||
}) | ||
|
||
describe('shouldTrackView', function () { | ||
const validView = { tabId: 1, url: 'https://brave.com/' } | ||
const validResponseList = [{ tabId: validView.tabId, details: { originalURL: validView.url, httpResponseCode: 200 } }] | ||
const noMatchResponseList = [{ tabId: 3, details: {originalURL: 'https://not-brave.com'} }] | ||
const matchButErrored = [{ tabId: validView.tabId, details: { originalURL: validView.url, httpResponseCode: 404 } }] | ||
|
||
describe('input validation', function () { | ||
it('returns false if view is falsey', function () { | ||
assert.equal(ledgerUtil.shouldTrackView(null, validResponseList), false) | ||
}) | ||
it('returns false if view.url is falsey', function () { | ||
assert.equal(ledgerUtil.shouldTrackView({tabId: 1}, validResponseList), false) | ||
}) | ||
it('returns false if view.tabId is falsey', function () { | ||
assert.equal(ledgerUtil.shouldTrackView({url: 'https://brave.com/'}, validResponseList), false) | ||
}) | ||
it('returns false if responseList is falsey', function () { | ||
assert.equal(ledgerUtil.shouldTrackView(validView, null), false) | ||
}) | ||
it('returns false if responseList is not an array', function () { | ||
assert.equal(ledgerUtil.shouldTrackView(validView, {}), false) | ||
}) | ||
it('returns false if responseList is a 0 length array', function () { | ||
assert.equal(ledgerUtil.shouldTrackView(validView, []), false) | ||
}) | ||
}) | ||
|
||
describe('when finding a matching response based on tabId and url', function () { | ||
it('returns false if no match found', function () { | ||
assert.equal(ledgerUtil.shouldTrackView(validView, noMatchResponseList), false) | ||
}) | ||
it('returns false if match is found BUT response code is a failure (ex: 404)', function () { | ||
assert.equal(ledgerUtil.shouldTrackView(validView, matchButErrored), false) | ||
}) | ||
it('returns true when match is found AND response code is a success (ex: 200)', function () { | ||
assert.equal(ledgerUtil.shouldTrackView(validView, validResponseList), true) | ||
}) | ||
}) | ||
}) | ||
}) |