-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for #26: refactor: new getRequestEntity function
Also, tests and coverage reporting!
- Loading branch information
1 parent
0cd241a
commit 3909fe5
Showing
6 changed files
with
83 additions
and
31 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 |
---|---|---|
|
@@ -12,3 +12,6 @@ before_install: | |
script: | ||
- npm run build | ||
- npm run lint | ||
|
||
after_success: | ||
- npm run coveralls |
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 |
---|---|---|
@@ -1,8 +1,38 @@ | ||
const {log} = require('./log') | ||
const {hostInEntity} = require('./lists') | ||
|
||
function requestAllower (tabID, totalExecTime, startDateTime) { | ||
totalExecTime[tabID] += Date.now() - startDateTime | ||
return {} | ||
} | ||
|
||
function getRequestEntity (entityList, originTopHost, requestTopHost, mainFrameOriginTopHost) { | ||
var requestEntityName | ||
let sameEntity = false | ||
for (let entityName in entityList) { | ||
var entity = entityList[entityName] | ||
var requestIsEntityResource = false | ||
var originIsEntityProperty = false | ||
var mainFrameOriginIsEntityProperty = false | ||
|
||
requestIsEntityResource = hostInEntity(entity.resources, requestTopHost) | ||
if (requestIsEntityResource) { | ||
requestEntityName = entityName | ||
} | ||
|
||
originIsEntityProperty = hostInEntity(entity.properties, originTopHost) | ||
|
||
mainFrameOriginIsEntityProperty = hostInEntity(entity.properties, mainFrameOriginTopHost) | ||
|
||
if ((originIsEntityProperty || mainFrameOriginIsEntityProperty) && requestIsEntityResource) { | ||
log(`originTopHost ${originTopHost} and resource requestTopHost ${requestTopHost} belong to the same entity: ${entityName}; allowing request`) | ||
sameEntity = true | ||
} | ||
} | ||
return {'entityName': requestEntityName, 'sameEntity': sameEntity} | ||
} | ||
|
||
module.exports = { | ||
requestAllower | ||
requestAllower, | ||
getRequestEntity | ||
} |
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 |
---|---|---|
@@ -1,19 +1,44 @@ | ||
var test = require('tape') | ||
var {allowRequest} = require('../js/requests') | ||
var {requestAllower, getRequestEntity} = require('../js/requests') | ||
|
||
var entityListFixtureData = require('./entitylist-fixture.json') | ||
|
||
test('allowRequest returns {}', (t) => { | ||
t.plan(1) | ||
let tabID = 1 | ||
let totalExecTime = {} | ||
totalExecTime[tabID] = 0 | ||
t.deepEqual(allowRequest(tabID, totalExecTime, Date.now()), {}) | ||
var allowRequest = requestAllower.bind(null, tabID, totalExecTime, Date.now()) | ||
t.deepEqual(allowRequest(), {}) | ||
}) | ||
|
||
test('allowRequest adds some ms to totalExecTime[tabID]', (t) => { | ||
t.plan(1) | ||
let tabID = 1 | ||
let totalExecTime = {} | ||
totalExecTime[tabID] = 0 | ||
allowRequest(tabID, totalExecTime, 0) | ||
var allowRequest = requestAllower.bind(null, tabID, totalExecTime, 0) | ||
allowRequest() | ||
t.ok(totalExecTime[tabID] > 0, 'added ms to totalExecTime[tabID]') | ||
}) | ||
|
||
test('getRequestEntity request to google.com from facebook.com returns Google and false', (t) => { | ||
t.plan(2) | ||
let requestEntity = getRequestEntity(entityListFixtureData, 'facebook.com', 'google.com', 'facebook.com') | ||
t.equal(requestEntity.entityName, 'Google') | ||
t.notOk(requestEntity.sameEntity) | ||
}) | ||
|
||
test('getRequestEntity request to facebook.com from instagram.com returns Facebook and true', (t) => { | ||
t.plan(2) | ||
let requestEntity = getRequestEntity(entityListFixtureData, 'instagram.com', 'facebook.com', 'instagram.com') | ||
t.equal(requestEntity.entityName, 'Facebook') | ||
t.ok(requestEntity.sameEntity) | ||
}) | ||
|
||
test('getRequestEntity request to facebook.com from github.io iframe on facebook.com returns Facebook and true', (t) => { | ||
t.plan(2) | ||
let requestEntity = getRequestEntity(entityListFixtureData, 'githhub.io', 'facebook.com', 'facebook.com') | ||
t.equal(requestEntity.entityName, 'Facebook') | ||
t.ok(requestEntity.sameEntity) | ||
}) |