diff --git a/test/mocks/xhr.js b/test/mocks/xhr.js new file mode 100644 index 00000000000..9fb8fe87fa0 --- /dev/null +++ b/test/mocks/xhr.js @@ -0,0 +1,9 @@ + +export let server = sinon.createFakeServer(); +export let xhr = global.XMLHttpRequest; + +beforeEach(function() { + server.restore(); + server = sinon.createFakeServer(); + xhr = global.XMLHttpRequest; +}); diff --git a/test/spec/AnalyticsAdapter_spec.js b/test/spec/AnalyticsAdapter_spec.js index 2e992d3298c..2ede81538c1 100644 --- a/test/spec/AnalyticsAdapter_spec.js +++ b/test/spec/AnalyticsAdapter_spec.js @@ -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; @@ -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(); }); @@ -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'}); }); @@ -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'}); }); @@ -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'}); }); @@ -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'}); }); @@ -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'}); }); @@ -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'}); }); @@ -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'}); }); @@ -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'}); }); @@ -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'}); }); @@ -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 () { @@ -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'}); }); @@ -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); }); }); }); diff --git a/test/spec/auctionmanager_spec.js b/test/spec/auctionmanager_spec.js index 970bc61cc61..2693daa62b3 100644 --- a/test/spec/auctionmanager_spec.js +++ b/test/spec/auctionmanager_spec.js @@ -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'); @@ -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 })]; @@ -776,8 +776,6 @@ describe('auctionmanager.js', function () { }); beforeEach(function () { - server = sinon.createFakeServer(); - adUnits = [{ code: ADUNIT_CODE, bids: [ @@ -789,7 +787,6 @@ describe('auctionmanager.js', function () { eventsEmitSpy = sinon.spy(events, 'emit'); }); afterEach(function () { - server.restore(); events.emit.restore(); }); @@ -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, @@ -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' @@ -1082,7 +1074,6 @@ describe('auctionmanager.js', function () { afterEach(() => { doneSpy.resetHistory(); - xhr.restore(); config.resetConfig(); }); @@ -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); }) }); diff --git a/test/spec/modules/adxcgAnalyticsAdapter_spec.js b/test/spec/modules/adxcgAnalyticsAdapter_spec.js index 605da3bd6bc..2c495370a57 100644 --- a/test/spec/modules/adxcgAnalyticsAdapter_spec.js +++ b/test/spec/modules/adxcgAnalyticsAdapter_spec.js @@ -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(); }); @@ -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); @@ -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); }); diff --git a/test/spec/modules/appierAnalyticsAdapter_spec.js b/test/spec/modules/appierAnalyticsAdapter_spec.js index 07b9796a4cb..cf47f31afe4 100644 --- a/test/spec/modules/appierAnalyticsAdapter_spec.js +++ b/test/spec/modules/appierAnalyticsAdapter_spec.js @@ -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, @@ -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() { diff --git a/test/spec/modules/criteortusIdSystem_spec.js b/test/spec/modules/criteortusIdSystem_spec.js index 217a2f86ba7..6ceaa7aa953 100644 --- a/test/spec/modules/criteortusIdSystem_spec.js +++ b/test/spec/modules/criteortusIdSystem_spec.js @@ -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; @@ -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(); }); @@ -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) @@ -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() { @@ -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) diff --git a/test/spec/modules/dfpAdServerVideo_spec.js b/test/spec/modules/dfpAdServerVideo_spec.js index 6271c9b38f4..ff527c379e2 100644 --- a/test/spec/modules/dfpAdServerVideo_spec.js +++ b/test/spec/modules/dfpAdServerVideo_spec.js @@ -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', @@ -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 = [{ @@ -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, @@ -347,7 +342,6 @@ describe('The DFP video support module', function () { afterEach(function() { config.resetConfig(); - xhr.restore(); }); after(function () { @@ -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.'); diff --git a/test/spec/modules/eplanningAnalyticsAdapter_spec.js b/test/spec/modules/eplanningAnalyticsAdapter_spec.js index 8609024c7d9..3e72cfc1dcc 100644 --- a/test/spec/modules/eplanningAnalyticsAdapter_spec.js +++ b/test/spec/modules/eplanningAnalyticsAdapter_spec.js @@ -2,23 +2,17 @@ import eplAnalyticsAdapter from 'modules/eplanningAnalyticsAdapter'; import includes from 'core-js/library/fn/array/includes'; import { expect } from 'chai'; import {parse as parseURL} from 'src/url'; +import { server } from 'test/mocks/xhr'; let adapterManager = require('src/adapterManager').default; let events = require('src/events'); let constants = require('src/constants.json'); describe('eplanning 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(); eplAnalyticsAdapter.disableAnalytics(); }); @@ -115,7 +109,7 @@ describe('eplanning analytics adapter', function () { events.emit(constants.EVENTS.AUCTION_END, {auctionId: pauctionId}); // Step 7: Find the request data sent (filtering other hosts) - requests = requests.filter(req => { + let requests = server.requests.filter(req => { return req.url.indexOf(initOptions.host) > -1; }); expect(requests.length).to.equal(1); diff --git a/test/spec/modules/feedadBidAdapter_spec.js b/test/spec/modules/feedadBidAdapter_spec.js index 3432a40eca4..d7d0f32e252 100644 --- a/test/spec/modules/feedadBidAdapter_spec.js +++ b/test/spec/modules/feedadBidAdapter_spec.js @@ -1,7 +1,7 @@ import {expect} from 'chai'; import {spec} from 'modules/feedadBidAdapter'; import {BANNER, NATIVE, VIDEO} from '../../../src/mediaTypes'; -import * as sinon from 'sinon'; +import {server} from 'test/mocks/xhr'; const CODE = 'feedad'; @@ -382,28 +382,15 @@ describe('FeedAdAdapter', function () { cases.forEach(([name, data, eventKlass]) => { let subject = spec[name]; describe(name + ' handler', function () { - let xhr; - let requests; - - beforeEach(function () { - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = xhr => requests.push(xhr); - }); - - afterEach(function () { - xhr.restore(); - }); - it('should do nothing on empty data', function () { subject(undefined); subject(null); - expect(requests.length).to.equal(0); + expect(server.requests.length).to.equal(0); }); it('should do nothing when bid metadata is not set', function () { subject(data); - expect(requests.length).to.equal(0); + expect(server.requests.length).to.equal(0); }); it('should send tracking params when correct metadata was set', function () { @@ -420,8 +407,8 @@ describe('FeedAdAdapter', function () { sdk_version: '1.0.0' }; subject(data); - expect(requests.length).to.equal(1); - let call = requests[0]; + expect(server.requests.length).to.equal(1); + let call = server.requests[0]; expect(call.url).to.equal('https://api.feedad.com/1/prebid/web/events'); expect(JSON.parse(call.requestBody)).to.deep.equal(expectedData); expect(call.method).to.equal('POST'); diff --git a/test/spec/modules/fintezaAnalyticsAdapter_spec.js b/test/spec/modules/fintezaAnalyticsAdapter_spec.js index 2cc2f380016..da575035d09 100644 --- a/test/spec/modules/fintezaAnalyticsAdapter_spec.js +++ b/test/spec/modules/fintezaAnalyticsAdapter_spec.js @@ -2,6 +2,7 @@ import fntzAnalyticsAdapter from 'modules/fintezaAnalyticsAdapter'; import includes from 'core-js/library/fn/array/includes'; import { expect } from 'chai'; import { parse as parseURL } from 'src/url'; +import { server } from 'test/mocks/xhr'; let adapterManager = require('src/adapterManager').default; let events = require('src/events'); @@ -18,14 +19,8 @@ describe('finteza analytics adapter', function () { const clientId = 'fntz-client-32145'; const uniqCookie = '5045380421580287382'; - let xhr; - let requests; - beforeEach(function () { setCookie('_fz_uniq', uniqCookie); - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = request => { requests.push(request) }; sinon.stub(events, 'getEvents').returns([]); sinon.spy(fntzAnalyticsAdapter, 'track'); @@ -49,7 +44,6 @@ describe('finteza analytics adapter', function () { afterEach(function () { setCookie('_fz_uniq', '', new Date(0)); - xhr.restore(); events.getEvents.restore(); fntzAnalyticsAdapter.track.restore(); fntzAnalyticsAdapter.disableAnalytics(); @@ -82,12 +76,12 @@ describe('finteza analytics adapter', function () { // Emit the events with the "real" arguments events.emit(constants.EVENTS.BID_REQUESTED, bidRequest); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); - expect(requests[0].method).to.equal('GET'); - expect(requests[0].withCredentials).to.equal(true); + expect(server.requests[0].method).to.equal('GET'); + expect(server.requests[0].withCredentials).to.equal(true); - const url = parseURL(requests[0].url); + const url = parseURL(server.requests[0].url); expect(url.protocol).to.equal('https'); expect(url.hostname).to.equal('content.mql5.com'); @@ -125,12 +119,12 @@ describe('finteza analytics adapter', function () { // Emit the events with the "real" arguments events.emit(constants.EVENTS.BID_RESPONSE, bidResponse); - expect(requests.length).to.equal(2); + expect(server.requests.length).to.equal(2); - expect(requests[0].method).to.equal('GET'); - expect(requests[0].withCredentials).to.equal(true); + expect(server.requests[0].method).to.equal('GET'); + expect(server.requests[0].withCredentials).to.equal(true); - let url = parseURL(requests[0].url); + let url = parseURL(server.requests[0].url); expect(url.protocol).to.equal('https'); expect(url.hostname).to.equal('content.mql5.com'); @@ -141,10 +135,10 @@ describe('finteza analytics adapter', function () { expect(url.search.value).to.equal(String(cpm)); expect(url.search.unit).to.equal('usd'); - expect(requests[1].method).to.equal('GET'); - expect(requests[1].withCredentials).to.equal(true); + expect(server.requests[1].method).to.equal('GET'); + expect(server.requests[1].withCredentials).to.equal(true); - url = parseURL(requests[1].url); + url = parseURL(server.requests[1].url); expect(url.protocol).to.equal('https'); expect(url.hostname).to.equal('content.mql5.com'); @@ -179,12 +173,12 @@ describe('finteza analytics adapter', function () { // Emit the events with the "real" arguments events.emit(constants.EVENTS.BID_WON, bidWon); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); - expect(requests[0].method).to.equal('GET'); - expect(requests[0].withCredentials).to.equal(true); + expect(server.requests[0].method).to.equal('GET'); + expect(server.requests[0].withCredentials).to.equal(true); - const url = parseURL(requests[0].url); + const url = parseURL(server.requests[0].url); expect(url.protocol).to.equal('https'); expect(url.hostname).to.equal('content.mql5.com'); @@ -218,12 +212,12 @@ describe('finteza analytics adapter', function () { // Emit the events with the "real" arguments events.emit(constants.EVENTS.BID_TIMEOUT, bidTimeout); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); - expect(requests[0].method).to.equal('GET'); - expect(requests[0].withCredentials).to.equal(true); + expect(server.requests[0].method).to.equal('GET'); + expect(server.requests[0].withCredentials).to.equal(true); - const url = parseURL(requests[0].url); + const url = parseURL(server.requests[0].url); expect(url.protocol).to.equal('https'); expect(url.hostname).to.equal('content.mql5.com'); diff --git a/test/spec/modules/freeWheelAdserverVideo_spec.js b/test/spec/modules/freeWheelAdserverVideo_spec.js index c2afaf8d69f..4e37a345716 100644 --- a/test/spec/modules/freeWheelAdserverVideo_spec.js +++ b/test/spec/modules/freeWheelAdserverVideo_spec.js @@ -2,12 +2,11 @@ import { expect } from 'chai'; import { adpodUtils } from 'modules/freeWheelAdserverVideo'; import { auctionManager } from 'src/auctionManager'; import { config } from 'src/config'; +import { server } from 'test/mocks/xhr'; describe('freeWheel adserver module', function() { let amStub; let amGetAdUnitsStub; - let xhr; - let requests; before(function () { let adUnits = [{ @@ -56,10 +55,6 @@ describe('freeWheel adserver module', function() { }); beforeEach(function () { - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = request => requests.push(request); - config.setConfig({ adpod: { brandCategoryExclusion: false, @@ -70,7 +65,6 @@ describe('freeWheel adserver module', function() { afterEach(function() { config.resetConfig(); - xhr.restore(); }); after(function () { @@ -188,7 +182,7 @@ describe('freeWheel adserver module', function() { } }); - requests[0].respond( + server.requests[0].respond( 200, { 'Content-Type': 'text/plain' }, JSON.stringify({'responses': getBidsReceived().slice(0, 4)}) diff --git a/test/spec/modules/livewrappedAnalyticsAdapter_spec.js b/test/spec/modules/livewrappedAnalyticsAdapter_spec.js index 649b85b9c07..98ed21132e3 100644 --- a/test/spec/modules/livewrappedAnalyticsAdapter_spec.js +++ b/test/spec/modules/livewrappedAnalyticsAdapter_spec.js @@ -1,6 +1,7 @@ import livewrappedAnalyticsAdapter, { BID_WON_TIMEOUT } from 'modules/livewrappedAnalyticsAdapter'; import CONSTANTS from 'src/constants.json'; import { config } from 'src/config'; +import { server } from 'test/mocks/xhr'; let events = require('src/events'); let utils = require('src/utils'); @@ -208,17 +209,11 @@ function performStandardAuction() { describe('Livewrapped analytics adapter', function () { let sandbox; - let xhr; - let requests; let clock; beforeEach(function () { sandbox = sinon.sandbox.create(); - xhr = sandbox.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = request => requests.push(request); - sandbox.stub(events, 'getEvents').returns([]); sandbox.stub(utils, 'timestamp').returns(1519149562416); @@ -255,8 +250,8 @@ describe('Livewrapped analytics adapter', function () { clock.tick(BID_WON_TIMEOUT + 1000); - expect(requests.length).to.equal(1); - let request = requests[0]; + expect(server.requests.length).to.equal(1); + let request = server.requests[0]; expect(request.url).to.equal('https://lwadm.com/analytics/10'); @@ -279,14 +274,14 @@ describe('Livewrapped analytics adapter', function () { events.emit(BID_WON, MOCK.BID_WON[1]); - expect(requests.length).to.equal(2); + expect(server.requests.length).to.equal(2); - let message = JSON.parse(requests[0].requestBody); + let message = JSON.parse(server.requests[0].requestBody); expect(message.wins.length).to.equal(1); expect(message.requests).to.deep.equal(ANALYTICS_MESSAGE.requests); expect(message.wins[0]).to.deep.equal(ANALYTICS_MESSAGE.wins[0]); - message = JSON.parse(requests[1].requestBody); + message = JSON.parse(server.requests[1].requestBody); expect(message.wins.length).to.equal(1); expect(message.wins[0]).to.deep.equal(ANALYTICS_MESSAGE.wins[1]); }); @@ -299,9 +294,9 @@ describe('Livewrapped analytics adapter', function () { clock.tick(BID_WON_TIMEOUT + 1000); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); - let message = JSON.parse(requests[0].requestBody); + let message = JSON.parse(server.requests[0].requestBody); expect(message.timeouts.length).to.equal(1); expect(message.timeouts[0].bidder).to.equal('livewrapped'); expect(message.timeouts[0].adUnit).to.equal('panorama_d_1'); @@ -313,8 +308,8 @@ describe('Livewrapped analytics adapter', function () { clock.tick(BID_WON_TIMEOUT + 1000); - expect(requests.length).to.equal(1); - let request = requests[0]; + expect(server.requests.length).to.equal(1); + let request = server.requests[0]; let message = JSON.parse(request.requestBody); diff --git a/test/spec/modules/openxAnalyticsAdapter_spec.js b/test/spec/modules/openxAnalyticsAdapter_spec.js index 168f06aa915..1020b89d3b7 100644 --- a/test/spec/modules/openxAnalyticsAdapter_spec.js +++ b/test/spec/modules/openxAnalyticsAdapter_spec.js @@ -4,6 +4,7 @@ import { config } from 'src/config'; import events from 'src/events'; import CONSTANTS from 'src/constants.json'; import * as utils from 'src/utils'; +import { server } from 'test/mocks/xhr'; const { EVENTS: { AUCTION_INIT, BID_REQUESTED, BID_RESPONSE, BID_TIMEOUT, BID_WON } @@ -152,12 +153,7 @@ describe('openx analytics adapter', function() { }, {}); } - let xhr; - let requests; - before(function() { - xhr = sinon.useFakeXMLHttpRequest(); - xhr.onCreate = request => requests.push(request); sinon.stub(events, 'getEvents').returns([]); openxAdapter.enableAnalytics({ options: { @@ -167,13 +163,11 @@ describe('openx analytics adapter', function() { }); after(function() { - xhr.restore(); events.getEvents.restore(); openxAdapter.disableAnalytics(); }); beforeEach(function() { - requests = []; openxAdapter.reset(); }); @@ -185,7 +179,7 @@ describe('openx analytics adapter', function() { [BID_REQUESTED, bidRequestedOpenX] ]); - expect(requests.length).to.equal(0); + expect(server.requests.length).to.equal(0); }); it('should send 1 request to the right endpoint', function() { @@ -195,9 +189,9 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseOpenX] ]); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); - const endpoint = requests[0].url.split('?')[0]; + const endpoint = server.requests[0].url.split('?')[0]; // note IE11 returns the default secure port, so we look for this alternate value as well in these tests expect(endpoint).to.be.oneOf(['https://ads.openx.net/w/1.0/pban', 'https://ads.openx.net:443/w/1.0/pban']); }); @@ -210,7 +204,7 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseOpenX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.include({ 'hb.ct': String(bidRequestedOpenX.auctionStart), 'hb.rid': auctionInit.auctionId, @@ -236,7 +230,7 @@ describe('openx analytics adapter', function() { config.getConfig.restore(); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.include({ 'hb.cur': 'bitcoin' }); @@ -249,7 +243,7 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseOpenX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.not.have.key('hb.cur'); }); }); @@ -276,7 +270,7 @@ describe('openx analytics adapter', function() { window.top.performance = originalPerf; Date.now.restore(); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.include({ 'hb.dcl': String(timing.domContentLoadedEventEnd - timing.fetchStart), 'hb.dl': String(timing.loadEventEnd - timing.fetchStart), @@ -297,7 +291,7 @@ describe('openx analytics adapter', function() { window.top.performance = originalPerf; - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.not.have.keys( 'hb.dcl', 'hb.dl', @@ -317,7 +311,7 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseCloseX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.include({ ts: bidResponseOpenX.ts, auid: bidRequestedOpenX.bids[0].params.unit @@ -332,7 +326,7 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseCloseX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.include({ auid: bidRequestedOpenX.bids[0].params.unit }); @@ -346,7 +340,7 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseCloseX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.not.have.keys('auid', 'ts'); }); }); @@ -361,7 +355,7 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseCloseX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); const auctionStart = bidRequestedOpenX.auctionStart; expect(queryData).to.include({ 'hb.exn': [ @@ -394,7 +388,7 @@ describe('openx analytics adapter', function() { [BID_TIMEOUT, bidTimeoutOpenX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); const auctionStart = bidRequestedOpenX.auctionStart; expect(queryData).to.include({ 'hb.exn': [ @@ -425,7 +419,7 @@ describe('openx analytics adapter', function() { [BID_WON, bidWonOpenX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.include({ 'hb.we': '0', 'hb.g1': 'false' @@ -439,7 +433,7 @@ describe('openx analytics adapter', function() { [BID_RESPONSE, bidResponseOpenX] ]); - const queryData = getQueryData(requests[0].url); + const queryData = getQueryData(server.requests[0].url); expect(queryData).to.include({ 'hb.we': '-1', 'hb.g1': 'true' diff --git a/test/spec/modules/prebidServerBidAdapter_spec.js b/test/spec/modules/prebidServerBidAdapter_spec.js index c6c19f0f5c1..cf616e9ec8c 100644 --- a/test/spec/modules/prebidServerBidAdapter_spec.js +++ b/test/spec/modules/prebidServerBidAdapter_spec.js @@ -6,6 +6,7 @@ import { ajax } from 'src/ajax'; import { config } from 'src/config'; import events from 'src/events'; import CONSTANTS from 'src/constants'; +import { server } from 'test/mocks/xhr'; let CONFIG = { accountId: '1', @@ -479,19 +480,13 @@ describe('S2S Adapter', function () { }); describe('request function', function () { - let xhr; - let requests; - beforeEach(function () { - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = request => requests.push(request); config.resetConfig(); resetSyncedStatus(); }); afterEach(function () { - xhr.restore(); + config.resetConfig(); }); it('should not add outstrean without renderer', function () { @@ -501,7 +496,7 @@ describe('S2S Adapter', function () { config.setConfig({ s2sConfig: ortb2Config }); adapter.callBids(OUTSTREAM_VIDEO_REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.imp[0].banner).to.exist; expect(requestBid.imp[0].video).to.not.exist; }); @@ -530,7 +525,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(REQUEST, gdprBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.regs.ext.gdpr).is.equal(1); expect(requestBid.user.ext.consent).is.equal('abc123'); @@ -539,7 +534,7 @@ describe('S2S Adapter', function () { config.setConfig({ s2sConfig: CONFIG }); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - requestBid = JSON.parse(requests[1].requestBody); + requestBid = JSON.parse(server.requests[1].requestBody); expect(requestBid.regs).to.not.exist; expect(requestBid.user).to.not.exist; @@ -560,7 +555,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(REQUEST, gdprBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.gdpr).is.equal(1); expect(requestBid.gdpr_consent).is.equal('abc123def'); @@ -582,7 +577,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(REQUEST, gdprBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.gdpr).is.equal(0); expect(requestBid.gdpr_consent).is.undefined; @@ -602,7 +597,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(REQUEST, gdprBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.gdpr).is.undefined; expect(requestBid.gdpr_consent).is.undefined; @@ -624,7 +619,7 @@ describe('S2S Adapter', function () { uspBidRequest[0].uspConsent = '1NYN'; adapter.callBids(REQUEST, uspBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.regs.ext.us_privacy).is.equal('1NYN'); @@ -632,7 +627,7 @@ describe('S2S Adapter', function () { config.setConfig({ s2sConfig: CONFIG }); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - requestBid = JSON.parse(requests[1].requestBody); + requestBid = JSON.parse(server.requests[1].requestBody); expect(requestBid.regs).to.not.exist; }); @@ -646,7 +641,7 @@ describe('S2S Adapter', function () { uspBidRequest[0].uspConsent = '1YNN'; adapter.callBids(REQUEST, uspBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.us_privacy).is.equal('1YNN'); expect(requestBid.bidders).to.contain('appnexus').and.to.have.lengthOf(1); @@ -673,7 +668,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(REQUEST, consentBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.regs.ext.us_privacy).is.equal('1NYN'); expect(requestBid.regs.ext.gdpr).is.equal(1); @@ -683,7 +678,7 @@ describe('S2S Adapter', function () { config.setConfig({ s2sConfig: CONFIG }); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - requestBid = JSON.parse(requests[1].requestBody); + requestBid = JSON.parse(server.requests[1].requestBody); expect(requestBid.regs).to.not.exist; expect(requestBid.user).to.not.exist; @@ -702,7 +697,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(REQUEST, consentBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.us_privacy).is.equal('1YNN'); expect(requestBid.gdpr).is.equal(1); @@ -731,7 +726,7 @@ describe('S2S Adapter', function () { digiTrustBidRequest[0].bids[0].userId = { digitrustid: { data: digiTrustObj } }; adapter.callBids(REQUEST, digiTrustBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.user.ext.digitrust).to.deep.equal({ id: digiTrustObj.id, @@ -741,7 +736,7 @@ describe('S2S Adapter', function () { digiTrustObj.privacy.optout = true; adapter.callBids(REQUEST, digiTrustBidRequest, addBidResponse, done, ajax); - requestBid = JSON.parse(requests[1].requestBody); + requestBid = JSON.parse(server.requests[1].requestBody); expect(requestBid.user && request.user.ext && requestBid.user.ext.digitrust).to.not.exist; }); @@ -755,7 +750,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.device).to.deep.equal({ ifa: '6D92078A-8246-4BA4-AE5B-76104861E7DC', w: window.innerWidth, @@ -780,7 +775,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.device).to.deep.equal({ ifa: '6D92078A-8246-4BA4-AE5B-76104861E7DC', w: window.innerWidth, @@ -804,7 +799,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.device).to.deep.equal({ w: window.innerWidth, h: window.innerHeight @@ -826,7 +821,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.imp[0].native).to.deep.equal({ request: JSON.stringify({ @@ -885,7 +880,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.site).to.exist.and.to.be.a('object'); expect(requestBid.site.publisher).to.exist.and.to.be.a('object'); expect(requestBid.site.page).to.exist.and.to.be.a('string'); @@ -907,7 +902,7 @@ describe('S2S Adapter', function () { adapter.callBids(request, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.ext).to.deep.equal({ prebid: { @@ -941,7 +936,7 @@ describe('S2S Adapter', function () { $$PREBID_GLOBAL$$.aliasBidder('appnexus', alias); adapter.callBids(request, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.ext).to.deep.equal({ prebid: { @@ -970,7 +965,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(myRequest, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.imp[0].ext.appnexus).to.exist; expect(requestBid.imp[0].ext.appnexus.placement_id).to.exist.and.to.equal(10433394); @@ -994,7 +989,7 @@ describe('S2S Adapter', function () { let bidRequest = utils.deepClone(BID_REQUESTS); adapter.callBids(REQUEST, bidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.bidders).to.contain('appnexus').and.to.have.lengthOf(1); expect(requestBid.account).is.equal('1'); @@ -1008,7 +1003,7 @@ describe('S2S Adapter', function () { let bidRequest = utils.deepClone(BID_REQUESTS); adapter.callBids(REQUEST, bidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.bidders).to.contain('appnexus').and.to.have.lengthOf(1); expect(requestBid.account).is.equal('1'); @@ -1020,7 +1015,7 @@ describe('S2S Adapter', function () { bidRequest = utils.deepClone(BID_REQUESTS); adapter.callBids(REQUEST, bidRequest, addBidResponse, done, ajax); - requestBid = JSON.parse(requests[0].requestBody); + requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.bidders).to.contain('appnexus').and.to.have.lengthOf(1); expect(requestBid.account).is.equal('1'); @@ -1044,7 +1039,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.imp[0].ext.appnexus).to.haveOwnProperty('key'); expect(requestBid.imp[0].ext.appnexus.key).to.be.equal('value') }); @@ -1069,7 +1064,7 @@ describe('S2S Adapter', function () { }; adapter.callBids(REQUEST, userIdBidRequest, addBidResponse, done, ajax); - let requestBid = JSON.parse(requests[0].requestBody); + let requestBid = JSON.parse(server.requests[0].requestBody); expect(typeof requestBid.user.ext.eids).is.equal('object'); expect(Array.isArray(requestBid.user.ext.eids)).to.be.true; expect(requestBid.user.ext.eids.filter(eid => eid.source === 'adserver.org')).is.not.empty; @@ -1098,7 +1093,7 @@ describe('S2S Adapter', function () { const bidRequests = utils.deepClone(BID_REQUESTS); adapter.callBids(REQUEST, bidRequests, addBidResponse, done, ajax); - const parsedRequestBody = JSON.parse(requests[0].requestBody); + const parsedRequestBody = JSON.parse(server.requests[0].requestBody); expect(parsedRequestBody.cur).to.deep.equal(['USD']); }); @@ -1113,7 +1108,7 @@ describe('S2S Adapter', function () { const bidRequests = utils.deepClone(BID_REQUESTS); adapter.callBids(REQUEST, bidRequests, addBidResponse, done, ajax); - const parsedRequestBody = JSON.parse(requests[1].requestBody); + const parsedRequestBody = JSON.parse(server.requests[1].requestBody); expect(parsedRequestBody.cur).to.deep.equal(['NZ']); }); @@ -1125,7 +1120,7 @@ describe('S2S Adapter', function () { const bidRequests = utils.deepClone(BID_REQUESTS); adapter.callBids(REQUEST, bidRequests, addBidResponse, done, ajax); - const parsedRequestBody = JSON.parse(requests[0].requestBody); + const parsedRequestBody = JSON.parse(server.requests[0].requestBody); expect(typeof parsedRequestBody.cur).to.equal('undefined'); }); @@ -1146,7 +1141,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.ext.prebid.targeting).to.haveOwnProperty('includebidderkeys'); expect(requestBid.ext.prebid.targeting.includebidderkeys).to.equal(false); @@ -1169,7 +1164,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid.ext.prebid.targeting).to.haveOwnProperty('includewinners'); expect(requestBid.ext.prebid.targeting.includewinners).to.equal(true); @@ -1190,7 +1185,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid).to.haveOwnProperty('ext'); expect(requestBid.ext).to.haveOwnProperty('prebid'); @@ -1221,7 +1216,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid).to.haveOwnProperty('ext'); expect(requestBid.ext).to.haveOwnProperty('prebid'); @@ -1254,7 +1249,7 @@ describe('S2S Adapter', function () { config.setConfig(_config); adapter.callBids(REQUEST, BID_REQUESTS, addBidResponse, done, ajax); - const requestBid = JSON.parse(requests[0].requestBody); + const requestBid = JSON.parse(server.requests[0].requestBody); expect(requestBid).to.haveOwnProperty('ext'); expect(requestBid.ext).to.haveOwnProperty('prebid'); diff --git a/test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js b/test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js index cd414a70236..fcff6837ece 100644 --- a/test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js +++ b/test/spec/modules/prebidmanagerAnalyticsAdapter_spec.js @@ -1,12 +1,10 @@ import prebidmanagerAnalytics from 'modules/prebidmanagerAnalyticsAdapter'; import {expect} from 'chai'; +import {server} from 'test/mocks/xhr'; let events = require('src/events'); let constants = require('src/constants.json'); describe('Prebid Manager Analytics Adapter', function () { - let xhr; - let requests; - let bidWonEvent = { 'bidderCode': 'appnexus', 'width': 300, @@ -33,18 +31,8 @@ describe('Prebid Manager Analytics Adapter', function () { 'adUrl': 'ad url' }; - before(function () { - xhr = sinon.useFakeXMLHttpRequest(); - xhr.onCreate = request => requests.push(request); - }); - - after(function () { - xhr.restore(); - }); - describe('Prebid Manager Analytic tests', function () { beforeEach(function () { - requests = []; sinon.stub(events, 'getEvents').returns([]); }); @@ -67,7 +55,6 @@ describe('Prebid Manager Analytics Adapter', function () { }); it('bid won event', function() { - xhr.onCreate = request => requests.push(request); let bundleId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'; prebidmanagerAnalytics.enableAnalytics({ provider: 'prebidmanager', @@ -79,11 +66,11 @@ describe('Prebid Manager Analytics Adapter', function () { events.emit(constants.EVENTS.BID_WON, bidWonEvent); prebidmanagerAnalytics.flush(); - expect(requests.length).to.equal(1); - expect(requests[0].url).to.equal('https://endpoint.prebidmanager.com/endpoint'); - expect(requests[0].requestBody.substring(0, 2)).to.equal('1:'); + expect(server.requests.length).to.equal(1); + expect(server.requests[0].url).to.equal('https://endpoint.prebidmanager.com/endpoint'); + expect(server.requests[0].requestBody.substring(0, 2)).to.equal('1:'); - const pmEvents = JSON.parse(requests[0].requestBody.substring(2)); + const pmEvents = JSON.parse(server.requests[0].requestBody.substring(2)); expect(pmEvents.pageViewId).to.exist; expect(pmEvents.bundleId).to.equal(bundleId); expect(pmEvents.ver).to.equal(1); diff --git a/test/spec/modules/pubwiseAnalyticsAdapter_spec.js b/test/spec/modules/pubwiseAnalyticsAdapter_spec.js index b77788fff4a..b2235212cbf 100644 --- a/test/spec/modules/pubwiseAnalyticsAdapter_spec.js +++ b/test/spec/modules/pubwiseAnalyticsAdapter_spec.js @@ -4,14 +4,7 @@ let adapterManager = require('src/adapterManager').default; let constants = require('src/constants.json'); describe('PubWise Prebid Analytics', function () { - let xhr; - - before(function () { - xhr = sinon.useFakeXMLHttpRequest(); - }); - after(function () { - xhr.restore(); pubwiseAnalytics.disableAnalytics(); }); diff --git a/test/spec/modules/roxotAnalyticsAdapter_spec.js b/test/spec/modules/roxotAnalyticsAdapter_spec.js index d40ceb61cd5..1ad6ba2e86a 100644 --- a/test/spec/modules/roxotAnalyticsAdapter_spec.js +++ b/test/spec/modules/roxotAnalyticsAdapter_spec.js @@ -1,13 +1,11 @@ import roxotAnalytic from 'modules/roxotAnalyticsAdapter'; import {expect} from 'chai'; +import {server} from 'test/mocks/xhr'; let events = require('src/events'); let constants = require('src/constants.json'); describe('Roxot Prebid Analytic', function () { - let xhr; - let requests; - let roxotConfigServerUrl = 'config-server'; let roxotEventServerUrl = 'event-server'; let publisherId = 'test_roxot_prebid_analytics_publisher_id'; @@ -161,17 +159,8 @@ describe('Roxot Prebid Analytic', function () { let bidderDone = bidRequested; let bidWon = bidAdjustmentWithBid; - before(function () { - xhr = sinon.useFakeXMLHttpRequest(); - xhr.onCreate = request => requests.push(request); - }); - after(function () { - xhr.restore(); - }); - describe('correct build and send events', function () { beforeEach(function () { - requests = []; sinon.stub(events, 'getEvents').returns([]); }); afterEach(function () { @@ -188,9 +177,9 @@ describe('Roxot Prebid Analytic', function () { } }); - expect(requests.length).to.equal(1); - expect(requests[0].url).to.equal('https://' + roxotConfigServerUrl + '/c?publisherId=' + publisherId + '&host=localhost'); - requests[0].respond(200, {'Content-Type': 'application/json'}, '{"a": 1, "i": 1, "bat": 1}'); + expect(server.requests.length).to.equal(1); + expect(server.requests[0].url).to.equal('https://' + roxotConfigServerUrl + '/c?publisherId=' + publisherId + '&host=localhost'); + server.requests[0].respond(200, {'Content-Type': 'application/json'}, '{"a": 1, "i": 1, "bat": 1}'); events.emit(constants.EVENTS.AUCTION_INIT, auctionInit); events.emit(constants.EVENTS.BID_REQUESTED, bidRequested); @@ -205,13 +194,13 @@ describe('Roxot Prebid Analytic', function () { events.emit(constants.EVENTS.BIDDER_DONE, bidderDone); events.emit(constants.EVENTS.BID_WON, bidWon); - expect(requests.length).to.equal(4); + expect(server.requests.length).to.equal(4); - expect(requests[1].url).to.equal('https://' + roxotEventServerUrl + '/a?publisherId=' + publisherId + '&host=localhost'); - expect(requests[2].url).to.equal('https://' + roxotEventServerUrl + '/bat?publisherId=' + publisherId + '&host=localhost'); - expect(requests[3].url).to.equal('https://' + roxotEventServerUrl + '/i?publisherId=' + publisherId + '&host=localhost'); + expect(server.requests[1].url).to.equal('https://' + roxotEventServerUrl + '/a?publisherId=' + publisherId + '&host=localhost'); + expect(server.requests[2].url).to.equal('https://' + roxotEventServerUrl + '/bat?publisherId=' + publisherId + '&host=localhost'); + expect(server.requests[3].url).to.equal('https://' + roxotEventServerUrl + '/i?publisherId=' + publisherId + '&host=localhost'); - let auction = JSON.parse(requests[1].requestBody); + let auction = JSON.parse(server.requests[1].requestBody); expect(auction).to.include.all.keys('event', 'eventName', 'options', 'data'); expect(auction.event).to.equal('a'); @@ -228,7 +217,7 @@ describe('Roxot Prebid Analytic', function () { expect(auction.data.adUnits[bidAfterTimeoutAdUnit].bidders[bidder].status).to.equal('timeout'); expect(auction.data.adUnits[noBidAdUnit].bidders[bidder].status).to.equal('noBid'); - let bidAfterTimeout = JSON.parse(requests[2].requestBody); + let bidAfterTimeout = JSON.parse(server.requests[2].requestBody); expect(bidAfterTimeout).to.include.all.keys('event', 'eventName', 'options', 'data'); expect(bidAfterTimeout.event).to.equal('bat'); @@ -237,7 +226,7 @@ describe('Roxot Prebid Analytic', function () { expect(bidAfterTimeout.data.bidder).to.equal(bidder); expect(bidAfterTimeout.data.cpm).to.equal(bidAdjustmentAfterTimeout.cpm); - let impression = JSON.parse(requests[3].requestBody); + let impression = JSON.parse(server.requests[3].requestBody); expect(impression).to.include.all.keys('event', 'eventName', 'options', 'data'); expect(impression.event).to.equal('i'); @@ -250,7 +239,6 @@ describe('Roxot Prebid Analytic', function () { describe('support ad unit filter', function () { beforeEach(function () { - requests = []; sinon.stub(events, 'getEvents').returns([]); }); afterEach(function () { @@ -268,9 +256,9 @@ describe('Roxot Prebid Analytic', function () { } }); - expect(requests.length).to.equal(1); - expect(requests[0].url).to.equal('https://' + roxotConfigServerUrl + '/c?publisherId=' + publisherId + '&host=localhost'); - requests[0].respond(200, {'Content-Type': 'application/json'}, '{"a": 1, "i": 1, "bat": 1}'); + expect(server.requests.length).to.equal(1); + expect(server.requests[0].url).to.equal('https://' + roxotConfigServerUrl + '/c?publisherId=' + publisherId + '&host=localhost'); + server.requests[0].respond(200, {'Content-Type': 'application/json'}, '{"a": 1, "i": 1, "bat": 1}'); events.emit(constants.EVENTS.AUCTION_INIT, auctionInit); events.emit(constants.EVENTS.BID_REQUESTED, bidRequested); @@ -285,12 +273,12 @@ describe('Roxot Prebid Analytic', function () { events.emit(constants.EVENTS.BIDDER_DONE, bidderDone); events.emit(constants.EVENTS.BID_WON, bidWon); - expect(requests.length).to.equal(3); + expect(server.requests.length).to.equal(3); - expect(requests[1].url).to.equal('https://' + roxotEventServerUrl + '/a?publisherId=' + publisherId + '&host=localhost'); - expect(requests[2].url).to.equal('https://' + roxotEventServerUrl + '/bat?publisherId=' + publisherId + '&host=localhost'); + expect(server.requests[1].url).to.equal('https://' + roxotEventServerUrl + '/a?publisherId=' + publisherId + '&host=localhost'); + expect(server.requests[2].url).to.equal('https://' + roxotEventServerUrl + '/bat?publisherId=' + publisherId + '&host=localhost'); - let auction = JSON.parse(requests[1].requestBody); + let auction = JSON.parse(server.requests[1].requestBody); expect(auction.data.adUnits).to.include.all.keys(noBidAdUnit, bidAfterTimeoutAdUnit); expect(auction.data.adUnits).to.not.include.all.keys(bidAdUnit); }); @@ -298,7 +286,6 @@ describe('Roxot Prebid Analytic', function () { describe('should correct parse config', function () { beforeEach(function () { - requests = []; sinon.stub(events, 'getEvents').returns([]); }); @@ -421,7 +408,7 @@ describe('Roxot Prebid Analytic', function () { options: publisherOptions }); - requests[0].respond(500); + server.requests[0].respond(500); expect(roxotAnalytic.getOptions().serverConfig).to.deep.equal({a: 1, i: 1, bat: 1, isError: 1}); }); diff --git a/test/spec/modules/rubiconAnalyticsAdapter_spec.js b/test/spec/modules/rubiconAnalyticsAdapter_spec.js index a4d07c35bc4..83d43086f90 100644 --- a/test/spec/modules/rubiconAnalyticsAdapter_spec.js +++ b/test/spec/modules/rubiconAnalyticsAdapter_spec.js @@ -1,6 +1,7 @@ import rubiconAnalyticsAdapter, { SEND_TIMEOUT, parseBidResponse } from 'modules/rubiconAnalyticsAdapter'; import CONSTANTS from 'src/constants.json'; import { config } from 'src/config'; +import { server } from 'test/mocks/xhr'; import { setConfig, @@ -476,18 +477,12 @@ function performStandardAuction() { describe('rubicon analytics adapter', function () { let sandbox; - let xhr; - let requests; let oldScreen; let clock; beforeEach(function () { sandbox = sinon.sandbox.create(); - xhr = sandbox.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = request => requests.push(request); - sandbox.stub(events, 'getEvents').returns([]); clock = sandbox.useFakeTimers(1519767013781); @@ -554,7 +549,7 @@ describe('rubicon analytics adapter', function () { performStandardAuction(); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); }); it('should unsample', function () { @@ -568,7 +563,7 @@ describe('rubicon analytics adapter', function () { performStandardAuction(); - expect(requests.length).to.equal(0); + expect(server.requests.length).to.equal(0); }); it('should throw errors for invalid samplingFactor', function () { @@ -582,7 +577,7 @@ describe('rubicon analytics adapter', function () { performStandardAuction(); - expect(requests.length).to.equal(0); + expect(server.requests.length).to.equal(0); expect(utils.logError.called).to.equal(true); }); }); @@ -598,7 +593,7 @@ describe('rubicon analytics adapter', function () { performStandardAuction(); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); }); it('should unsample', function () { @@ -612,7 +607,7 @@ describe('rubicon analytics adapter', function () { performStandardAuction(); - expect(requests.length).to.equal(0); + expect(server.requests.length).to.equal(0); }); it('should throw errors for invalid samplingFactor', function () { @@ -626,7 +621,7 @@ describe('rubicon analytics adapter', function () { performStandardAuction(); - expect(requests.length).to.equal(0); + expect(server.requests.length).to.equal(0); expect(utils.logError.called).to.equal(true); }); }); @@ -649,8 +644,8 @@ describe('rubicon analytics adapter', function () { it('should build a batched message from prebid events', function () { performStandardAuction(); - expect(requests.length).to.equal(1); - let request = requests[0]; + expect(server.requests.length).to.equal(1); + let request = server.requests[0]; expect(request.url).to.equal('//localhost:9999/event'); @@ -724,15 +719,15 @@ describe('rubicon analytics adapter', function () { events.emit(BID_WON, MOCK.BID_WON[1]); - expect(requests.length).to.equal(2); + expect(server.requests.length).to.equal(2); - let message = JSON.parse(requests[0].requestBody); + let message = JSON.parse(server.requests[0].requestBody); validate(message); expect(message.bidsWon.length).to.equal(1); expect(message.auctions).to.deep.equal(ANALYTICS_MESSAGE.auctions); expect(message.bidsWon[0]).to.deep.equal(ANALYTICS_MESSAGE.bidsWon[0]); - message = JSON.parse(requests[1].requestBody); + message = JSON.parse(server.requests[1].requestBody); validate(message); expect(message.bidsWon.length).to.equal(1); expect(message).to.not.have.property('auctions'); @@ -747,9 +742,9 @@ describe('rubicon analytics adapter', function () { clock.tick(SEND_TIMEOUT + 1000); - expect(requests.length).to.equal(1); + expect(server.requests.length).to.equal(1); - let message = JSON.parse(requests[0].requestBody); + let message = JSON.parse(server.requests[0].requestBody); validate(message); let timedOutBid = message.auctions[0].adUnits[0].bids[0]; expect(timedOutBid.status).to.equal('error'); @@ -804,8 +799,8 @@ describe('rubicon analytics adapter', function () { performStandardAuction(); - expect(requests.length).to.equal(1); - const request = requests[0]; + expect(server.requests.length).to.equal(1); + const request = server.requests[0]; const message = JSON.parse(request.requestBody); expect(message.integration).to.equal('testType'); diff --git a/test/spec/modules/scaleableAnalyticsAdapter_spec.js b/test/spec/modules/scaleableAnalyticsAdapter_spec.js index 300f72751a4..f26d91f7aa1 100644 --- a/test/spec/modules/scaleableAnalyticsAdapter_spec.js +++ b/test/spec/modules/scaleableAnalyticsAdapter_spec.js @@ -2,7 +2,7 @@ import scaleableAnalytics from 'modules/scaleableAnalyticsAdapter'; import { expect } from 'chai'; import events from 'src/events'; import CONSTANTS from 'src/constants.json'; -import adapterManager from 'src/adapterManager'; +import { server } from 'test/mocks/xhr'; const BID_TIMEOUT = CONSTANTS.EVENTS.BID_TIMEOUT; const AUCTION_INIT = CONSTANTS.EVENTS.AUCTION_INIT; @@ -47,21 +47,8 @@ describe('Scaleable Analytics Adapter', function() { site: MOCK_DATA.site }; - let xhr; - let requests; - - before(function() { - xhr = sinon.useFakeXMLHttpRequest(); - xhr.onCreate = request => requests.push(request); - }); - - after(function() { - xhr.restore(); - }); - describe('Event Handling', function() { beforeEach(function() { - requests = []; sinon.stub(events, 'getEvents').returns([]); scaleableAnalytics.enableAnalytics({ @@ -82,7 +69,7 @@ describe('Scaleable Analytics Adapter', function() { adUnitCodes: [MOCK_DATA.adUnitCode] }); - const result = JSON.parse(requests[0].requestBody); + const result = JSON.parse(server.requests[0].requestBody); expect(result).to.deep.equal({ event: 'request', site: MOCK_DATA.site, @@ -111,7 +98,7 @@ describe('Scaleable Analytics Adapter', function() { it('should handle the bid won event', function(done) { events.emit(BID_WON, MOCK_DATA.bidResponse); - const result = JSON.parse(requests[0].requestBody); + const result = JSON.parse(server.requests[0].requestBody); expect(result).to.deep.equal({ adunit: MOCK_DATA.adUnitCode, code: MOCK_DATA.bidResponse.bidderCode, @@ -127,7 +114,7 @@ describe('Scaleable Analytics Adapter', function() { it('should handle the auction end event', function(done) { events.emit(AUCTION_END, {}); - const result = JSON.parse(requests[0].requestBody); + const result = JSON.parse(server.requests[0].requestBody); expect(result).to.deep.equal(MOCK_DATA.expectedBidResponse); done(); diff --git a/test/spec/modules/serverbidServerBidAdapter_spec.js b/test/spec/modules/serverbidServerBidAdapter_spec.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/spec/modules/sigmoidAnalyticsAdapter_spec.js b/test/spec/modules/sigmoidAnalyticsAdapter_spec.js index 0dee6337e0d..c07d35ae68f 100644 --- a/test/spec/modules/sigmoidAnalyticsAdapter_spec.js +++ b/test/spec/modules/sigmoidAnalyticsAdapter_spec.js @@ -5,13 +5,8 @@ let adapterManager = require('src/adapterManager').default; let constants = require('src/constants.json'); describe('sigmoid Prebid Analytic', function () { - let xhr; - before(function () { - xhr = sinon.useFakeXMLHttpRequest(); - }) after(function () { sigmoidAnalytic.disableAnalytics(); - xhr.restore(); }); describe('enableAnalytics', function () { diff --git a/test/spec/modules/sonobiAnalyticsAdapter_spec.js b/test/spec/modules/sonobiAnalyticsAdapter_spec.js index 389dfee34f9..d857c060f90 100644 --- a/test/spec/modules/sonobiAnalyticsAdapter_spec.js +++ b/test/spec/modules/sonobiAnalyticsAdapter_spec.js @@ -1,25 +1,20 @@ import sonobiAnalytics from 'modules/sonobiAnalyticsAdapter'; import {expect} from 'chai'; +import {server} from 'test/mocks/xhr'; let events = require('src/events'); let adapterManager = require('src/adapterManager').default; let constants = require('src/constants.json'); describe('Sonobi Prebid Analytic', function () { - let xhr; - let requests = []; var clock; describe('enableAnalytics', function () { beforeEach(function () { - requests = []; - xhr = sinon.useFakeXMLHttpRequest(); - xhr.onCreate = request => requests.push(request); sinon.stub(events, 'getEvents').returns([]); clock = sinon.useFakeTimers(Date.now()); }); afterEach(function () { - xhr.restore(); events.getEvents.restore(); clock.restore(); }); @@ -81,8 +76,8 @@ describe('Sonobi Prebid Analytic', function () { events.emit(constants.EVENTS.AUCTION_END, {auctionId: '13', bidsReceived: [bid]}); clock.tick(5000); - expect(requests).to.have.length(1); - expect(JSON.parse(requests[0].requestBody)).to.have.length(3) + expect(server.requests).to.have.length(1); + expect(JSON.parse(server.requests[0].requestBody)).to.have.length(3) done(); }); }); diff --git a/test/spec/modules/sortableAnalyticsAdapter_spec.js b/test/spec/modules/sortableAnalyticsAdapter_spec.js index 90bd5fcdf22..866f3fcb512 100644 --- a/test/spec/modules/sortableAnalyticsAdapter_spec.js +++ b/test/spec/modules/sortableAnalyticsAdapter_spec.js @@ -3,11 +3,10 @@ import sortableAnalyticsAdapter, {TIMEOUT_FOR_REGISTRY, DEFAULT_PBID_TIMEOUT} fr import events from 'src/events'; import CONSTANTS from 'src/constants.json'; import * as prebidGlobal from 'src/prebidGlobal'; +import {server} from 'test/mocks/xhr'; describe('Sortable Analytics Adapter', function() { - let requests; let sandbox; - let xhr; let clock; const initialConfig = { @@ -137,8 +136,6 @@ describe('Sortable Analytics Adapter', function() { beforeEach(function() { sandbox = sinon.sandbox.create(); - xhr = sandbox.useFakeXMLHttpRequest(); - xhr.onCreate = (request) => requests.push(request); clock = sandbox.useFakeTimers(); sandbox.stub(events, 'getEvents').returns([]); sandbox.stub(prebidGlobal, 'getGlobal').returns({ @@ -152,7 +149,6 @@ describe('Sortable Analytics Adapter', function() { } }); - requests = []; sortableAnalyticsAdapter.enableAnalytics(initialConfig); }); @@ -190,8 +186,8 @@ describe('Sortable Analytics Adapter', function() { clock.tick(DEFAULT_PBID_TIMEOUT); - 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.have.own.property('pbid'); expect(result.pbid).to.deep.include({ ai: 'fb8d579a-5c3f-4705-ab94-3cff39005d9e', @@ -242,10 +238,10 @@ describe('Sortable Analytics Adapter', function() { clock.tick(TIMEOUT_FOR_REGISTRY); - expect(requests.length).to.equal(2); - const pbid_req = JSON.parse(requests[0].requestBody); + expect(server.requests.length).to.equal(2); + const pbid_req = JSON.parse(server.requests[0].requestBody); expect(pbid_req).to.have.own.property('pbid'); - const pbwon_req = JSON.parse(requests[1].requestBody); + const pbwon_req = JSON.parse(server.requests[1].requestBody); expect(pbwon_req).to.have.own.property('pbrw'); expect(pbwon_req.pbrw).to.deep.equal({ ac: '300x250', @@ -270,10 +266,10 @@ describe('Sortable Analytics Adapter', function() { clock.tick(TIMEOUT_FOR_REGISTRY); - expect(requests.length).to.equal(2); - const pbid_req = JSON.parse(requests[0].requestBody); + expect(server.requests.length).to.equal(2); + const pbid_req = JSON.parse(server.requests[0].requestBody); expect(pbid_req).to.have.own.property('pbid'); - const pbto_req = JSON.parse(requests[1].requestBody); + const pbto_req = JSON.parse(server.requests[1].requestBody); expect(pbto_req).to.have.own.property('pbto'); expect(pbto_req.pbto).to.deep.equal({ ai: 'fb8d579a-5c3f-4705-ab94-3cff39005d9e', @@ -291,8 +287,8 @@ describe('Sortable Analytics Adapter', function() { clock.tick(TIMEOUT_FOR_REGISTRY); - expect(requests.length).to.equal(1); - const err_req = JSON.parse(requests[0].requestBody); + expect(server.requests.length).to.equal(1); + const err_req = JSON.parse(server.requests[0].requestBody); expect(err_req).to.have.own.property('pber'); expect(err_req.pber).to.include({ args: '{}', diff --git a/test/spec/modules/sovrnAnalyticsAdapter_spec.js b/test/spec/modules/sovrnAnalyticsAdapter_spec.js index 299e22ca790..25c615f0d5c 100644 --- a/test/spec/modules/sovrnAnalyticsAdapter_spec.js +++ b/test/spec/modules/sovrnAnalyticsAdapter_spec.js @@ -1,7 +1,8 @@ -import sovrnAnalyticsAdapter from '../../../modules/sovrnAnalyticsAdapter' -import { expect } from 'chai' -import {config} from 'src/config' -import adaptermanager from 'src/adapterManager' +import sovrnAnalyticsAdapter from '../../../modules/sovrnAnalyticsAdapter'; +import { expect } from 'chai'; +import {config} from 'src/config'; +import adaptermanager from 'src/adapterManager'; +import { server } from 'test/mocks/xhr'; var assert = require('assert'); let events = require('src/events'); @@ -171,16 +172,10 @@ let bidAdjustmentNoMatchingRequest = { let bidResponseNoMatchingRequest = bidAdjustmentNoMatchingRequest; describe('Sovrn Analytics Adapter', function () { - let xhr; - let requests; beforeEach(() => { - xhr = sinon.useFakeXMLHttpRequest(); - xhr.onCreate = request => requests.push(request); - requests = []; sinon.stub(events, 'getEvents').returns([]); }); afterEach(() => { - xhr.restore(); events.getEvents.restore(); }); @@ -436,7 +431,7 @@ describe('Sovrn Analytics Adapter', function () { emitEvent('BID_RESPONSE', bidResponse, auctionId); emitEvent('BID_RESPONSE', bidResponse2, auctionId) emitEvent('AUCTION_END', {}, auctionId); - let requestBody = JSON.parse(requests[0].requestBody); + let requestBody = JSON.parse(server.requests[0].requestBody); let requestsFromRequestBody = requestBody.requests[0]; let bidsFromRequests = requestsFromRequestBody.bids[0]; expect(requestBody).to.deep.include(expectedPostBody); @@ -512,7 +507,7 @@ describe('Sovrn Analytics Adapter', function () { it('should send bid won data ', function () { emitEvent('AUCTION_INIT', auctionInit, auctionId); emitEvent('BID_WON', bidWonEvent, auctionId); - let requestBody = JSON.parse(requests[0].requestBody); + let requestBody = JSON.parse(server.requests[0].requestBody); expect(requestBody).to.deep.include(expectedBidWonBody); expect(requestBody.winningBid).to.deep.include(expectedWinningBid); }); @@ -536,9 +531,9 @@ describe('Sovrn Analytics Adapter', function () { emitEvent('AUCTION_INIT', auctionInit, auctionId) emitEvent('BID_REQUESTED', bidRequested, auctionId) emitEvent('AUCTION_END', {}, auctionId) - requests[0].respond(200) + server.requests[0].respond(200) emitEvent('BID_RESPONSE', bidResponse, auctionId) - let requestBody = JSON.parse(requests[1].requestBody) + let requestBody = JSON.parse(server.requests[1].requestBody) expect(requestBody.payload).to.equal('error') expect(requestBody.message).to.include('Event Received after Auction Close Auction Id') }) diff --git a/test/spec/modules/vubleAnalyticsAdapter_spec.js b/test/spec/modules/vubleAnalyticsAdapter_spec.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/spec/unit/core/bidderFactory_spec.js b/test/spec/unit/core/bidderFactory_spec.js index e8ddf52c128..e1b2f205bf7 100644 --- a/test/spec/unit/core/bidderFactory_spec.js +++ b/test/spec/unit/core/bidderFactory_spec.js @@ -2,10 +2,10 @@ import { newBidder, registerBidder, preloadBidderMappingFile } from 'src/adapter import adapterManager from 'src/adapterManager'; import * as ajax from 'src/ajax'; import { expect } from 'chai'; -import { STATUS } from 'src/constants'; import { userSync } from 'src/userSync' import * as utils from 'src/utils'; import { config } from 'src/config'; +import { server } from 'test/mocks/xhr'; const CODE = 'sampleBidder'; const MOCK_BIDS_REQUEST = { @@ -792,7 +792,7 @@ describe('preload mapping url hook', function() { let adapterManagerStub; beforeEach(function () { - fakeTranslationServer = sinon.fakeServer.create(); + fakeTranslationServer = server; getLocalStorageStub = sinon.stub(utils, 'getDataFromLocalStorage'); adapterManagerStub = sinon.stub(adapterManager, 'getBidAdapter'); }); diff --git a/test/spec/unit/pbjs_api_spec.js b/test/spec/unit/pbjs_api_spec.js index b59911153e5..094220ea521 100644 --- a/test/spec/unit/pbjs_api_spec.js +++ b/test/spec/unit/pbjs_api_spec.js @@ -1142,7 +1142,6 @@ describe('Unit: Prebid Module', function () { describe('requestBids', function () { let logMessageSpy; let makeRequestsStub; - let xhr; let adUnits; let clock; let bidsBackHandlerStub = sinon.stub(); @@ -1186,7 +1185,6 @@ describe('Unit: Prebid Module', function () { logMessageSpy = sinon.spy(utils, 'logMessage'); makeRequestsStub = sinon.stub(adapterManager, 'makeBidRequests'); makeRequestsStub.returns(bidRequests); - xhr = sinon.useFakeXMLHttpRequest(); adUnits = [{ code: 'adUnit-code', @@ -1215,7 +1213,6 @@ describe('Unit: Prebid Module', function () { adapterManager.makeBidRequests.restore(); auctionModule.newAuction.restore(); utils.logMessage.restore(); - xhr.restore(); }); it('should execute callback after timeout', function () { @@ -1337,18 +1334,6 @@ describe('Unit: Prebid Module', function () { }); describe('requestBids', function () { - let xhr; - let requests; - - beforeEach(function () { - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = request => requests.push(request); - }); - - afterEach(function () { - xhr.restore(); - }); var adUnitsBackup; var auctionManagerStub; let logMessageSpy; diff --git a/test/spec/videoCache_spec.js b/test/spec/videoCache_spec.js index 8f423a799ac..e2a1b106554 100644 --- a/test/spec/videoCache_spec.js +++ b/test/spec/videoCache_spec.js @@ -2,6 +2,7 @@ import 'mocha/mocha'; import chai from 'chai'; import { getCacheUrl, store } from 'src/videoCache'; import { config } from 'src/config'; +import { server } from 'test/mocks/xhr'; const should = chai.should(); @@ -17,24 +18,11 @@ describe('The video cache', function () { } describe('when the cache server is unreachable', function () { - let xhr; - let requests; - - beforeEach(function () { - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = (request) => requests.push(request); - }); - - afterEach(function () { - xhr.restore(); - }); - it('should execute the callback with an error when store() is called', function () { const callback = sinon.spy(); store([{ vastUrl: 'my-mock-url.com' }], callback); - requests[0].respond(503, { + server.requests[0].respond(503, { 'Content-Type': 'plain/text', }, 'The server could not save anything at the moment.'); @@ -44,13 +32,7 @@ describe('The video cache', function () { }); describe('when the cache server is available', function () { - let xhr; - let requests; - beforeEach(function () { - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = (request) => requests.push(request); config.setConfig({ cache: { url: 'https://prebid.adnxs.com/pbc/v1/cache' @@ -59,7 +41,6 @@ describe('The video cache', function () { }); afterEach(function () { - xhr.restore(); config.resetConfig(); }); @@ -139,7 +120,7 @@ describe('The video cache', function () { }]; store(bids, function () { }); - const request = requests[0]; + const request = server.requests[0]; request.method.should.equal('POST'); request.url.should.equal('https://prebid.adnxs.com/pbc/v1/cache'); request.requestHeaders['Content-Type'].should.equal('text/plain;charset=utf-8'); @@ -215,7 +196,7 @@ describe('The video cache', function () { function assertRequestMade(bid, expectedValue) { store([bid], function () { }); - const request = requests[0]; + const request = server.requests[0]; request.method.should.equal('POST'); request.url.should.equal('https://prebid.adnxs.com/pbc/v1/cache'); request.requestHeaders['Content-Type'].should.equal('text/plain;charset=utf-8'); @@ -232,7 +213,7 @@ describe('The video cache', function () { function fakeServerCall(bid, responseBody) { const callback = sinon.spy(); store([bid], callback); - requests[0].respond( + server.requests[0].respond( 200, { 'Content-Type': 'application/json', diff --git a/test/test_index.js b/test/test_index.js index 206f1be1f52..53d75e36176 100644 --- a/test/test_index.js +++ b/test/test_index.js @@ -1,5 +1,6 @@ require('test/helpers/prebidGlobal.js'); require('test/mocks/adloaderStub.js'); +require('test/mocks/xhr.js'); var testsContext = require.context('.', true, /_spec$/); testsContext.keys().forEach(testsContext);