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

Add deal id handling for Adform adapter #9

Merged
merged 6 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 24 additions & 20 deletions src/adapters/adform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,38 @@ function AdformAdapter() {
};

function _callBids(params) {
//var callbackName = '_adf_' + utils.getUniqueIdentifierStr();
var bid;
var noDomain = true;
var bid, _value, _key, i, j, k, l;
var bids = params.bids;
var request = [];
var callbackName = '_adf_' + utils.getUniqueIdentifierStr();
var globalParams = [ [ 'adxDomain', 'adx.adform.net' ], [ 'url', null ], [ 'tid', null ], [ 'callback', '$$PREBID_GLOBAL$$.' + callbackName ] ];

for (var i = 0, l = bids.length; i < l; i++) {
for (i = 0, l = bids.length; i < l; i++) {
bid = bids[i];
if (bid.adxDomain && noDomain) {
noDomain = false;
request.unshift('//' + bid.adxDomain + '/adx/?rp=4');

for (j = 0, k = globalParams.length; j < k; j++) {
_key = globalParams[j][0];
_value = bid[_key] || bid.params[_key];
if (_value) {
bid[_key] = bid.params[_key] = null;
globalParams[j][1] = _value;
}
}

request.push(formRequestUrl(bid.params));
}

if (noDomain) {
request.unshift('//adx.adform.net/adx/?rp=4');
request.unshift('//' + globalParams[0][1]+ '/adx/?rp=4');

for (i = 1, l = globalParams.length; i < l; i++) {
_key = globalParams[i][0];
_value = globalParams[i][1];
if (_value) {
request.push(globalParams[i][0] + '='+ encodeURIComponent(_value));
}
}

$$PREBID_GLOBAL$$[callbackName] = handleCallback(bids);
request.push('callback=$$PREBID_GLOBAL$$.' + callbackName);

adloader.loadScript(request.join('&'));
}
Expand All @@ -41,18 +50,12 @@ function AdformAdapter() {
var key;
var url = [];

var validProps = [
'mid', 'inv', 'pdom', 'mname', 'mkw', 'mkv', 'cat', 'bcat', 'bcatrt', 'adv', 'advt', 'cntr', 'cntrt', 'maxp',
'minp', 'sminp', 'w', 'h', 'pb', 'pos', 'cturl', 'iturl', 'cttype', 'hidedomain', 'cdims', 'test'
];

for (var i = 0, l = validProps.length; i < l; i++) {
key = validProps[i];
if (reqData.hasOwnProperty(key))
url.push(key, '=', reqData[key], '&');
for (key in reqData) {
if (reqData.hasOwnProperty(key) && reqData[key])
url.push(key, '=', reqData[key], '&');
}

return encode64(url.join(''));
return encode64(url.join('').slice(0, -1));
}

function handleCallback(bids) {
Expand All @@ -74,6 +77,7 @@ function AdformAdapter() {
bidObject.ad = adItem.banner;
bidObject.width = adItem.width;
bidObject.height = adItem.height;
bidObject.dealId = adItem.deal_id;
bidmanager.addBidResponse(bid.placementCode, bidObject);
} else {
bidObject = bidfactory.createBid(2);
Expand Down
170 changes: 170 additions & 0 deletions test/spec/adapters/adform_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// jshint esversion: 6

import { assert } from 'chai';
import * as utils from '../../../src/utils';
import adLoader from '../../../src/adloader';
import bidManager from '../../../src/bidmanager';
import adapter from '../../../src/adapters/adform';

describe('Adform adapter', () => {
let _adapter, sandbox;

describe('request', () => {
it('should create callback method on PREBID_GLOBAL', () => {
assert.typeOf($$PREBID_GLOBAL$$._adf_callback, 'function');
});

it('should pass multiple bids via single request', () => {
const _request = adLoader.loadScript;

assert(_request.calledOnce);
assert.lengthOf(_request.args[0], 1);
assert.lengthOf(parseUrl(_request.args[0][0]).items, 3);
});

it('should handle global request parameters', () => {
const _request = parseUrl(adLoader.loadScript.args[0][0]);
const _query = _request.query;

assert.equal(_request.path, '//newdomain/adx');
assert.equal(_query.callback.split('.')[1], '_adf_callback');
assert.equal(_query.tid, 145);
assert.equal(_query.rp, 4);
assert.equal(_query.url, encodeURIComponent('some// there'));
});


it('should correctly form bid items', () => {
const _items = parseUrl(adLoader.loadScript.args[0][0]).items;

assert.deepEqual(_items[0], { mid: '1' });
assert.deepEqual(_items[1], { mid: '2', someVar: 'someValue' });
assert.deepEqual(_items[2], { mid: '3', pdom: 'home' });
});
});

describe('response callback', () => {
it('should create bid response item for every requested item', () => {
assert(bidManager.addBidResponse.calledThrice);
});

it('should correctly form bid response object', () => {
const _bid = bidManager.addBidResponse.firstCall.args;
const _bidObject = _bid[1];

assert.equal(_bid[0], 'code-1');
assert.equal(_bidObject.statusMessage, 'Bid available');
assert.equal(_bidObject.bidderCode, 'adform');
assert.equal(_bidObject.cpm, 1.1);
assert.equal(_bidObject.cur, 'EUR');
assert.equal(_bidObject.ad, '<tag>');
assert.equal(_bidObject.width, 90);
assert.equal(_bidObject.height, 90);
assert.equal(_bidObject.dealId, 'deal-1');
});

it('should correctly form empty bid response object', () => {
const _bid = bidManager.addBidResponse.secondCall.args;
const _bidObject = _bid[1];

assert.equal(_bid[0], 'code-2');
assert.equal(_bidObject.statusMessage, 'Bid returned empty or error response');
assert.equal(_bidObject.bidderCode, 'adform');
});

it('should filter out item which does not fit required size', () => {
const _bid = bidManager.addBidResponse.thirdCall.args;
const _bidObject = _bid[1];

assert.equal(_bid[0], 'code-3');
assert.equal(_bidObject.statusMessage, 'Bid returned empty or error response');
assert.equal(_bidObject.bidderCode, 'adform');
});

beforeEach(() => {
sandbox.stub(bidManager, 'addBidResponse');
$$PREBID_GLOBAL$$._adf_callback([
{
response: 'banner',
width: 90,
height: 90,
banner: '<tag>',
win_bid: 1.1,
win_cur: 'EUR',
deal_id: 'deal-1'
},
{},
{
response: 'banner',
width: 50,
height: 50,
banner: '<tag>'
}
]);
});
});

beforeEach(() => {
_adapter = adapter();
utils.getUniqueIdentifierStr = () => 'callback';
sandbox = sinon.sandbox.create();
sandbox.stub(adLoader, 'loadScript');
_adapter.callBids({
bids: [
{
placementCode: 'code-1',
sizes: [ [ 100, 100], [ 90, 90 ] ],
params: {
mid: 1,
url: 'some// there'
},
adxDomain: 'newdomain',
tid: 45
},
{
placementCode: 'code-2',
sizes: [ [ 100, 100] ],
params: {
mid: 2,
tid: 145,
someVar: 'someValue'
}
},
{
placementCode: 'code-3',
sizes: [ [ 50, 40], [ 40, 50 ] ],
params: {
mid: 3,
pdom: 'home'
}
}
]});
});

afterEach(() => {
sandbox.restore();
});
});

function parseUrl(url) {
const parts = url.split('/');
const query = parts.pop().split('&');
return {
path: parts.join('/'),
items: query
.filter((i) => ! ~i.indexOf('='))
.map((i) => atob(i)
.split('&')
.reduce(toObject, {})),
query: query
.filter((i) => ~i.indexOf('='))
.map((i) => i.replace('?', ''))
.reduce(toObject, {})
};
}

function toObject(cache, string) {
const keyValue = string.split('=');
cache[keyValue[0]] = keyValue[1];
return cache;
}