diff --git a/modules/openxBidAdapter.js b/modules/openxBidAdapter.js index 1b9766553c2d..0271bc15a588 100644 --- a/modules/openxBidAdapter.js +++ b/modules/openxBidAdapter.js @@ -1,298 +1,231 @@ import { config } from 'src/config'; -const bidfactory = require('src/bidfactory.js'); -const bidmanager = require('src/bidmanager.js'); -const ajax = require('src/ajax'); -const CONSTANTS = require('src/constants.json'); -const utils = require('src/utils.js'); -const adaptermanager = require('src/adaptermanager'); - -const OpenxAdapter = function OpenxAdapter() { - const BIDDER_CODE = 'openx'; - const BIDDER_CONFIG = 'hb_pb'; - const BIDDER_VERSION = '1.0.1'; - let startTime; - let timeout = config.getConfig('bidderTimeout'); - let shouldSendBoPixel = true; - - let pdNode = null; - - function oxARJResponse (oxResponseObj) { +import {registerBidder} from 'src/adapters/bidderFactory'; +import * as utils from 'src/utils'; +import {userSync} from 'src/userSync'; + +const SUPPORTED_AD_TYPES = ['banner']; +const BIDDER_CODE = 'openx'; +const BIDDER_CONFIG = 'hb_pb'; +const BIDDER_VERSION = '2.0.0'; + +export const spec = { + code: BIDDER_CODE, + supportedMediaTypes: SUPPORTED_AD_TYPES, + isBidRequestValid: function(bid) { + return !!(bid.params.unit || bid.params.delDomain); + }, + buildRequests: function(bids) { + let isIfr; + let currentURL = (window.parent !== window) ? document.referrer : window.location.href; try { - oxResponseObj = JSON.parse(oxResponseObj); - } catch (_) { - // Could not parse response, changing to an empty response instead - oxResponseObj = { - ads: {} - }; + isIfr = window.self !== window.top; + } catch (e) { + isIfr = false; + } + if (bids.length === 0) { + return; } + + let delDomain = bids[0].params.delDomain; + let configuredBc = bids[0].params.bc; + let bc = configuredBc || `${BIDDER_CONFIG}_${BIDDER_VERSION}`; + + return buildOXRequest(bids, { + ju: currentURL, + jr: currentURL, + ch: document.charSet || document.characterSet, + res: `${screen.width}x${screen.height}x${screen.colorDepth}`, + ifr: isIfr, + tz: new Date().getTimezoneOffset(), + tws: getViewportDimensions(isIfr), + ef: 'bt%2Cdb', + be: 1, + bc: bc, + nocache: new Date().getTime() + }, + delDomain); + }, + interpretResponse: function(oxResponseObj, bidRequest) { + let bidResponses = []; let adUnits = oxResponseObj.ads.ad; if (oxResponseObj.ads && oxResponseObj.ads.pixels) { - makePDCall(oxResponseObj.ads.pixels); + userSync.registerSync('iframe', 'openx', oxResponseObj.ads.pixels); } - if (!adUnits) { adUnits = []; } - - let bids = $$PREBID_GLOBAL$$._bidsRequested.find(bidSet => bidSet.bidderCode === 'openx').bids; - for (let i = 0; i < bids.length; i++) { - let bid = bids[i]; - let auid = null; - let adUnit = null; - // find the adunit in the response - for (let j = 0; j < adUnits.length; j++) { - adUnit = adUnits[j]; - if (String(bid.params.unit) === String(adUnit.adunitid) && adUnitHasValidSizeFromBid(adUnit, bid) && !adUnit.used) { - auid = adUnit.adunitid; - break; - } - } - - let beaconParams = { - bd: +(new Date()) - startTime, - br: '0', // may be 0, t, or p - bt: Math.min(timeout, window.PREBID_TIMEOUT || config.getConfig('bidderTimeout')), - bs: window.location.hostname - }; - // no fill :( - if (!auid || !adUnit.pub_rev) { - addBidResponse(null, bid); - continue; - } - adUnit.used = true; - - beaconParams.br = beaconParams.bt < beaconParams.bd ? 't' : 'p'; - beaconParams.bp = adUnit.pub_rev; - beaconParams.ts = adUnit.ts; - addBidResponse(adUnit, bid); - if (shouldSendBoPixel === true) { - buildBoPixel(adUnit.creative[0], beaconParams); - } - } - }; - - function getViewportDimensions(isIfr) { - let width; - let height; - let tWin = window; - let tDoc = document; - let docEl = tDoc.documentElement; - let body; - - if (isIfr) { - try { - tWin = window.top; - tDoc = window.top.document; - } catch (e) { - return; - } - docEl = tDoc.documentElement; - body = tDoc.body; - - width = tWin.innerWidth || docEl.clientWidth || body.clientWidth; - height = tWin.innerHeight || docEl.clientHeight || body.clientHeight; - } else { - docEl = tDoc.documentElement; - width = tWin.innerWidth || docEl.clientWidth; - height = tWin.innerHeight || docEl.clientHeight; - } - - return `${width}x${height}`; - } - - function makePDCall(pixelsUrl) { - let pdFrame = utils.createInvisibleIframe(); - let name = 'openx-pd'; - pdFrame.setAttribute('id', name); - pdFrame.setAttribute('name', name); - let rootNode = document.body; - - if (!rootNode) { - return; - } - - pdFrame.src = pixelsUrl; - - if (pdNode) { - pdNode.parentNode.replaceChild(pdFrame, pdNode); - pdNode = pdFrame; - } else { - pdNode = rootNode.appendChild(pdFrame); - } + bidResponses = createBidResponses(adUnits, bidRequest.payload); + return bidResponses; } +}; - function addBidResponse(adUnit, bid) { - let bidResponse = bidfactory.createBid(adUnit ? CONSTANTS.STATUS.GOOD : CONSTANTS.STATUS.NO_BID, bid); +function createBidResponses(adUnits, {bids, startTime}) { + let bidResponses = []; + for (let i = 0; i < adUnits.length; i++) { + let adUnit = adUnits[i]; + let bidResponse = {}; + bidResponse.requestId = bids[i].bidId; bidResponse.bidderCode = BIDDER_CODE; + let shouldSendBoPixel = bids.some((bid, index, arr) => { return bid.params.sendBoPixel; }); - if (adUnit) { - let creative = adUnit.creative[0]; - bidResponse.ad = adUnit.html; + if (adUnit.pub_rev) { bidResponse.cpm = Number(adUnit.pub_rev) / 1000; - bidResponse.ad_id = adUnit.adid; - if (adUnit.deal_id) { - bidResponse.dealId = adUnit.deal_id; - } - if (creative) { - bidResponse.width = creative.width; - bidResponse.height = creative.height; - } - if (adUnit.tbd) { - bidResponse.tbd = adUnit.tbd; - } - } - bidmanager.addBidResponse(bid.placementCode, bidResponse); - } - - function buildQueryStringFromParams(params) { - for (let key in params) { - if (params.hasOwnProperty(key)) { - if (!params[key]) { - delete params[key]; - } - } - } - return utils._map(Object.keys(params), key => `${key}=${params[key]}`) - .join('&'); + } else { + bidResponse.cpm = 0; + } + let creative = adUnit.creative[0]; + if (creative) { + bidResponse.width = creative.width; + bidResponse.height = creative.height; + } + bidResponse.creativeId = creative.id; + bidResponse.ad = adUnit.html; + if (adUnit.deal_id) { + bidResponse.dealId = adUnit.deal_id; + } + // default 5 mins + bidResponse.ttl = 300; + // true is net, false is gross + bidResponse.netRevenue = true; + bidResponse.currency = adUnit.currency; + + // additional fields to add + if (adUnit.tbd) { + bidResponse.tbd = adUnit.tbd; + } + bidResponse.ts = adUnit.ts; + + let bt = config.getConfig('bidderTimeout'); + if (window.PREBID_TIMEOUT) { + bt = Math.min(window.PREBID_TIMEOUT, bt); + } + let beaconParams = { + bd: +(new Date()) - startTime, + br: '0', // may be 0, t, or p + bt: bt, + bs: window.location.hostname + }; + + beaconParams.br = beaconParams.bt < beaconParams.bd ? 't' : 'p'; + beaconParams.bp = adUnit.pub_rev; + beaconParams.ts = adUnit.ts; + if (shouldSendBoPixel) { + buildBoPixel(adUnit.creative[0], beaconParams); + } + bidResponses.push(bidResponse); } + return bidResponses; +} - function buildBoPixel(creative, params) { - let img = new Image(); - let recordPixel = creative.tracking.impression; - let boBase = recordPixel.match(/([^?]+\/)ri\?/); +function buildBoPixel(creative, params) { + let img = new Image(); + let recordPixel = creative.tracking.impression; + let boBase = recordPixel.match(/([^?]+\/)ri\?/); - if (boBase) { - img.src = `${boBase[1]}bo?${buildQueryStringFromParams(params)}`; - } + if (boBase) { + img.src = `${boBase[1]}bo?${buildQueryStringFromParams(params)}`; } +} - function adUnitHasValidSizeFromBid(adUnit, bid) { - let sizes = utils.parseSizesInput(bid.sizes); - let sizeLength = (sizes && sizes.length) || 0; - let found = false; - let creative = adUnit.creative && adUnit.creative[0]; - let creative_size = String(creative.width) + 'x' + String(creative.height); - - if (utils.isArray(sizes)) { - for (let i = 0; i < sizeLength; i++) { - let size = sizes[i]; - if (String(size) === String(creative_size)) { - found = true; - break; - } +function buildQueryStringFromParams(params) { + for (let key in params) { + if (params.hasOwnProperty(key)) { + if (!params[key]) { + delete params[key]; } } - return found; - } - - function formatCustomParms(customKey, customParams) { - let value = customParams[customKey]; - if (Array.isArray(value)) { - // if value is an array, join them with commas first - value = value.join(','); - } - // return customKey=customValue format, escaping + to . and / to _ - return (customKey + '=' + value).replace('+', '.').replace('/', '_') } - - function buildRequest(bids, params, delDomain) { - if (!utils.isArray(bids)) { + return utils._map(Object.keys(params), key => `${key}=${params[key]}`) + .join('&'); +} + +function getViewportDimensions(isIfr) { + let width; + let height; + let tWin = window; + let tDoc = document; + let docEl = tDoc.documentElement; + let body; + + if (isIfr) { + try { + tWin = window.top; + tDoc = window.top.document; + } catch (e) { return; } + docEl = tDoc.documentElement; + body = tDoc.body; - params.auid = utils._map(bids, bid => bid.params.unit).join('%2C'); - params.dddid = utils._map(bids, bid => bid.transactionId).join('%2C'); - params.aus = utils._map(bids, bid => { - return utils.parseSizesInput(bid.sizes).join(','); - }).join('|'); + width = tWin.innerWidth || docEl.clientWidth || body.clientWidth; + height = tWin.innerHeight || docEl.clientHeight || body.clientHeight; + } else { + docEl = tDoc.documentElement; + width = tWin.innerWidth || docEl.clientWidth; + height = tWin.innerHeight || docEl.clientHeight; + } - let customParamsForAllBids = []; - let hasCustomParam = false; - bids.forEach(function (bid) { - if (bid.params.customParams) { - let customParamsForBid = utils._map(Object.keys(bid.params.customParams), customKey => formatCustomParms(customKey, bid.params.customParams)); - let formattedCustomParams = window.btoa(customParamsForBid.join('&')); - hasCustomParam = true; - customParamsForAllBids.push(formattedCustomParams); - } else { - customParamsForAllBids.push(''); - } - }); - if (hasCustomParam) { - params.tps = customParamsForAllBids.join('%2C'); - } + return `${width}x${height}`; +} - let customFloorsForAllBids = []; - let hasCustomFloor = false; - bids.forEach(function (bid) { - if (bid.params.customFloor) { - customFloorsForAllBids.push(bid.params.customFloor * 1000); - hasCustomFloor = true; - } else { - customFloorsForAllBids.push(0); - } - }); - if (hasCustomFloor) { - params.aumfs = customFloorsForAllBids.join('%2C'); - } +function formatCustomParms(customKey, customParams) { + let value = customParams[customKey]; + if (utils.isArray(value)) { + // if value is an array, join them with commas first + value = value.join(','); + } + // return customKey=customValue format, escaping + to . and / to _ + return (customKey.toLowerCase() + '=' + value.toLowerCase()).replace('+', '.').replace('/', '_') +} - try { - let queryString = buildQueryStringFromParams(params); - let url = `//${delDomain}/w/1.0/arj?${queryString}`; - ajax.ajax(url, oxARJResponse, void (0), { - withCredentials: true - }); - } catch (err) { - utils.logMessage(`Ajax call failed due to ${err}`); - } +function buildOXRequest(bids, oxParams, delDomain) { + if (!utils.isArray(bids)) { + return; } - function callBids(params) { - let isIfr; - const bids = params.bids || []; - let currentURL = (window.parent !== window) ? document.referrer : window.location.href; - currentURL = currentURL && encodeURIComponent(currentURL); - try { - isIfr = window.self !== window.top; - } catch (e) { - isIfr = false; - } - if (bids.length === 0) { - return; + oxParams.auid = utils._map(bids, bid => bid.params.unit).join(','); + oxParams.dddid = utils._map(bids, bid => bid.transactionId).join(','); + oxParams.aus = utils._map(bids, bid => { + return utils.parseSizesInput(bid.sizes).join(','); + }).join('|'); + + let customParamsForAllBids = []; + let hasCustomParam = false; + bids.forEach(function (bid) { + if (bid.params.customParams) { + let customParamsForBid = utils._map(Object.keys(bid.params.customParams), customKey => formatCustomParms(customKey, bid.params.customParams)); + let formattedCustomParams = window.btoa(customParamsForBid.join('&')); + hasCustomParam = true; + customParamsForAllBids.push(formattedCustomParams); + } else { + customParamsForAllBids.push(''); } + }); + if (hasCustomParam) { + oxParams.tps = customParamsForAllBids.join(','); + } - let delDomain = bids[0].params.delDomain; - let bcOverride = bids[0].params.bc; - - startTime = new Date(params.start); - if (params.timeout) { - timeout = params.timeout; - } - if (bids[0].params.hasOwnProperty('sendBoPixel') && typeof (bids[0].params.sendBoPixel) === 'boolean') { - shouldSendBoPixel = bids[0].params.sendBoPixel; + let customFloorsForAllBids = []; + let hasCustomFloor = false; + bids.forEach(function (bid) { + if (bid.params.customFloor) { + customFloorsForAllBids.push(bid.params.customFloor * 1000); + hasCustomFloor = true; + } else { + customFloorsForAllBids.push(0); } - - buildRequest(bids, { - ju: currentURL, - jr: currentURL, - ch: document.charSet || document.characterSet, - res: `${screen.width}x${screen.height}x${screen.colorDepth}`, - ifr: isIfr, - tz: startTime.getTimezoneOffset(), - tws: getViewportDimensions(isIfr), - ef: 'bt%2Cdb', - be: 1, - bc: bcOverride || `${BIDDER_CONFIG}_${BIDDER_VERSION}`, - nocache: new Date().getTime() - }, - delDomain); + }); + if (hasCustomFloor) { + oxParams.aumfs = customFloorsForAllBids.join(','); } + let url = `//${delDomain}/w/1.0/arj`; return { - callBids: callBids + method: 'GET', + url: url, + data: oxParams, + payload: {'bids': bids, 'startTime': new Date()} }; -}; - -adaptermanager.registerBidAdapter(new OpenxAdapter(), 'openx'); +} -module.exports = OpenxAdapter; +registerBidder(spec); diff --git a/test/spec/modules/openxBidAdapter_spec.js b/test/spec/modules/openxBidAdapter_spec.js index c08e8c256e61..d712dd888380 100644 --- a/test/spec/modules/openxBidAdapter_spec.js +++ b/test/spec/modules/openxBidAdapter_spec.js @@ -1,277 +1,221 @@ -const expect = require('chai').expect; -const assert = require('chai').assert; -const adapter = require('modules/openxBidAdapter')(); -const bidmanager = require('src/bidmanager'); -const adloader = require('src/adloader'); -const CONSTANTS = require('src/constants.json'); -const ajax = require('src/ajax'); +import { expect } from 'chai'; +import { spec } from 'modules/openxBidAdapter'; +import { newBidder } from 'src/adapters/bidderFactory'; -describe('openx adapter tests', function () { - describe('test openx callback response', function () { - let stubAjax; - let stubAddBidResponse; - this.response = null; - let responseHandlerCallback = (_url, callback, _data, _params) => { - return callback(this.response); +const URLBASE = '/w/1.0/arj'; + +describe('OpenxAdapter', () => { + const adapter = newBidder(spec); + + describe('inherited functions', () => { + it('exists and is a function', () => { + expect(adapter.callBids).to.exist.and.to.be.a('function'); + }); + }); + + describe('isBidRequestValid', () => { + let bid = { + 'bidder': 'openx', + 'params': { + 'unit': '12345678', + 'delDomain': 'test-del-domain' + }, + 'adUnitCode': 'adunit-code', + 'sizes': [[300, 250], [300, 600]], + 'bidId': '30b31c1838de1e', + 'bidderRequestId': '22edbae2733bf6', + 'auctionId': '1d1a030790a475', }; - beforeEach(() => { - stubAjax = sinon.stub(ajax, 'ajax', responseHandlerCallback); - stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse'); - sinon.stub(document.body, 'appendChild'); + it('should return true when required params found', () => { + expect(spec.isBidRequestValid(bid)).to.equal(true); }); - afterEach(() => { - stubAjax.restore(); - stubAddBidResponse.restore(); - this.response = null; - document.body.appendChild.restore(); + + it('should return false when required params are not passed', () => { + let bid = Object.assign({}, bid); + delete bid.params; + bid.params = {}; + expect(spec.isBidRequestValid(bid)).to.equal(false); }); - it('should add empty bid responses if no bids returned', () => { - // empty ads in bidresponse - this.response = JSON.stringify({ - 'ads': - { - 'version': 1, - 'count': 1, - 'pixels': 'http://testpixels.net', - 'ad': [] - } - }); + }); - let bidderRequest = { - bidderCode: 'openx', - bids: [ - { - bidId: 'bidId1', - bidder: 'openx', - params: { - delDomain: 'delDomain1', - unit: '1234' - }, - sizes: [[300, 250]], - placementCode: 'test-gpt-div-1234' - } - ] - }; + describe('buildRequests', () => { + let bidRequests = [{ + 'bidder': 'openx', + 'params': { + 'unit': '12345678', + 'delDomain': 'test-del-domain' + }, + 'adUnitCode': 'adunit-code', + 'sizes': [[300, 250], [300, 600]], + 'bidId': '30b31c1838de1e', + 'bidderRequestId': '22edbae2733bf6', + 'auctionId': '1d1a030790a475', + }]; + + it('should send bid request to openx url via GET', () => { + const request = spec.buildRequests(bidRequests); + expect(request.url).to.equal('//' + bidRequests[0].params.delDomain + URLBASE); + expect(request.method).to.equal('GET'); + }); - $$PREBID_GLOBAL$$._bidsRequested.push(bidderRequest); - adapter.callBids(bidderRequest); + it('should have the correct parameters', () => { + const request = spec.buildRequests(bidRequests); + const dataParams = request.data; - let bidPlacementCode1 = stubAddBidResponse.getCall(0).args[0]; - let bidResponse1 = stubAddBidResponse.getCall(0).args[1]; - expect(bidPlacementCode1).to.equal('test-gpt-div-1234'); - expect(bidResponse1.getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - expect(bidResponse1.bidderCode).to.equal('openx'); + expect(dataParams.auid).to.exist; + expect(dataParams.auid).to.equal('12345678'); + expect(dataParams.aus).to.exist; + expect(dataParams.aus).to.equal('300x250,300x600'); }); - it('should add bid responses if bids are returned', () => { - let bidderRequest = { - bidderCode: 'openx', - bids: [ - { - bidId: 'bidId1', - bidder: 'openx', - params: { - delDomain: 'delDomain1', - unit: '1234' - }, - sizes: [[300, 250]], - placementCode: 'test-gpt-div-1234' - } - ] - }; - - this.response = JSON.stringify({ - 'ads': + it('should send out custom params on bids that have customParams specified', () => { + let bidRequest = Object.assign({}, + bidRequests[0], { - 'version': 1, - 'count': 1, - 'pixels': 'http://testpixels.net', - 'ad': [ - { - 'adunitid': 1234, - 'adid': 5678, - 'type': 'html', - 'html': 'test_html', - 'framed': 1, - 'is_fallback': 0, - 'ts': 'ts', - 'cpipc': 1000, - 'pub_rev': '1000', - 'adv_id': 'adv_id', - 'brand_id': '', - 'creative': [ - { - 'width': '300', - 'height': '250', - 'target': '_blank', - 'mime': 'text/html', - 'media': 'test_media', - 'tracking': { - 'impression': 'test_impression', - 'inview': 'test_inview', - 'click': 'test_click' - } - } - ] - }] + params: { + 'unit': '12345678', + 'delDomain': 'test-del-domain', + 'customParams': {'Test1': 'testval1+', 'test2': ['testval2/', 'testval3']} + } } - }); - - $$PREBID_GLOBAL$$._bidsRequested.push(bidderRequest); - adapter.callBids(bidderRequest); + ); - let bidPlacementCode1 = stubAddBidResponse.getCall(0).args[0]; - let bidResponse1 = stubAddBidResponse.getCall(0).args[1]; - let bid1width = '300'; - let bid1height = '250'; - let cpm = 1; - expect(bidPlacementCode1).to.equal('test-gpt-div-1234'); - expect(bidResponse1.getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD); - expect(bidResponse1.bidderCode).to.equal('openx'); - expect(bidResponse1.width).to.equal(bid1width); - expect(bidResponse1.height).to.equal(bid1height); - expect(bidResponse1.cpm).to.equal(cpm); - }); - }); - describe('test openx ad requests', () => { - let spyAjax; - let spyBtoa; - beforeEach(() => { - spyAjax = sinon.spy(ajax, 'ajax'); - spyBtoa = sinon.spy(window, 'btoa'); - sinon.stub(document.body, 'appendChild'); - }); - afterEach(() => { - spyAjax.restore(); - spyBtoa.restore(); - document.body.appendChild.restore(); - }); + const request = spec.buildRequests([bidRequest]); + const dataParams = request.data; - it('should not call ajax when inputting with empty params', () => { - adapter.callBids({}); - assert(!spyAjax.called); + expect(dataParams.tps).to.exist; + expect(dataParams.tps).to.equal(btoa('test1=testval1.&test2=testval2_,testval3')); }); - it('should call ajax with the correct bid url', () => { - let params = { - bids: [ - { - sizes: [[300, 250], [300, 600]], - params: { - delDomain: 'testdelDomain', - unit: 1234 - } + it('should send out custom floors on bids that have customFloors specified', () => { + let bidRequest = Object.assign({}, + bidRequests[0], + { + params: { + 'unit': '12345678', + 'delDomain': 'test-del-domain', + 'customFloor': 1.5 } - ] - }; - adapter.callBids(params); - sinon.assert.calledOnce(spyAjax); + } + ); - let bidUrl = spyAjax.getCall(0).args[0]; - expect(bidUrl).to.include('testdelDomain'); - expect(bidUrl).to.include('1234'); - expect(bidUrl).to.include('300x250,300x600'); + const request = spec.buildRequests([bidRequest]); + const dataParams = request.data; + + expect(dataParams.aumfs).to.exist; + expect(dataParams.aumfs).to.equal('1500'); }); - it('should send out custom params on bids that have customParams specified', () => { - let params = { - bids: [ - { - sizes: [[300, 250], [300, 600]], - params: { - delDomain: 'testdelDomain', - unit: 1234, - customParams: {'test1': 'testval1+', 'test2': ['testval2/', 'testval3']} - } + it('should send out custom bc parameter, if override is present', () => { + let bidRequest = Object.assign({}, + bidRequests[0], + { + params: { + 'unit': '12345678', + 'delDomain': 'test-del-domain', + 'bc': 'hb_override' } - ] - }; - adapter.callBids(params); + } + ); - sinon.assert.calledOnce(spyAjax); - sinon.assert.calledWith(spyBtoa, 'test1=testval1.&test2=testval2_,testval3'); - let bidUrl = spyAjax.getCall(0).args[0]; - expect(bidUrl).to.include('testdelDomain'); - expect(bidUrl).to.include('1234'); - expect(bidUrl).to.include('300x250,300x600'); + const request = spec.buildRequests([bidRequest]); + const dataParams = request.data; + + expect(dataParams.bc).to.exist; + expect(dataParams.bc).to.equal('hb_override'); }); + }); - it('should send out custom floors on bids that have customFloors specified', () => { - let params = { - bids: [ - { - sizes: [[300, 250], [300, 600]], - params: { - delDomain: 'testdelDomain', - unit: 1234, - customFloor: 1 - } - }, - { - sizes: [[320, 50]], - params: { - delDomain: 'testdelDomain', - unit: 1234 - } - }, + describe('interpretResponse', () => { + let bids = [{ + 'bidder': 'openx', + 'params': { + 'unit': '12345678', + 'delDomain': 'test-del-domain' + }, + 'adUnitCode': 'adunit-code', + 'sizes': [[300, 250], [300, 600]], + 'bidId': '30b31c1838de1e', + 'bidderRequestId': '22edbae2733bf6', + 'auctionId': '1d1a030790a475', + }]; + let bidRequest = { + method: 'GET', + url: 'url', + data: {}, + payload: {'bids': bids, 'startTime': new Date()} + }; + let bidResponse = { + 'ads': + { + 'version': 1, + 'count': 1, + 'pixels': 'http://testpixels.net', + 'ad': [ { - sizes: [[728, 90]], - params: { - delDomain: 'testdelDomain', - unit: 1234, - customFloor: 1.5 - } - } - ] - }; - adapter.callBids(params); + 'adunitid': 12345678, + 'adid': 5678, + 'type': 'html', + 'html': 'test_html', + 'framed': 1, + 'is_fallback': 0, + 'ts': 'ts', + 'cpipc': 1000, + 'pub_rev': '1000', + 'adv_id': 'adv_id', + 'brand_id': '', + 'creative': [ + { + 'width': '300', + 'height': '250', + 'target': '_blank', + 'mime': 'text/html', + 'media': 'test_media', + 'tracking': { + 'impression': 'test_impression', + 'inview': 'test_inview', + 'click': 'test_click' + } + } + ] + }] + } + }; + it('should return correct bid response', () => { + let expectedResponse = [ + { + 'requestId': '30b31c1838de1e', + 'bidderCode': 'openx', + 'cpm': 1, + 'width': '300', + 'height': '250', + 'creativeId': 5678, + 'ad': 'test_html', + 'ttl': 300, + 'netRevenue': true, + 'currency': 'USD', + 'ts': 'ts' + } + ]; - sinon.assert.calledOnce(spyAjax); - let bidUrl = spyAjax.getCall(0).args[0]; - expect(bidUrl).to.include('testdelDomain'); - expect(bidUrl).to.include('1234'); - expect(bidUrl).to.include('300x250,300x600|320x50|728x90'); - expect(bidUrl).to.include('aumfs=1000%2C0%2C1500'); + let result = spec.interpretResponse(bidResponse, bidRequest); + expect(Object.keys(result[0])).to.eql(Object.keys(expectedResponse[0])); }); - it('should change bc param if configureable bc is specified', () => { - let params = { - bids: [ - { - sizes: [[300, 250], [300, 600]], - params: { - delDomain: 'testdelDomain', - unit: 1234, - bc: 'hb_pb_test' - } - }, - { - sizes: [[320, 50]], - params: { - delDomain: 'testdelDomain', - unit: 1234, - bc: 'hb_pb_test' - } - }, - { - sizes: [[728, 90]], - params: { - delDomain: 'testdelDomain', - unit: 1234, - bc: 'hb_pb_test' - } - } - ] + it('handles nobid responses', () => { + bidResponse = { + 'ads': + { + 'version': 1, + 'count': 1, + 'pixels': 'http://testpixels.net', + 'ad': [] + } }; - adapter.callBids(params); - sinon.assert.calledOnce(spyAjax); - let bidUrl = spyAjax.getCall(0).args[0]; - expect(bidUrl).to.include('testdelDomain'); - expect(bidUrl).to.include('1234'); - expect(bidUrl).to.include('300x250,300x600|320x50|728x90'); - expect(bidUrl).to.include('bc=hb_pb_test'); + let result = spec.interpretResponse(bidResponse, bidRequest); + expect(result.length).to.equal(0); }); }); });