diff --git a/modules/sharethroughBidAdapter.js b/modules/sharethroughBidAdapter.js index 21d1561192a4..d53fb0d92dba 100644 --- a/modules/sharethroughBidAdapter.js +++ b/modules/sharethroughBidAdapter.js @@ -48,6 +48,20 @@ var SharethroughAdapter = function SharethroughAdapter() { function _strcallback(bidObj, bidResponse) { try { bidResponse = JSON.parse(bidResponse); + } catch (e) { + _handleInvalidBid(bidObj); + return; + } + + if (bidResponse.creatives && bidResponse.creatives.length > 0) { + _handleBid(bidObj, bidResponse); + } else { + _handleInvalidBid(bidObj); + } + } + + function _handleBid(bidObj, bidResponse) { + try { const bidId = bidResponse.bidId; const bid = bidfactory.createBid(1, bidObj); bid.bidderCode = STR_BIDDER_CODE; @@ -91,6 +105,7 @@ var SharethroughAdapter = function SharethroughAdapter() { function _handleInvalidBid(bidObj) { const bid = bidfactory.createBid(2, bidObj); + bid.bidderCode = STR_BIDDER_CODE; bidmanager.addBidResponse(bidObj.placementCode, bid); } diff --git a/test/spec/modules/sharethroughBidAdapter_spec.js b/test/spec/modules/sharethroughBidAdapter_spec.js index 070f20958bb0..5453e5941556 100644 --- a/test/spec/modules/sharethroughBidAdapter_spec.js +++ b/test/spec/modules/sharethroughBidAdapter_spec.js @@ -1,6 +1,7 @@ import { expect } from 'chai'; import Adapter from '../../../modules/sharethroughBidAdapter'; import bidManager from '../../../src/bidmanager'; +import bidfactory from '../../../src/bidfactory'; describe('sharethrough adapter', () => { let adapter; @@ -69,9 +70,11 @@ describe('sharethrough adapter', () => { let firstBid; let secondBid; let server; + let stubAddBidResponse; + let stubCreateBid; beforeEach(() => { - sandbox.stub(bidManager, 'addBidResponse'); + stubAddBidResponse = sandbox.stub(bidManager, 'addBidResponse'); server = sinon.fakeServer.create(); $$PREBID_GLOBAL$$._bidsRequested.push(bidderRequest); @@ -117,6 +120,7 @@ describe('sharethrough adapter', () => { afterEach(() => { server.restore(); + stubAddBidResponse.reset(); }); it('should add a bid object for each bid', () => { @@ -167,5 +171,70 @@ describe('sharethrough adapter', () => { expect(firstBid).to.have.property('pkey', 'aaaa1111'); expect(secondBid).to.have.property('pkey', 'bbbb2222'); }); + + describe('when bidResponse string cannot be JSON parsed', () => { + beforeEach(() => { + pbjs._bidsRequested.push(bidderRequest); + adapter.str.placementCodeSet['foo'] = {}; + + server.respondWith(/aaaa1111/, 'non JSON string'); + adapter.callBids(bidderRequest); + + server.respond(); + }); + + afterEach(() => { + server.restore(); + stubAddBidResponse.reset(); + }); + + it('should add a bid response', () => { + sinon.assert.called(bidManager.addBidResponse); + }); + + it('should set bidder code on invalid bid response', () => { + let bidResponse = bidManager.addBidResponse.firstCall.args[1] + expect(bidResponse).to.have.property('bidderCode', 'sharethrough') + }); + }); + + describe('when no fill', () => { + beforeEach(() => { + pbjs._bidsRequested.push(bidderRequest); + adapter.str.placementCodeSet['foo'] = {}; + + let bidderResponse1 = { + 'adserverRequestId': '40b6afd5-6134-4fbb-850a-bb8972a46994', + 'bidId': 'bidId1', + 'creatives': [ + { + 'cpm': 12.34, + 'auctionWinId': 'b2882d5e-bf8b-44da-a91c-0c11287b8051', + 'version': 1 + } + ], + 'stxUserId': '' + }; + + server.respondWith(/aaaa1111/, JSON.stringify(bidderResponse1)); + adapter.callBids(bidderRequest); + + server.respond(); + }); + + afterEach(() => { + server.restore(); + stubAddBidResponse.reset(); + }); + + it('should add a bid response', () => { + sinon.assert.called(bidManager.addBidResponse); + }); + + it('should set bidder code on invalid bid response', () => { + let bidResponse = bidManager.addBidResponse.firstCall.args[1] + expect(bidResponse).to.have.property('bidderCode', 'sharethrough') + }); + }); }); });