Skip to content
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

[FIX]Ignore cache requests & no track incognito host Issue #15 #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ isChrome = () => {
};

headersReceivedListener = (requestDetails) => {
// Do not count bytes from requests from local cache
if (requestDetails.fromCache) return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Suggested change
if (requestDetails.fromCache) return
if (requestDetails.fromCache || requestDetails.incognito) {
return;
}

And remove CONST_INCOGNITO


let CONST_INCOGNITO = 'Incognito';
cguignol marked this conversation as resolved.
Show resolved Hide resolved

if (isChrome()) {
const origin = extractHostname(!requestDetails.initiator ? requestDetails.url : requestDetails.initiator);
// If Incognito request we do not track the domain origin
const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(!requestDetails.initiator ? requestDetails.url : requestDetails.initiator));
cguignol marked this conversation as resolved.
Show resolved Hide resolved
const responseHeadersContentLength = requestDetails.responseHeaders.find(element => element.name.toLowerCase() === "content-length");
const contentLength = undefined === responseHeadersContentLength ? {value: 0}
: responseHeadersContentLength;
Expand All @@ -38,7 +44,8 @@ headersReceivedListener = (requestDetails) => {
let filter = browser.webRequest.filterResponseData(requestDetails.requestId);

filter.ondata = event => {
const origin = extractHostname(!requestDetails.originUrl ? requestDetails.url : requestDetails.originUrl);
// If Incognito request we do not track the domain origin
const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(!requestDetails.originUrl ? requestDetails.url : requestDetails.originUrl));
cguignol marked this conversation as resolved.
Show resolved Hide resolved
setByteLengthPerOrigin(origin, event.data.byteLength);

filter.write(event.data);
Expand All @@ -51,7 +58,7 @@ headersReceivedListener = (requestDetails) => {
return {};
};

setBrowserIcon = (type) => {
const setBrowserIcon = (type) => {
chrome.browserAction.setIcon({path: `icons/icon-${type}-48.png`});
};

Expand Down
38 changes: 38 additions & 0 deletions test/test-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ describe('headersReceivedListener', function () {
let requestDetails = {};
// backup for spied methods
const DOMAIN_NAME = 'http://www.spotify.com';
const INCOGNITO_CONST = 'Test';
const extractHostNameBackup = extractHostname;
const setByteLengthPerOriginBackup = setByteLengthPerOrigin;

Expand Down Expand Up @@ -210,6 +211,7 @@ describe('headersReceivedListener', function () {
done();
});


it('should call extractHostname with Initiator when it is provided from parameter (Chrome Browser behavior)', function (done) {
extractHostname = chai.spy();

Expand All @@ -221,6 +223,31 @@ describe('headersReceivedListener', function () {
done();
});

it('should use incognito as Origin when Incognito is provided from parameter (Chrome Browser behavior)', function (done) {
extractHostname = chai.spy();
setByteLengthPerOrigin = chai.spy();

requestDetails.incognito = INCOGNITO_CONST;

headersReceivedListener(requestDetails);

expect(extractHostname).to.not.have.been.called();
expect(setByteLengthPerOrigin).to.have.been.called.with('Incognito');
done();
});

it('should use incognito as Origin when Incognito is provided from parameter (Mozilla Firefox Browser behavior)', function (done) {
extractHostname = chai.spy();
setByteLengthPerOrigin = chai.spy();

requestDetails.incognito = INCOGNITO_CONST;
headersReceivedListener(requestDetails);

expect(extractHostname).to.not.have.been.called();
expect(setByteLengthPerOrigin).to.have.been.called.with('Incognito');
done();
});

it('should call extractHostname with url when neither Initiator nor originUrl is not provided from from parameter', function (done) {
extractHostname = chai.spy();

Expand Down Expand Up @@ -253,4 +280,15 @@ describe('headersReceivedListener', function () {
expect(setByteLengthPerOrigin).to.have.been.called.with('www.spotify.com', 0);
done();
});

it('should not call setByteLengthPerOrigin when fromCache is defined', function(done){
setByteLengthPerOrigin = chai.spy();

requestDetails.fromCache = "test";
requestDetails.responseHeaders = [];
headersReceivedListener(requestDetails);

expect(setByteLengthPerOrigin).to.not.have.been.called();
done();
});
});