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

Rhythmone Adapter - deprecate direct usage of window objects. #4061

Merged
merged 1 commit into from Aug 7, 2019
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
67 changes: 30 additions & 37 deletions modules/rhythmoneBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ function RhythmOneBidAdapter() {
let SUPPORTED_VIDEO_API = [1, 2, 5];
let slotsToBids = {};
let that = this;
let version = '2.0.1.0';
var win = typeof window !== 'undefined' ? window : {};
let version = '2.1';

this.isBidRequestValid = function (bid) {
return !!(bid.params && bid.params.placementId);
Expand All @@ -26,14 +25,21 @@ function RhythmOneBidAdapter() {
return [];
};

function frameImp(BRs) {
function frameImp(BRs, bidderRequest) {
var impList = [];
var isSecure = 0;
if (bidderRequest && bidderRequest.refererInfo && bidderRequest.refererInfo.stack.length) {
// clever trick to get the protocol
var el = document.createElement('a');
el.href = bidderRequest.refererInfo.stack[0];
isSecure = (el.protocol == 'https:') ? 1 : 0;
}
for (var i = 0; i < BRs.length; i++) {
slotsToBids[BRs[i].adUnitCode || BRs[i].placementCode] = BRs[i];
slotsToBids[BRs[i].adUnitCode] = BRs[i];
var impObj = {};
impObj.id = BRs[i].adUnitCode;
impObj.bidfloor = parseFloat(utils.deepAccess(BRs[i], 'params.floor')) || 0;
impObj.secure = win.location.protocol === 'https:' ? 1 : 0;
impObj.secure = isSecure;

if (utils.deepAccess(BRs[i], 'mediaTypes.banner') || utils.deepAccess(BRs[i], 'mediaType') === 'banner') {
let banner = frameBanner(BRs[i]);
Expand All @@ -54,31 +60,25 @@ function RhythmOneBidAdapter() {
}

function frameSite(bidderRequest) {
return {
domain: attempt(function() {
var d = win.document.location.ancestorOrigins;
if (d && d.length > 0) {
return d[d.length - 1];
}
return win.top.document.location.hostname; // try/catch is in the attempt function
}, ''),
page: attempt(function() {
var l;
// try/catch is in the attempt function
try {
l = win.top.document.location.href.toString();
} catch (ex) {
l = win.document.location.href.toString();
}
return l;
}, ''),
ref: attempt(function() {
if (bidderRequest && bidderRequest.refererInfo) {
return bidderRequest.refererInfo.referer;
}
return '';
}, '')
var site = {
domain: '',
page: '',
ref: ''
}
if (bidderRequest && bidderRequest.refererInfo) {
var ri = bidderRequest.refererInfo;
site.ref = ri.referer;

if (ri.stack.length) {
site.page = ri.stack[ri.stack.length - 1];

// clever trick to get the domain
var el = document.createElement('a');
el.href = ri.stack[0];
site.domain = el.hostname;
}
}
return site;
}

function frameDevice() {
Expand Down Expand Up @@ -165,7 +165,7 @@ function RhythmOneBidAdapter() {
function frameBid(BRs, bidderRequest) {
return {
id: BRs[0].bidderRequestId,
imp: frameImp(BRs),
imp: frameImp(BRs, bidderRequest),
site: frameSite(bidderRequest),
device: frameDevice(),
user: {
Expand All @@ -191,13 +191,6 @@ function RhythmOneBidAdapter() {
}
}

function attempt(valueFunction, defaultValue) {
try {
return valueFunction();
} catch (ex) { }
return defaultValue;
}

this.buildRequests = function (BRs, bidderRequest) {
let fallbackPlacementId = getFirstParam('placementId', BRs);
if (fallbackPlacementId === undefined || BRs.length < 1) {
Expand Down
97 changes: 96 additions & 1 deletion test/spec/modules/rhythmoneBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ describe('rhythmone adapter tests', function () {
beforeEach(function() {
this.defaultBidderRequest = {
'refererInfo': {
'referer': 'Reference Page'
'referer': 'Reference Page',
'stack': [
'aodomain.dvl',
'page.dvl'
]
}
};
});
Expand Down Expand Up @@ -611,6 +615,91 @@ describe('rhythmone adapter tests', function () {

expect(bidRequest).to.be.empty;
});

it('should return empty site data when refererInfo is missing', function() {
delete this.defaultBidderRequest.refererInfo;
var bidRequestList = [
{
'bidder': 'rhythmone',
'params': {
'placementId': 'myplacement',
'zone': 'myzone',
'path': 'mypath'
},
'mediaType': 'banner',
'adUnitCode': 'div-gpt-ad-1438287399331-0',
'sizes': [[300, 250]],
'transactionId': 'd7b773de-ceaa-484d-89ca-d9f51b8d61ec',
'bidderRequestId': '418b37f85e772c',
'auctionId': '18fd8b8b0bd757',
'bidRequestsCount': 1,
'bidId': '51ef8751f9aead'
}
];

var bidRequest = r1adapter.buildRequests(bidRequestList, this.defaultBidderRequest);
const openrtbRequest = JSON.parse(bidRequest.data);

expect(openrtbRequest.site.domain).to.equal('');
expect(openrtbRequest.site.page).to.equal('');
expect(openrtbRequest.site.ref).to.equal('');
});
});

it('should return empty site.domain and site.page when refererInfo.stack is empty', function() {
this.defaultBidderRequest.refererInfo.stack = [];
var bidRequestList = [
{
'bidder': 'rhythmone',
'params': {
'placementId': 'myplacement',
'zone': 'myzone',
'path': 'mypath'
},
'mediaType': 'banner',
'adUnitCode': 'div-gpt-ad-1438287399331-0',
'sizes': [[300, 250]],
'transactionId': 'd7b773de-ceaa-484d-89ca-d9f51b8d61ec',
'bidderRequestId': '418b37f85e772c',
'auctionId': '18fd8b8b0bd757',
'bidRequestsCount': 1,
'bidId': '51ef8751f9aead'
}
];

var bidRequest = r1adapter.buildRequests(bidRequestList, this.defaultBidderRequest);
const openrtbRequest = JSON.parse(bidRequest.data);

expect(openrtbRequest.site.domain).to.equal('');
expect(openrtbRequest.site.page).to.equal('');
expect(openrtbRequest.site.ref).to.equal('Reference Page');
});

it('should secure correctly', function() {
this.defaultBidderRequest.refererInfo.stack[0] = ['https://securesite.dvl'];
var bidRequestList = [
{
'bidder': 'rhythmone',
'params': {
'placementId': 'myplacement',
'zone': 'myzone',
'path': 'mypath'
},
'mediaType': 'banner',
'adUnitCode': 'div-gpt-ad-1438287399331-0',
'sizes': [[300, 250]],
'transactionId': 'd7b773de-ceaa-484d-89ca-d9f51b8d61ec',
'bidderRequestId': '418b37f85e772c',
'auctionId': '18fd8b8b0bd757',
'bidRequestsCount': 1,
'bidId': '51ef8751f9aead'
}
];

var bidRequest = r1adapter.buildRequests(bidRequestList, this.defaultBidderRequest);
const openrtbRequest = JSON.parse(bidRequest.data);

expect(openrtbRequest.imp[0].secure).to.equal(1);
});

describe('misc interpretResponse', function () {
Expand Down Expand Up @@ -647,4 +736,10 @@ describe('rhythmone adapter tests', function () {
expect(r1adapter.isBidRequestValid(bid)).to.equal(false);
});
});

describe('getUserSyncs', function () {
it('returns an empty string', function () {
expect(r1adapter.getUserSyncs()).to.deep.equal([]);
});
});
});