From c82ceb45cfd4878e819bf9995ff0d4e4276377a9 Mon Sep 17 00:00:00 2001 From: protonate Date: Fri, 29 Jul 2016 10:26:32 -0700 Subject: [PATCH 1/9] set bid response id to sovrn impid --- src/adapters/sovrn.js | 2 +- src/bidfactory.js | 8 ++++---- src/prebid.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/adapters/sovrn.js b/src/adapters/sovrn.js index 4c4b7cdc1da..843fe31f273 100644 --- a/src/adapters/sovrn.js +++ b/src/adapters/sovrn.js @@ -119,7 +119,7 @@ var SovrnAdapter = function SovrnAdapter() { //store bid response //bid status is good (indicating 1) - bid = bidfactory.createBid(1); + bid = bidfactory.createBid(1, bidObj); bid.creative_id = sovrnBid.id; bid.bidderCode = 'sovrn'; bid.cpm = responseCPM; diff --git a/src/bidfactory.js b/src/bidfactory.js index d0ef73209de..0b43a9b7f4f 100644 --- a/src/bidfactory.js +++ b/src/bidfactory.js @@ -14,8 +14,8 @@ var utils = require('./utils.js'); dealId, priceKeyString; */ -function Bid(statusCode) { - var _bidId = utils.getUniqueIdentifierStr(); +function Bid(statusCode, bidRequest) { + var _bidId = bidRequest && bidRequest.bidId || utils.getUniqueIdentifierStr(); var _statusCode = statusCode || 0; this.bidderCode = ''; @@ -49,6 +49,6 @@ function Bid(statusCode) { } // Bid factory function. -exports.createBid = function (statusCode) { - return new Bid(statusCode); +exports.createBid = function () { + return new Bid(...arguments); }; diff --git a/src/prebid.js b/src/prebid.js index dd1ca5ef914..67d323d8f16 100644 --- a/src/prebid.js +++ b/src/prebid.js @@ -375,7 +375,7 @@ $$PREBID_GLOBAL$$.setTargetingForGPTAsync = function () { utils.logError('window.googletag is not defined on the page'); return; } - + //first reset any old targeting getPresetTargeting(); resetPresetTargeting(); From 1eb9f946df60e2a7fad8156f7343056117adb75e Mon Sep 17 00:00:00 2001 From: protonate Date: Tue, 2 Aug 2016 15:20:31 -0700 Subject: [PATCH 2/9] no need to match on "best effort" if `bid.adId` is set --- src/bidmanager.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bidmanager.js b/src/bidmanager.js index a70c58020a2..0f2a28b6ce1 100644 --- a/src/bidmanager.js +++ b/src/bidmanager.js @@ -70,10 +70,13 @@ function getBidSetForBidder(bidder) { exports.addBidResponse = function (adUnitCode, bid) { if (bid) { //first lookup bid request and assign it back the bidId if it matches the adUnitCode - let bidRequest = getBidSetForBidder(bid.bidderCode).bids.find(bidRequest => bidRequest.placementCode === adUnitCode); - if(bidRequest && bidRequest.bidId) { - bid.adId = bidRequest.bidId; + if (!bid.adId) { + let bidRequest = getBidSetForBidder(bid.bidderCode).bids.find(bidRequest => bidRequest.placementCode === adUnitCode); + if (bidRequest && bidRequest.bidId) { + bid.adId = bidRequest.bidId; + } } + Object.assign(bid, { requestId: getBidSetForBidder(bid.bidderCode).requestId, responseTimestamp: timestamp(), From 8d26f91d28b067e11080c2c58c7dbfc30bb10c16 Mon Sep 17 00:00:00 2001 From: protonate Date: Thu, 4 Aug 2016 09:33:39 -0700 Subject: [PATCH 3/9] Sovrn adapter creates bid response with ID of bid request --- src/adapters/sovrn.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/adapters/sovrn.js b/src/adapters/sovrn.js index 843fe31f273..2e417473386 100644 --- a/src/adapters/sovrn.js +++ b/src/adapters/sovrn.js @@ -78,7 +78,7 @@ var SovrnAdapter = function SovrnAdapter() { missing.forEach(function (bidRequest) { // Add a no-bid response for this bid request. var bid = {}; - bid = bidfactory.createBid(2); + bid = bidfactory.createBid(2, bidRequest); bid.bidderCode = 'sovrn'; bidmanager.addBidResponse(bidRequest.placementCode, bid); }); From e59858d983f31c7053d7c592fcd8008a81e9edf9 Mon Sep 17 00:00:00 2001 From: protonate Date: Thu, 4 Aug 2016 09:34:10 -0700 Subject: [PATCH 4/9] whitespace fix --- src/bidmanager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bidmanager.js b/src/bidmanager.js index 0f2a28b6ce1..f1ff57f35fb 100644 --- a/src/bidmanager.js +++ b/src/bidmanager.js @@ -155,7 +155,7 @@ function getKeyValueTargetingPairs(bidderCode, custBidObj) { val: function (bidResponse) { if (_granularity === CONSTANTS.GRANULARITY_OPTIONS.AUTO) { return bidResponse.pbAg; - } else if (_granularity === CONSTANTS.GRANULARITY_OPTIONS.DENSE) { + } else if (_granularity === CONSTANTS.GRANULARITY_OPTIONS.DENSE) { return bidResponse.pbDg; } else if (_granularity === CONSTANTS.GRANULARITY_OPTIONS.LOW) { return bidResponse.pbLg; From d082eaf3da1b767231329de0147320e08527d2e8 Mon Sep 17 00:00:00 2001 From: protonate Date: Fri, 5 Aug 2016 14:59:23 -0700 Subject: [PATCH 5/9] code style fixes --- test/spec/unit/pbjs_api_spec.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/spec/unit/pbjs_api_spec.js b/test/spec/unit/pbjs_api_spec.js index b790e91daaf..7ae54884f4e 100644 --- a/test/spec/unit/pbjs_api_spec.js +++ b/test/spec/unit/pbjs_api_spec.js @@ -24,10 +24,10 @@ $$PREBID_GLOBAL$$._bidsRequested = getBidRequests(); $$PREBID_GLOBAL$$._bidsReceived = getBidResponses(); function resetAuction() { - $$PREBID_GLOBAL$$._sendAllBids = false; - $$PREBID_GLOBAL$$.clearAuction(); - $$PREBID_GLOBAL$$._bidsRequested = getBidRequests(); - $$PREBID_GLOBAL$$._bidsReceived = getBidResponses(); + $$PREBID_GLOBAL$$._sendAllBids = false; + $$PREBID_GLOBAL$$.clearAuction(); + $$PREBID_GLOBAL$$._bidsRequested = getBidRequests(); + $$PREBID_GLOBAL$$._bidsReceived = getBidResponses(); } var Slot = function Slot(elementId, pathId) { @@ -43,7 +43,7 @@ var Slot = function Slot(elementId, pathId) { setTargeting: function setTargeting(key, value) { var obj = []; - obj[key] = value; + obj[key] = value; this.targeting.push(obj); }, @@ -140,7 +140,7 @@ describe('Unit: Prebid Module', function () { }); it('should return current targeting data for slots', function () { - $$PREBID_GLOBAL$$.enableSendAllBids(); + $$PREBID_GLOBAL$$.enableSendAllBids(); const targeting = $$PREBID_GLOBAL$$.getAdserverTargeting(); const expected = getAdServerTargeting(); assert.deepEqual(targeting, expected, 'targeting ok'); @@ -260,9 +260,9 @@ describe('Unit: Prebid Module', function () { var slots = createSlotArrayScenario2(); window.googletag.pubads().setSlots(slots); $$PREBID_GLOBAL$$.setTargetingForGPTAsync(config.adUnitCodes); - + var targeting = []; - slots[1].getTargeting().map(function(value) { + slots[1].getTargeting().map(function(value) { var temp = []; temp.push(Object.keys(value).toString()); temp.push(value[Object.keys(value)]); From 3ecea76e2cae34592378f34eebc7c8305f6b434d Mon Sep 17 00:00:00 2001 From: protonate Date: Fri, 5 Aug 2016 15:00:23 -0700 Subject: [PATCH 6/9] use sinon fake server request and response --- test/spec/adapters/sovrn_spec.js | 165 +++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/test/spec/adapters/sovrn_spec.js b/test/spec/adapters/sovrn_spec.js index 6c89b03f933..15fc4d23390 100644 --- a/test/spec/adapters/sovrn_spec.js +++ b/test/spec/adapters/sovrn_spec.js @@ -169,5 +169,170 @@ describe('sovrn adapter tests', function () { stubAddBidResponse.restore(); }); }); + + //describe('Sovrn bid response has same ID as request', () => { + // + // let xhr; + // let requests; + // let server; + // + // beforeEach(() => { + // server = sinon.fakeServer.create(); + // sinon.stub(bidmanager, 'addBidResponse'); + // xhr = sinon.useFakeXMLHttpRequest(); + // requests = []; + // xhr.onCreate = xhr => requests.push(xhr); + // }); + // + // afterEach(() => { + // xhr.restore(); + // server.restore(); + // bidmanager.addBidResponse.restore(); + // }); + // + // it('should create a bidResponse with same ID as bidRequest', (done) => { + // + // server.respondWith(`window.pbjs.sovrnResponse({ + // "id" : "2738918e3c54fa7", + // "seatbid" : [ { + // "bid" : [ { + // "id" : "a_315045_5b5792b27d0e42ccbe7801644753250e", + // "impid" : "256ccac965746f", + // "price" : 0.01, + // "nurl" : "http://vap1sjc1.lijit.com/www/delivery/lg.php?bannerid=124612&campaignid=3313&zoneid=315045&cb=90062211&tid=a_315045_5b5792b27d0e42ccbe7801644753250e", + // "adm" : "%3Ca%20href%3D%22http%3A%2F%2Fvapden1.lijit.com%2Fwww%2Fdelivery%2Fck.php%3Foaparams%3D2__bannerid%3D124612__campaignid%3D3313__zoneid%3D315045__cb%3Dc06f6e8e__tid%3Da_315045_5b5792b27d0e42ccbe7801644753250e__maxdest%3D%22%3E%0A%3Cimg%20src%3D%22http%3A%2F%2Fap.lijit.com%2Fwww%2Fimages%2Fsovrn-house-banner2-1.gif%22%20border%3D%220%22%20width%3D%22300%22%20height%3D%22250%22%3E%0A%3C%2Fa%3E", + // "h" : 250, + // "w" : 300, + // "ext" : { } + // }, { + // "id" : "a_381972_c8749f371a674f9f9763e7e03c90b2bd", + // "impid" : "266a58c0351705c", + // "price" : 15.0, + // "nurl" : "http://vap1sjc1.lijit.com/www/delivery/lg.php?bannerid=135907&campaignid=3325&zoneid=381972&cb=21077374&tid=a_381972_c8749f371a674f9f9763e7e03c90b2bd", + // "adm" : "%3Cimg%20src%3D%22http%3A%2F%2Fplacehold.it%2F300x600%22%3E", + // "h" : 600, + // "w" : 300, + // "ext" : { } + // } ] + // } ] + // })`); + // + // $$PREBID_GLOBAL$$.requestBids({ + // bidsBackHandler: () => {}, + // timeout: 2000, + // adUnits: sovrnAdUnits + // }); + // + // server.respond(); + // + // sinon.assert.calledTwice(bidmanager.addBidResponse); + // + // const response = bidmanager.addBidResponse.firstCall.args[1]; + // expect(response).to.have.property('statusMessage', 'Bid available'); + // expect(response).to.have.property('cpm', 0.5); + // + // done(); + // }); + //}); + + /** + * TESTING + */ + describe('request function', () => { + + let xhr; + let requests; + + beforeEach(() => { + xhr = sinon.useFakeXMLHttpRequest(); + requests = []; + xhr.onCreate = request => requests.push(request); + }); + + afterEach(() => xhr.restore()); + + it('exists and is a function', () => { + expect($$PREBID_GLOBAL$$.requestBids).to.exist.and.to.be.a('function'); + }); + + it('sends bid request to ENDPOINT via GET', () => { + $$PREBID_GLOBAL$$.requestBids({ + bidsBackHandler: () => {}, + timeout: 2000, + adUnits: sovrnAdUnits + }); + //expect(requests[0].url).to.equal(ENDPOINT); + expect(requests[0].method).to.equal('GET'); + }); + + }); + + describe('response handler', () => { + + let server; + + beforeEach(() => { + server = sinon.fakeServer.create(); + sinon.stub(bidmanager, 'addBidResponse'); + }); + + afterEach(() => { + server.restore(); + bidmanager.addBidResponse.restore(); + }); + + it('registers bids', () => { + server.respondWith('test'); + + $$PREBID_GLOBAL$$.requestBids({ + bidsBackHandler: () => {}, + timeout: 2000, + adUnits: sovrnAdUnits + }); + server.respond(); + sinon.assert.calledTwice(bidmanager.addBidResponse); + + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response).to.have.property('statusMessage', 'Bid available'); + expect(response).to.have.property('cpm', 0.5); + }); + + it('handles blank bids', () => { + server.respondWith('test'); + + $$PREBID_GLOBAL$$.requestBids({ + bidsBackHandler: () => {}, + timeout: 2000, + adUnits: sovrnAdUnits + }); + server.respond(); + sinon.assert.calledOnce(bidmanager.addBidResponse); + + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response).to.have.property('statusMessage', + 'Bid returned empty or error response'); + }); + + it('handles nobid responses', () => { + server.respondWith('test'); + + $$PREBID_GLOBAL$$.requestBids({ + bidsBackHandler: () => {}, + timeout: 2000, + adUnits: sovrnAdUnits + }); + server.respond(); + sinon.assert.calledOnce(bidmanager.addBidResponse); + + const response = bidmanager.addBidResponse.firstCall.args[1]; + expect(response).to.have.property( + 'statusMessage', + 'Bid returned empty or error response' + ); + }); + }); + /** + * END TESTING + */ }); From 7c821ed1dcc9bfb9c9ff44c4734fdc47d85e464f Mon Sep 17 00:00:00 2001 From: protonate Date: Sun, 7 Aug 2016 19:45:07 -0700 Subject: [PATCH 7/9] test bid object and props exist --- src/bidmanager.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bidmanager.js b/src/bidmanager.js index f1ff57f35fb..ee2f5c03de7 100644 --- a/src/bidmanager.js +++ b/src/bidmanager.js @@ -116,7 +116,7 @@ exports.addBidResponse = function (adUnitCode, bid) { $$PREBID_GLOBAL$$._bidsReceived.push(bid); } - if (bidsBackAdUnit(bid.adUnitCode)) { + if (bid && bid.adUnitCode && bidsBackAdUnit(bid.adUnitCode)) { triggerAdUnitCallbacks(bid.adUnitCode); } @@ -124,7 +124,7 @@ exports.addBidResponse = function (adUnitCode, bid) { this.executeCallback(); } - if (bid.timeToRespond > $$PREBID_GLOBAL$$.bidderTimeout) { + if (bid && bid.timeToRespond > $$PREBID_GLOBAL$$.bidderTimeout) { events.emit(CONSTANTS.EVENTS.BID_TIMEOUT, this.getTimedOutBidders()); this.executeCallback(); From 62ac5eb934b6814b76b925cdc0133c26a3fea079 Mon Sep 17 00:00:00 2001 From: protonate Date: Sun, 7 Aug 2016 19:46:05 -0700 Subject: [PATCH 8/9] troubleshoot sinon fake request --- test/spec/adapters/sovrn_spec.js | 323 +++++++++++-------------------- 1 file changed, 114 insertions(+), 209 deletions(-) diff --git a/test/spec/adapters/sovrn_spec.js b/test/spec/adapters/sovrn_spec.js index 15fc4d23390..d05c4e3cde2 100644 --- a/test/spec/adapters/sovrn_spec.js +++ b/test/spec/adapters/sovrn_spec.js @@ -3,6 +3,91 @@ describe('sovrn adapter tests', function () { const adapter = require('src/adapters/sovrn'); const bidmanager = require('src/bidmanager'); + const sovrnAdUnits = [ + { + code: 'div-gpt-ad-12345-1', + sizes: [[320, 50]], + bids: [ + { + bidder: 'sovrn', + params: { + tagid: '315045' + } + } + ] + }, + { + code: 'div-gpt-ad-12345-2', + sizes: [[320, 50]], + bids: [ + { + bidder: 'sovrn', + params: { + tagid: '315046' + } + } + ] + } + ]; + + var bidderRequest = { + bidderCode: 'sovrn', + requestId: 'd3e07445-ab06-44c8-a9dd-5ef9af06d2a6', + bidderRequestId: '7101db09af0db2', + bids: [ + { + bidId: 'bidId1', + bidder: 'sovrn', + params: { + tagid: '315045' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-1' + }, + { + bidId: 'bidId2', + bidder: 'sovrn', + params: { + tagid: '315046' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-2' + }, + { + bidId: 'bidId3', + bidder: 'sovrn', + params: { + tagid: '315047' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-2' + } + ], + start: 1469479810130, + timeout: 3000 + }; + + // Returning a single bid in the response. + var singleResponse = { + 'id': '54321111', + 'seatbid': [ + { + 'bid': [ + { + 'id': '1111111', + 'impid': 'bidId2', + 'price': 0.09, + 'nurl': 'http://url', + 'adm': 'ad-code', + 'h': 250, + 'w': 300, + 'ext': {} + } + ] + } + ] + }; + describe('sovrnResponse', function () { it('should exist and be a function', function () { @@ -12,39 +97,6 @@ describe('sovrn adapter tests', function () { it('should add empty bid responses if no bids returned', function () { var stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse'); - var bidderRequest = { - bidderCode: 'sovrn', - bids: [ - { - bidId: 'bidId1', - bidder: 'sovrn', - params: { - tagid: '315045' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-1' - }, - { - bidId: 'bidId2', - bidder: 'sovrn', - params: { - tagid: '315046' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-2' - }, - { - bidId: 'bidId3', - bidder: 'sovrn', - params: { - tagid: '315047' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-2' - }, - ] - }; - // no bids returned in the response. var response = { "id": "54321", @@ -53,7 +105,7 @@ describe('sovrn adapter tests', function () { pbjs._bidsRequested.push(bidderRequest); // adapter needs to be called, in order for the stub to register. - adapter() + adapter(); pbjs.sovrnResponse(response); @@ -84,61 +136,10 @@ describe('sovrn adapter tests', function () { it('should add a bid response for bids returned and empty bid responses for the rest', function () { var stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse'); - var bidderRequest = { - bidderCode: 'sovrn', - bids: [ - { - bidId: 'bidId1', - bidder: 'sovrn', - params: { - tagid: '315045' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-1' - }, - { - bidId: 'bidId2', - bidder: 'sovrn', - params: { - tagid: '315046' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-2' - }, - { - bidId: 'bidId3', - bidder: 'sovrn', - params: { - tagid: '315047' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-2' - }, - ] - }; - - // Returning a single bid in the response. - var response = { - "id": "54321111", - "seatbid": [ { - "bid" : [ { - "id" : "1111111", - "impid" : "bidId2", - "price" : 0.09, - "nurl" : "http://url", - "adm" : "ad-code", - "h" : 250, - "w" : 300, - "ext" : { } - } ] - } ] - }; - - pbjs._bidsRequested.push(bidderRequest); // adapter needs to be called, in order for the stub to register. adapter() - pbjs.sovrnResponse(response); + pbjs.sovrnResponse(singleResponse); var bidPlacementCode1 = stubAddBidResponse.getCall(0).args[0]; var bidObject1 = stubAddBidResponse.getCall(0).args[1]; @@ -170,98 +171,38 @@ describe('sovrn adapter tests', function () { }); }); - //describe('Sovrn bid response has same ID as request', () => { - // - // let xhr; - // let requests; - // let server; - // - // beforeEach(() => { - // server = sinon.fakeServer.create(); - // sinon.stub(bidmanager, 'addBidResponse'); - // xhr = sinon.useFakeXMLHttpRequest(); - // requests = []; - // xhr.onCreate = xhr => requests.push(xhr); - // }); - // - // afterEach(() => { - // xhr.restore(); - // server.restore(); - // bidmanager.addBidResponse.restore(); - // }); - // - // it('should create a bidResponse with same ID as bidRequest', (done) => { - // - // server.respondWith(`window.pbjs.sovrnResponse({ - // "id" : "2738918e3c54fa7", - // "seatbid" : [ { - // "bid" : [ { - // "id" : "a_315045_5b5792b27d0e42ccbe7801644753250e", - // "impid" : "256ccac965746f", - // "price" : 0.01, - // "nurl" : "http://vap1sjc1.lijit.com/www/delivery/lg.php?bannerid=124612&campaignid=3313&zoneid=315045&cb=90062211&tid=a_315045_5b5792b27d0e42ccbe7801644753250e", - // "adm" : "%3Ca%20href%3D%22http%3A%2F%2Fvapden1.lijit.com%2Fwww%2Fdelivery%2Fck.php%3Foaparams%3D2__bannerid%3D124612__campaignid%3D3313__zoneid%3D315045__cb%3Dc06f6e8e__tid%3Da_315045_5b5792b27d0e42ccbe7801644753250e__maxdest%3D%22%3E%0A%3Cimg%20src%3D%22http%3A%2F%2Fap.lijit.com%2Fwww%2Fimages%2Fsovrn-house-banner2-1.gif%22%20border%3D%220%22%20width%3D%22300%22%20height%3D%22250%22%3E%0A%3C%2Fa%3E", - // "h" : 250, - // "w" : 300, - // "ext" : { } - // }, { - // "id" : "a_381972_c8749f371a674f9f9763e7e03c90b2bd", - // "impid" : "266a58c0351705c", - // "price" : 15.0, - // "nurl" : "http://vap1sjc1.lijit.com/www/delivery/lg.php?bannerid=135907&campaignid=3325&zoneid=381972&cb=21077374&tid=a_381972_c8749f371a674f9f9763e7e03c90b2bd", - // "adm" : "%3Cimg%20src%3D%22http%3A%2F%2Fplacehold.it%2F300x600%22%3E", - // "h" : 600, - // "w" : 300, - // "ext" : { } - // } ] - // } ] - // })`); - // - // $$PREBID_GLOBAL$$.requestBids({ - // bidsBackHandler: () => {}, - // timeout: 2000, - // adUnits: sovrnAdUnits - // }); - // - // server.respond(); - // - // sinon.assert.calledTwice(bidmanager.addBidResponse); - // - // const response = bidmanager.addBidResponse.firstCall.args[1]; - // expect(response).to.have.property('statusMessage', 'Bid available'); - // expect(response).to.have.property('cpm', 0.5); - // - // done(); - // }); - //}); - - /** - * TESTING - */ describe('request function', () => { let xhr; let requests; + let sovrnAdapter; beforeEach(() => { - xhr = sinon.useFakeXMLHttpRequest(); - requests = []; - xhr.onCreate = request => requests.push(request); + this.xhr = sinon.useFakeXMLHttpRequest(); + requests = this.requests = []; + + this.xhr.onCreate = request => { + requests.push(request); + }; + + pbjs.adUnitsBackup = pbjs.adUnits; + pbjs.adUnits = sovrnAdUnits; + + // adapter needs to be called, in order for the stub to register. + sovrnAdapter = adapter(); }); - afterEach(() => xhr.restore()); + afterEach(() => this.xhr.restore()); + + pbjs.adUnits = pbjs.adUnitsBackup; it('exists and is a function', () => { - expect($$PREBID_GLOBAL$$.requestBids).to.exist.and.to.be.a('function'); + expect(adapter().callBids).to.exist.and.to.be.a('function'); }); it('sends bid request to ENDPOINT via GET', () => { - $$PREBID_GLOBAL$$.requestBids({ - bidsBackHandler: () => {}, - timeout: 2000, - adUnits: sovrnAdUnits - }); - //expect(requests[0].url).to.equal(ENDPOINT); + sovrnAdapter.callBids(bidderRequest); + expect(requests[0].url).to.equal(ENDPOINT); expect(requests[0].method).to.equal('GET'); }); @@ -270,69 +211,33 @@ describe('sovrn adapter tests', function () { describe('response handler', () => { let server; + let sovrnAdapter; + let stubAddBidResponse; beforeEach(() => { + stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse'); server = sinon.fakeServer.create(); - sinon.stub(bidmanager, 'addBidResponse'); + sovrnAdapter = adapter(); + pbjs._bidsRequested.push(bidderRequest); }); afterEach(() => { server.restore(); bidmanager.addBidResponse.restore(); + sovrn = undefined; }); it('registers bids', () => { server.respondWith('test'); - $$PREBID_GLOBAL$$.requestBids({ - bidsBackHandler: () => {}, - timeout: 2000, - adUnits: sovrnAdUnits - }); + sovrnAdapter.callBids(bidderRequest); server.respond(); sinon.assert.calledTwice(bidmanager.addBidResponse); const response = bidmanager.addBidResponse.firstCall.args[1]; expect(response).to.have.property('statusMessage', 'Bid available'); expect(response).to.have.property('cpm', 0.5); - }); - - it('handles blank bids', () => { - server.respondWith('test'); - - $$PREBID_GLOBAL$$.requestBids({ - bidsBackHandler: () => {}, - timeout: 2000, - adUnits: sovrnAdUnits - }); - server.respond(); - sinon.assert.calledOnce(bidmanager.addBidResponse); - - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response).to.have.property('statusMessage', - 'Bid returned empty or error response'); - }); - - it('handles nobid responses', () => { - server.respondWith('test'); - - $$PREBID_GLOBAL$$.requestBids({ - bidsBackHandler: () => {}, - timeout: 2000, - adUnits: sovrnAdUnits - }); - server.respond(); - sinon.assert.calledOnce(bidmanager.addBidResponse); - - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response).to.have.property( - 'statusMessage', - 'Bid returned empty or error response' - ); + pbjs.adUnits = pbjs.adUnitsBackup; }); }); - /** - * END TESTING - */ }); - From 83a8844a0f821046d0b3e1d6d334fbad71554a83 Mon Sep 17 00:00:00 2001 From: protonate Date: Mon, 8 Aug 2016 11:16:23 -0700 Subject: [PATCH 9/9] remove sinon fake request/response approach and just test the bid id from sovrnResponse --- test/spec/adapters/sovrn_spec.js | 245 +++++++++++-------------------- 1 file changed, 88 insertions(+), 157 deletions(-) diff --git a/test/spec/adapters/sovrn_spec.js b/test/spec/adapters/sovrn_spec.js index d05c4e3cde2..2ffed6f6774 100644 --- a/test/spec/adapters/sovrn_spec.js +++ b/test/spec/adapters/sovrn_spec.js @@ -3,91 +3,6 @@ describe('sovrn adapter tests', function () { const adapter = require('src/adapters/sovrn'); const bidmanager = require('src/bidmanager'); - const sovrnAdUnits = [ - { - code: 'div-gpt-ad-12345-1', - sizes: [[320, 50]], - bids: [ - { - bidder: 'sovrn', - params: { - tagid: '315045' - } - } - ] - }, - { - code: 'div-gpt-ad-12345-2', - sizes: [[320, 50]], - bids: [ - { - bidder: 'sovrn', - params: { - tagid: '315046' - } - } - ] - } - ]; - - var bidderRequest = { - bidderCode: 'sovrn', - requestId: 'd3e07445-ab06-44c8-a9dd-5ef9af06d2a6', - bidderRequestId: '7101db09af0db2', - bids: [ - { - bidId: 'bidId1', - bidder: 'sovrn', - params: { - tagid: '315045' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-1' - }, - { - bidId: 'bidId2', - bidder: 'sovrn', - params: { - tagid: '315046' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-2' - }, - { - bidId: 'bidId3', - bidder: 'sovrn', - params: { - tagid: '315047' - }, - sizes: [[320, 50]], - placementCode: 'div-gpt-ad-12345-2' - } - ], - start: 1469479810130, - timeout: 3000 - }; - - // Returning a single bid in the response. - var singleResponse = { - 'id': '54321111', - 'seatbid': [ - { - 'bid': [ - { - 'id': '1111111', - 'impid': 'bidId2', - 'price': 0.09, - 'nurl': 'http://url', - 'adm': 'ad-code', - 'h': 250, - 'w': 300, - 'ext': {} - } - ] - } - ] - }; - describe('sovrnResponse', function () { it('should exist and be a function', function () { @@ -97,6 +12,39 @@ describe('sovrn adapter tests', function () { it('should add empty bid responses if no bids returned', function () { var stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse'); + var bidderRequest = { + bidderCode: 'sovrn', + bids: [ + { + bidId: 'bidId1', + bidder: 'sovrn', + params: { + tagid: '315045' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-1' + }, + { + bidId: 'bidId2', + bidder: 'sovrn', + params: { + tagid: '315046' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-2' + }, + { + bidId: 'bidId3', + bidder: 'sovrn', + params: { + tagid: '315047' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-2' + }, + ] + }; + // no bids returned in the response. var response = { "id": "54321", @@ -105,7 +53,7 @@ describe('sovrn adapter tests', function () { pbjs._bidsRequested.push(bidderRequest); // adapter needs to be called, in order for the stub to register. - adapter(); + adapter() pbjs.sovrnResponse(response); @@ -136,10 +84,61 @@ describe('sovrn adapter tests', function () { it('should add a bid response for bids returned and empty bid responses for the rest', function () { var stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse'); + var bidderRequest = { + bidderCode: 'sovrn', + bids: [ + { + bidId: 'bidId1', + bidder: 'sovrn', + params: { + tagid: '315045' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-1' + }, + { + bidId: 'bidId2', + bidder: 'sovrn', + params: { + tagid: '315046' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-2' + }, + { + bidId: 'bidId3', + bidder: 'sovrn', + params: { + tagid: '315047' + }, + sizes: [[320, 50]], + placementCode: 'div-gpt-ad-12345-2' + }, + ] + }; + + // Returning a single bid in the response. + var response = { + "id": "54321111", + "seatbid": [ { + "bid" : [ { + "id" : "1111111", + "impid" : "bidId2", + "price" : 0.09, + "nurl" : "http://url", + "adm" : "ad-code", + "h" : 250, + "w" : 300, + "ext" : { } + } ] + } ] + }; + + pbjs._bidsRequested.push(bidderRequest); // adapter needs to be called, in order for the stub to register. adapter() - pbjs.sovrnResponse(singleResponse); + pbjs.sovrnResponse(response); var bidPlacementCode1 = stubAddBidResponse.getCall(0).args[0]; var bidObject1 = stubAddBidResponse.getCall(0).args[1]; @@ -156,6 +155,7 @@ describe('sovrn adapter tests', function () { expect(bidObject1.height).to.equal(250); expect(bidObject1.width).to.equal(300); expect(bidObject1.ad).to.equal('ad-code'); + expect(bidObject1.adId).to.equal('bidId2'); expect(bidPlacementCode2).to.equal('div-gpt-ad-12345-1'); expect(bidObject2.getStatusCode()).to.equal(2); @@ -170,74 +170,5 @@ describe('sovrn adapter tests', function () { stubAddBidResponse.restore(); }); }); - - describe('request function', () => { - - let xhr; - let requests; - let sovrnAdapter; - - beforeEach(() => { - this.xhr = sinon.useFakeXMLHttpRequest(); - requests = this.requests = []; - - this.xhr.onCreate = request => { - requests.push(request); - }; - - pbjs.adUnitsBackup = pbjs.adUnits; - pbjs.adUnits = sovrnAdUnits; - - // adapter needs to be called, in order for the stub to register. - sovrnAdapter = adapter(); - }); - - afterEach(() => this.xhr.restore()); - - pbjs.adUnits = pbjs.adUnitsBackup; - - it('exists and is a function', () => { - expect(adapter().callBids).to.exist.and.to.be.a('function'); - }); - - it('sends bid request to ENDPOINT via GET', () => { - sovrnAdapter.callBids(bidderRequest); - expect(requests[0].url).to.equal(ENDPOINT); - expect(requests[0].method).to.equal('GET'); - }); - - }); - - describe('response handler', () => { - - let server; - let sovrnAdapter; - let stubAddBidResponse; - - beforeEach(() => { - stubAddBidResponse = sinon.stub(bidmanager, 'addBidResponse'); - server = sinon.fakeServer.create(); - sovrnAdapter = adapter(); - pbjs._bidsRequested.push(bidderRequest); - }); - - afterEach(() => { - server.restore(); - bidmanager.addBidResponse.restore(); - sovrn = undefined; - }); - - it('registers bids', () => { - server.respondWith('test'); - - sovrnAdapter.callBids(bidderRequest); - server.respond(); - sinon.assert.calledTwice(bidmanager.addBidResponse); - - const response = bidmanager.addBidResponse.firstCall.args[1]; - expect(response).to.have.property('statusMessage', 'Bid available'); - expect(response).to.have.property('cpm', 0.5); - pbjs.adUnits = pbjs.adUnitsBackup; - }); - }); }); +