Skip to content

Commit

Permalink
replace all xhr stubs with global xhr stub to prevent all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
snapwich committed Jan 2, 2020
1 parent 773a004 commit 89f9b81
Show file tree
Hide file tree
Showing 29 changed files with 204 additions and 378 deletions.
9 changes: 9 additions & 0 deletions test/mocks/xhr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export let server = sinon.createFakeServer();
export let xhr = global.XMLHttpRequest;

beforeEach(function() {
server.restore();
server = sinon.createFakeServer();
xhr = global.XMLHttpRequest;
});
33 changes: 14 additions & 19 deletions test/spec/AnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import events from 'src/events';
import CONSTANTS from 'src/constants.json';
import { server } from 'test/mocks/xhr';

const REQUEST_BIDS = CONSTANTS.EVENTS.REQUEST_BIDS;
const BID_REQUESTED = CONSTANTS.EVENTS.BID_REQUESTED;
Expand All @@ -20,19 +21,13 @@ describe(`
FEATURE: Analytics Adapters API
SCENARIO: A publisher enables analytics
AND an \`example\` instance of \`AnalyticsAdapter\`\n`, () => {
let xhr;
let requests;
let adapter;

beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = (request) => requests.push(request);
adapter = new AnalyticsAdapter(config);
});

afterEach(function () {
xhr.restore();
adapter.disableAnalytics();
});

Expand All @@ -42,7 +37,7 @@ FEATURE: Analytics Adapters API

adapter.track({ eventType, args });

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {some: 'data'}, eventType: 'bidRequested'});
});

Expand All @@ -53,7 +48,7 @@ FEATURE: Analytics Adapters API
events.emit(eventType, args);
adapter.enableAnalytics();

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {wat: 'wot'}, eventType: 'bidResponse'});
});

Expand All @@ -73,7 +68,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {more: 'info'}, eventType: 'bidWon'});
});

Expand All @@ -84,7 +79,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'adRenderFailed'}, eventType: 'adRenderFailed'});
});

Expand All @@ -95,7 +90,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'addAdUnits'}, eventType: 'addAdUnits'});
});

Expand All @@ -106,7 +101,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'request'}, eventType: 'requestBids'});
});

Expand All @@ -117,7 +112,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'request'}, eventType: 'bidRequested'});
});

Expand All @@ -128,7 +123,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'response'}, eventType: 'bidResponse'});
});

Expand All @@ -139,7 +134,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

let result = JSON.parse(requests[0].requestBody);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {call: 'timeout'}, eventType: 'bidTimeout'});
});

Expand All @@ -151,7 +146,7 @@ FEATURE: Analytics Adapters API
adapter.enableAnalytics();
events.emit(eventType, args);

expect(requests.length).to.equal(1);
expect(server.requests.length).to.equal(1);
});

describe(`AND sampling is enabled\n`, function () {
Expand All @@ -174,8 +169,8 @@ FEATURE: Analytics Adapters API
});
events.emit(eventType, args);

expect(requests.length).to.equal(1);
let result = JSON.parse(requests[0].requestBody);
expect(server.requests.length).to.equal(1);
let result = JSON.parse(server.requests[0].requestBody);
expect(result).to.deep.equal({args: {more: 'info'}, eventType: 'bidWon'});
});

Expand All @@ -187,7 +182,7 @@ FEATURE: Analytics Adapters API
});
events.emit(eventType, args);

expect(requests.length).to.equal(0);
expect(server.requests.length).to.equal(0);
});
});
});
Expand Down
13 changes: 2 additions & 11 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { config } from 'src/config';
import * as store from 'src/videoCache';
import * as ajaxLib from 'src/ajax';
import find from 'core-js/library/fn/array/find';
import { server } from 'test/mocks/xhr';

var assert = require('assert');

