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

Add referer selenium tests and browser comptat unit tests #183

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 additions & 1 deletion selenium/integration_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const {assert} = require('chai');

const {newDriver, startApp, stopApp, firstPartyHost} = require('./utils'),
const {newDriver, startApp, stopApp, firstPartyHost, thirdPartyText} = require('./utils'),
{By} = require('selenium-webdriver'),
{cookieApp, fpcookie} = require("./cookies"),
{refererApp} = require('./referer'),
{etagApp} = require('./etags');

describe('etag tests', function() {
Expand Down Expand Up @@ -58,3 +60,21 @@ describe('cookie tests', function() {
assert.deepEqual(request.cookies, {});
});
});

describe('referer tests', function() {
beforeEach(async function() {
this.app = refererApp();
this.driver = await newDriver();
startApp(this.app);
});
afterEach(function() {
stopApp(this.app);
this.driver.quit();
});
it('sends header when required', async function() {
let {app, driver} = this;
await driver.get(firstPartyHost);
let el = await driver.findElement(By.xpath('/html/body/div'));
assert.deepEqual(await el.getText(), thirdPartyText);
});
});
24 changes: 24 additions & 0 deletions selenium/referer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const express = require('express'),
{requestRecorderMiddleware, baseTestApp} = require('./utils');

function requireRefererApp(app = express()) {
app.use((req, res, next) => {
if (!req.headers['referer']) {
res.statusCode = 401;
return res.send();
}
next();
});
requestRecorderMiddleware(app);
return app;
}

function refererApp() {
let fpApp = express(),
tpApp = requireRefererApp();
return baseTestApp(fpApp, tpApp);
}

Object.assign(module.exports, {refererApp});
23 changes: 18 additions & 5 deletions selenium/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const srcDir = '../src/.',
host = (hostname, port) => `${hostname}:${port}`,
firstPartyHostname = 'firstparty.local',
thirdPartyHostname = 'thirdparty.local',
thirdPartyText = 'Third party loaded.',
firstPartyHost = host(firstPartyHostname, PORT),
thirdPartyHost = host(thirdPartyHostname, PORT);

Expand Down Expand Up @@ -46,6 +47,7 @@ async function loadDriverWithExtension(extPath) {
.build();
}


async function newDriver() {
const srcPath = path.resolve(__dirname, srcDir);
return await loadDriverWithExtension(srcPath);
Expand Down Expand Up @@ -95,16 +97,27 @@ function requestRecorderMiddleware(app = express()) {

function firstPartyApp(app = express(), tpHost = thirdPartyHost) {
app.get('*', (req, res) => {
return res.send(
`<script type="text/javascript" src="http://${tpHost}/tracker.js"></script>`
);
return res.send(`
<head>
<script type="text/javascript" src="http://${tpHost}/tracker.js"></script>
</head>
<body>
</body>
`);
});
return app;
}

function thirdPartyApp(app = express()) {
app.get('*', (req, res) => {
return res.send('console.log("third party script")');
return res.send(`
window.onload = () => {
console.log("third party script");
const div = document.createElement('div');
div.innerText = '${thirdPartyText}';
document.body.appendChild(div);
};
`);
});
return app;
}
Expand All @@ -119,4 +132,4 @@ function baseTestApp(fpApp, tpApp, app = express(), fpHostname = firstPartyHostn
return app;
}

Object.assign(module.exports, {newDriver, startApp, stopApp, PORT, firstPartyHostname, thirdPartyHostname, firstPartyHost, thirdPartyHost, Channel, requestRecorderMiddleware, firstPartyApp, thirdPartyApp, baseTestApp});
Object.assign(module.exports, {newDriver, startApp, stopApp, PORT, firstPartyHostname, thirdPartyHostname, firstPartyHost, thirdPartyHost, thirdPartyText, Channel, requestRecorderMiddleware, firstPartyApp, thirdPartyApp, baseTestApp});
12 changes: 12 additions & 0 deletions src/js/test/browser_compat_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

const {assert} = require('chai'),
{getOnBeforeRequestOptions, getOnBeforeSendHeadersOptions, getOnHeadersReceivedOptions} = require('../browser_compat');

describe('browser_compat.js', function() {
it("gets options", function () {
assert.deepEqual(getOnBeforeRequestOptions(), ['blocking'])
assert.deepEqual(getOnBeforeSendHeadersOptions(), ['blocking', 'requestHeaders'])
assert.deepEqual(getOnHeadersReceivedOptions(), ['blocking', 'responseHeaders'])
});
});