Skip to content

Commit

Permalink
Sharethrough Adapter - set bidderCode on invalid bid
Browse files Browse the repository at this point in the history
  • Loading branch information
Johnny Chau committed Aug 18, 2017
1 parent 000e453 commit ed6ba40
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
15 changes: 15 additions & 0 deletions modules/sharethroughBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +101,7 @@ var SharethroughAdapter = function SharethroughAdapter() {

function _handleInvalidBid(bidObj) {
const bid = bidfactory.createBid(2, bidObj);
bid.bidderCode = STR_BIDDER_CODE;
bidmanager.addBidResponse(bidObj.placementCode, bid);
}

Expand Down
71 changes: 70 additions & 1 deletion test/spec/modules/sharethroughBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -117,6 +120,7 @@ describe('sharethrough adapter', () => {

afterEach(() => {
server.restore();
stubAddBidResponse.reset();
});

it('should add a bid object for each bid', () => {
Expand Down Expand Up @@ -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')
});
});
});
});

0 comments on commit ed6ba40

Please sign in to comment.