Expand Down Expand Up @@ -766,7 +767,6 @@ describe('auctionmanager.js', function () {

describe('when auction timeout is 20', function () {
let eventsEmitSpy;
let server;

before(function () {
bids = [mockBid(), mockBid({ bidderCode: BIDDER_CODE1 })];
Expand All @@ -776,8 +776,6 @@ describe('auctionmanager.js', function () {
});

beforeEach(function () {
server = sinon.createFakeServer();

adUnits = [{
code: ADUNIT_CODE,
bids: [
Expand All @@ -789,7 +787,6 @@ describe('auctionmanager.js', function () {
eventsEmitSpy = sinon.spy(events, 'emit');
});
afterEach(function () {
server.restore();
events.emit.restore();
});

Expand Down Expand Up @@ -1058,8 +1055,6 @@ describe('auctionmanager.js', function () {
describe('auctionCallbacks', function() {
let bids = TEST_BIDS;
let bidRequests;
let xhr;
let requests;
let doneSpy;
let auction = {
getBidRequests: () => bidRequests,
Expand All @@ -1070,9 +1065,6 @@ describe('auctionmanager.js', function () {

beforeEach(() => {
doneSpy = sinon.spy();
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = (request) => requests.push(request);
config.setConfig({
cache: {
url: 'https://prebid.adnxs.com/pbc/v1/cache'
Expand All @@ -1082,7 +1074,6 @@ describe('auctionmanager.js', function () {

afterEach(() => {
doneSpy.resetHistory();
xhr.restore();
config.resetConfig();
});

Expand Down Expand Up @@ -1132,7 +1123,7 @@ describe('auctionmanager.js', function () {
assert.equal(doneSpy.callCount, 0);
const uuid = 'c488b101-af3e-4a99-b538-00423e5a3371';
const responseBody = `{"responses":[{"uuid":"${uuid}"}]}`;
requests[0].respond(200, { 'Content-Type': 'application/json' }, responseBody);
server.requests[0].respond(200, { 'Content-Type': 'application/json' }, responseBody);
assert.equal(doneSpy.callCount, 1);
})
});
Expand Down
16 changes: 5 additions & 11 deletions test/spec/modules/adxcgAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import adxcgAnalyticsAdapter from 'modules/adxcgAnalyticsAdapter';
import { expect } from 'chai';
import adapterManager from 'src/adapterManager.js';
import { server } from 'test/mocks/xhr';

let events = require('src/events');
let constants = require('src/constants.json');

describe('adxcg analytics adapter', function () {
let xhr;
let requests;

beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = request => requests.push(request);
sinon.stub(events, 'getEvents').returns([]);
});

afterEach(function () {
xhr.restore();
events.getEvents.restore();
});

Expand Down Expand Up @@ -193,9 +187,9 @@ describe('adxcg analytics adapter', function () {
// Step 5: Send auction end event
events.emit(constants.EVENTS.AUCTION_END, {});

expect(requests.length).to.equal(1);
expect(server.requests.length).to.equal(1);

let realAfterBid = JSON.parse(requests[0].requestBody);
let realAfterBid = JSON.parse(server.requests[0].requestBody);

expect(realAfterBid).to.deep.equal(expectedAfterBid);

Expand All @@ -204,8 +198,8 @@ describe('adxcg analytics adapter', function () {
// Step 6: Send auction bid won event
events.emit(constants.EVENTS.BID_WON, wonRequest);

expect(requests.length).to.equal(2);
let winEventData = JSON.parse(requests[1].requestBody);
expect(server.requests.length).to.equal(2);
let winEventData = JSON.parse(server.requests[1].requestBody);

expect(winEventData).to.deep.equal(wonExpect);
});
Expand Down
4 changes: 0 additions & 4 deletions test/spec/modules/appierAnalyticsAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const auctionId = 'b0b39610-b941-4659-a87c-de9f62d3e13e';

describe('Appier Prebid AnalyticsAdapter Testing', function () {
describe('event tracking and message cache manager', function () {
let xhr;

beforeEach(function () {
const configOptions = {
affiliateId: affiliateId,
Expand All @@ -32,12 +30,10 @@ describe('Appier Prebid AnalyticsAdapter Testing', function () {
provider: 'appierAnalytics',
options: configOptions
});
xhr = sinon.useFakeXMLHttpRequest();
});

afterEach(function () {
appierAnalyticsAdapter.disableAnalytics();
xhr.restore();
});

describe('#getCpmInUsd()', function() {
Expand Down
13 changes: 5 additions & 8 deletions test/spec/modules/criteortusIdSystem_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { criteortusIdSubmodule } from 'modules/criteortusIdSystem';
import * as utils from 'src/utils';
import { server } from 'test/mocks/xhr';

describe('Criteo RTUS', function() {
let xhr;
Expand All @@ -8,15 +9,11 @@ describe('Criteo RTUS', function() {
let logErrorStub;

beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = request => requests.push(request);
getCookieStub = sinon.stub(utils, 'getCookie');
logErrorStub = sinon.stub(utils, 'logError');
});

afterEach(function () {
xhr.restore();
getCookieStub.restore();
logErrorStub.restore();
});
Expand All @@ -39,7 +36,7 @@ describe('Criteo RTUS', function() {
const idResp = criteortusIdSubmodule.getId(configParams);
const submoduleCallback = idResp.callback;
submoduleCallback(callBackSpy);
requests[0].respond(
server.requests[0].respond(
200,
{ 'Content-Type': 'text/plain' },
JSON.stringify(response)
Expand All @@ -57,7 +54,7 @@ describe('Criteo RTUS', function() {
}
}
let uid = criteortusIdSubmodule.getId(configParams);
expect(requests.length).to.equal(0);
expect(server.requests.length).to.equal(0);
})

it('should call criteo endpoint for multiple bidders', function() {
Expand All @@ -74,13 +71,13 @@ describe('Criteo RTUS', function() {
const idResp = criteortusIdSubmodule.getId(configParams);
const submoduleCallback = idResp.callback;
submoduleCallback(callBackSpy);
requests[0].respond(
server.requests[0].respond(
200,
{ 'Content-Type': 'text/plain' },
JSON.stringify(response)
);
expect(callBackSpy.calledOnce).to.be.false;
requests[1].respond(
server.requests[1].respond(
200,
{ 'Content-Type': 'text/plain' },
JSON.stringify(response)
Expand Down
10 changes: 2 additions & 8 deletions test/spec/modules/dfpAdServerVideo_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { config } from 'src/config';
import { targeting } from 'src/targeting';
import { auctionManager } from 'src/auctionManager';
import * as adpod from 'modules/adpod';
import { server } from 'test/mocks/xhr';

const bid = {
videoCacheKey: 'abc',
Expand Down Expand Up @@ -302,8 +303,6 @@ describe('The DFP video support module', function () {
describe('adpod unit tests', function () {
let amStub;
let amGetAdUnitsStub;
let xhr;
let requests;

before(function () {
let adUnits = [{
Expand Down Expand Up @@ -333,10 +332,6 @@ describe('The DFP video support module', function () {
});

beforeEach(function () {
xhr = sinon.useFakeXMLHttpRequest();
requests = [];
xhr.onCreate = request => requests.push(request);

config.setConfig({
adpod: {
brandCategoryExclusion: true,
Expand All @@ -347,7 +342,6 @@ describe('The DFP video support module', function () {

afterEach(function() {
config.resetConfig();
xhr.restore();
});

after(function () {
Expand Down Expand Up @@ -466,7 +460,7 @@ describe('The DFP video support module', function () {
}
}));

requests[0].respond(503, {
server.requests[0].respond(503, {
'Content-Type': 'plain/text',
}, 'The server could not save anything at the moment.');

Expand Down
Loading

0 comments on commit 89f9b81

Please sign in to comment.