Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DSPX adapter for PrebidJs 3.0 #4597

Merged
merged 3 commits into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions modules/dspxBidAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as utils from '../src/utils';
import {config} from '../src/config';
import {registerBidder} from '../src/adapters/bidderFactory';

const BIDDER_CODE = 'dspx';
const ENDPOINT_URL = 'https://buyer.dspx.tv/request/';
const ENDPOINT_URL_DEV = 'https://dcbuyer.dspx.tv/request/';

export const spec = {
code: BIDDER_CODE,
aliases: ['dspx'],
isBidRequestValid: function(bid) {
return !!(bid.params.placement);
},
buildRequests: function(validBidRequests, bidderRequest) {
return validBidRequests.map(bidRequest => {
const params = bidRequest.params;
const placementId = params.placement;
const rnd = Math.floor(Math.random() * 99999999999);
const referrer = encodeURIComponent(bidderRequest.refererInfo.referer);
const bidId = bidRequest.bidId;
const isDev = params.devMode || false;

let bannerSizes = utils.parseSizesInput(utils.deepAccess(bidRequest, 'mediaTypes.banner.sizes') || bidRequest.sizes);
let [width, height] = bannerSizes[0].split('x');

let endpoint = isDev ? ENDPOINT_URL_DEV : ENDPOINT_URL;

const payload = {
_f: 'html',
alternative: 'prebid_js',
inventory_item_id: placementId,
srw: width,
srh: height,
idt: 100,
rnd: rnd,
ref: referrer,
bid_id: bidId,
};
if (params.pfilter !== undefined) {
payload.pfilter = params.pfilter;
}
if (params.bcat !== undefined) {
payload.bcat = params.bcat;
}
if (params.dvt !== undefined) {
payload.dvt = params.dvt;
}
return {
method: 'GET',
url: endpoint,
data: objectToQueryString(payload),
}
});
},
interpretResponse: function(serverResponse, bidRequest) {
const bidResponses = [];
const response = serverResponse.body;
const crid = response.crid || 0;
const cpm = response.cpm / 1000000 || 0;
if (cpm !== 0 && crid !== 0) {
const dealId = response.dealid || '';
const currency = response.currency || 'EUR';
const netRevenue = (response.netRevenue === undefined) ? true : response.netRevenue;
const bidResponse = {
requestId: response.bid_id,
cpm: cpm,
width: response.width,
height: response.height,
creativeId: crid,
dealId: dealId,
currency: currency,
netRevenue: netRevenue,
ttl: config.getConfig('_bidderTimeout'),
ad: response.adTag
};
bidResponses.push(bidResponse);
}
return bidResponses;
}
}

function objectToQueryString(obj, prefix) {
let str = [];
let p;
for (p in obj) {
if (obj.hasOwnProperty(p)) {
let k = prefix ? prefix + '[' + p + ']' : p;
let v = obj[p];
str.push((v !== null && typeof v === 'object')
? objectToQueryString(v, k)
: encodeURIComponent(k) + '=' + encodeURIComponent(v));
}
}
return str.join('&');
}

registerBidder(spec);
1 change: 1 addition & 0 deletions modules/dspxBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Dspx adapter for Prebid.js 1.0
bidder: "dspx",
params: {
placement: '101',
devMode: true // if true: library uses dev server for tests
mike-chowla marked this conversation as resolved.
Show resolved Hide resolved
pfilter: {
floorprice: 1000000, // EUR * 1,000,000
private_auction: 1, // Is private auction? 0 - no, 1 - yes
Expand Down
129 changes: 129 additions & 0 deletions test/spec/modules/dspxBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { expect } from 'chai';
import { spec } from 'modules/dspxBidAdapter';
import { newBidder } from 'src/adapters/bidderFactory';

const ENDPOINT_URL = 'https://buyer.dspx.tv/request/';

describe('dspxAdapter', function () {
const adapter = newBidder(spec);

describe('isBidRequestValid', function () {
let bid = {
'bidder': 'dspx',
'params': {
'placement': '6682',
'pfilter': {
'floorprice': 1000000
},
'bcat': 'IAB2,IAB4',
'dvt': 'desktop'
},
'sizes': [
[300, 250]
],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
'auctionId': '1d1a030790a475'
};

it('should return true when required params found', function () {
expect(spec.isBidRequestValid(bid)).to.equal(true);
});

it('should return false when required params are not passed', function () {
let bid = Object.assign({}, bid);
delete bid.params;
bid.params = {
'someIncorrectParam': 0
};
expect(spec.isBidRequestValid(bid)).to.equal(false);
});
});

describe('buildRequests', function () {
let bidRequests = [{
'bidder': 'dspx',
'params': {
'placement': '6682',
'pfilter': {
'floorprice': 1000000,
'private_auction': 0,
'geo': {
'country': 'DE'
}
},
'bcat': 'IAB2,IAB4',
'dvt': 'desktop'
},
'sizes': [
[300, 250]
],
'bidId': '30b31c1838de1e',
'bidderRequestId': '22edbae2733bf6',
'auctionId': '1d1a030790a475'
}];

let bidderRequest = {
refererInfo: {
referer: 'some_referrer.net'
}
}

const request = spec.buildRequests(bidRequests, bidderRequest);
it('sends bid request to our endpoint via GET', function () {
expect(request[0].method).to.equal('GET');
let data = request[0].data.replace(/rnd=\d+\&/g, '').replace(/ref=.*\&bid/g, 'bid');
expect(data).to.equal('_f=html&alternative=prebid_js&inventory_item_id=6682&srw=300&srh=250&idt=100&bid_id=30b31c1838de1e&pfilter%5Bfloorprice%5D=1000000&pfilter%5Bprivate_auction%5D=0&pfilter%5Bgeo%5D%5Bcountry%5D=DE&bcat=IAB2%2CIAB4&dvt=desktop');
});
});

describe('interpretResponse', function () {
let serverResponse = {
'body': {
'cpm': 5000000,
'crid': 100500,
'width': '300',
'height': '250',
'tag': '<!-- test creative -->',
'requestId': '220ed41385952a',
'currency': 'EUR',
'ttl': 60,
'netRevenue': true,
'zone': '6682'
}
};

let expectedResponse = [{
requestId: '23beaa6af6cdde',
cpm: 0.5,
width: 0,
height: 0,
creativeId: 100500,
dealId: '',
currency: 'EUR',
netRevenue: true,
ttl: 300,
ad: '<!-- test creative -->'
}];

it('should get the correct bid response by display ad', function () {
let bidRequest = [{
'method': 'GET',
'url': ENDPOINT_URL,
'data': {
'bid_id': '30b31c1838de1e'
}
}];
let result = spec.interpretResponse(serverResponse, bidRequest[0]);
expect(Object.keys(result[0])).to.have.members(Object.keys(expectedResponse[0]));
});

it('handles empty bid response', function () {
let response = {
body: {}
};
let result = spec.interpretResponse(response);
expect(result.length).to.equal(0);
});
});
});