This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 975
Don't register a visit if page returns a non-successful HTTP status code #4032
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* 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' | ||
|
||
const { responseHasContent } = require('./httpUtil') | ||
|
||
/** | ||
* 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.newURL | ||
: null | ||
|
||
if (url === responseUrl && response.tabId === tabId) { | ||
return responseHasContent(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
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,34 @@ | ||
/* global describe, it */ | ||
const httpUtil = require('../../../app/common/lib/httpUtil') | ||
const assert = require('assert') | ||
|
||
require('../braveUnit') | ||
|
||
describe('httpUtil test', function () { | ||
describe('responseHasContent', function () { | ||
describe('expected success codes', function () { | ||
it('returns true for various success responses (200, 203, 206)', function () { | ||
assert.equal(httpUtil.responseHasContent(200), true) | ||
assert.equal(httpUtil.responseHasContent(203), true) | ||
assert.equal(httpUtil.responseHasContent(206), true) | ||
}) | ||
it('returns true for a cached response (304)', function () { | ||
assert.equal(httpUtil.responseHasContent(304), true) | ||
}) | ||
}) | ||
|
||
describe('expected failure codes', function () { | ||
it('returns false for non-content success codes (used for REST apis, etc)', function () { | ||
assert.equal(httpUtil.responseHasContent(201), false) // created | ||
assert.equal(httpUtil.responseHasContent(202), false) // accepted | ||
}) | ||
it('returns false for various server side error responses (500-504)', function () { | ||
assert.equal(httpUtil.responseHasContent(500), false) // internal server error | ||
assert.equal(httpUtil.responseHasContent(501), false) // not implemented | ||
assert.equal(httpUtil.responseHasContent(502), false) // bad gateway | ||
assert.equal(httpUtil.responseHasContent(503), false) // service unavailable | ||
assert.equal(httpUtil.responseHasContent(504), false) // gateway timeout | ||
}) | ||
}) | ||
}) | ||
}) |
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,47 @@ | ||
/* global describe, it */ | ||
const ledgerUtil = require('../../../app/common/lib/ledgerUtil') | ||
const assert = require('assert') | ||
|
||
require('../braveUnit') | ||
|
||
describe('ledgerUtil test', function () { | ||
describe('shouldTrackView', function () { | ||
const validView = { tabId: 1, url: 'https://brave.com/' } | ||
const validResponseList = [{ tabId: validView.tabId, details: { newURL: validView.url, httpResponseCode: 200 } }] | ||
const noMatchResponseList = [{ tabId: 3, details: { newURL: 'https://not-brave.com' } }] | ||
const matchButErrored = [{ tabId: validView.tabId, details: { newURL: 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) | ||
}) | ||
}) | ||
}) | ||
}) |
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.
4xx? Don't necessarily need an exhaustive list, but the list isn't that long https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
can be done in a followup too
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.
done 👍