From a849eef76dabbe82ab8f4444e0481cb8902bfd5c Mon Sep 17 00:00:00 2001 From: John Ellis Date: Thu, 19 Jul 2018 16:53:15 -0400 Subject: [PATCH] Yieldbot adapter use utils.timestamp (#2848) --- modules/yieldbotBidAdapter.js | 16 +++---- test/spec/modules/yieldbotBidAdapter_spec.js | 47 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/modules/yieldbotBidAdapter.js b/modules/yieldbotBidAdapter.js index 43d942209078..a18448a0b0b5 100644 --- a/modules/yieldbotBidAdapter.js +++ b/modules/yieldbotBidAdapter.js @@ -10,7 +10,7 @@ import { registerBidder } from 'src/adapters/bidderFactory'; * @private */ export const YieldbotAdapter = { - _adapterLoaded: Date.now(), + _adapterLoaded: utils.timestamp(), _navigationStart: 0, _version: 'pbjs-yb-0.0.1', _bidRequestCount: 0, @@ -205,7 +205,7 @@ export const YieldbotAdapter = { const bidUrl = this.urlPrefix() + yieldbotSlotParams.psn + '/v1/init'; - searchParams['cts_ini'] = Date.now(); + searchParams['cts_ini'] = utils.timestamp(); requests.push({ method: 'GET', url: bidUrl, @@ -325,7 +325,7 @@ export const YieldbotAdapter = { buildAdUrl: function(urlPrefix, publisherNumber, commonSearchParams, bid) { const searchParams = Object.assign({}, commonSearchParams); - searchParams['cts_res'] = Date.now(); + searchParams['cts_res'] = utils.timestamp(); searchParams['slot'] = bid.slot + ':' + bid.size; searchParams['ioa'] = this.intersectionObserverAvailable(window); @@ -369,7 +369,7 @@ export const YieldbotAdapter = { const adUrl = this.buildAdUrl(urlPrefix, publisherNumber, commonSearchParams, bid); const impressionUrl = this.buildImpressionUrl(urlPrefix, publisherNumber, commonSearchParams); - const htmlMarkup = `
`; + const htmlMarkup = `
`; return { ad: htmlMarkup, creativeId: ybotAdRequestId }; }, @@ -410,7 +410,7 @@ export const YieldbotAdapter = { const userId = this.userId; const sessionId = this.sessionId; const pageviewId = this.newId(); - const currentBidTime = Date.now(); + const currentBidTime = utils.timestamp(); const lastBidTime = this.lastPageviewTime; const lastBidId = this.lastPageviewId; this.lastPageviewTime = currentBidTime; @@ -537,7 +537,7 @@ export const YieldbotAdapter = { setCookie: function(name, value, expireMillis, path, domain, secure) { const dataValue = encodeURIComponent(value); const cookieStr = name + '=' + dataValue + - (expireMillis ? ';expires=' + new Date(Date.now() + expireMillis).toGMTString() : '') + + (expireMillis ? ';expires=' + new Date(utils.timestamp() + expireMillis).toGMTString() : '') + (path ? ';path=' + path : '') + (domain ? ';domain=' + domain : '') + (secure ? ';secure' : ''); @@ -567,7 +567,7 @@ export const YieldbotAdapter = { * @private */ newId: function() { - return (+new Date()).toString(36) + 'xxxxxxxxxx' + return (utils.timestamp()).toString(36) + 'xxxxxxxxxx' .replace(/[x]/g, function() { return (0 | Math.random() * 36).toString(36); }); @@ -600,5 +600,5 @@ export const spec = { getUserSyncs: YieldbotAdapter.createDelegate(YieldbotAdapter, YieldbotAdapter.getUserSyncs) }; -YieldbotAdapter._navigationStart = Date.now(); +YieldbotAdapter._navigationStart = utils.timestamp(); registerBidder(spec); diff --git a/test/spec/modules/yieldbotBidAdapter_spec.js b/test/spec/modules/yieldbotBidAdapter_spec.js index 206645acd95f..2977e4ef30df 100644 --- a/test/spec/modules/yieldbotBidAdapter_spec.js +++ b/test/spec/modules/yieldbotBidAdapter_spec.js @@ -1323,4 +1323,51 @@ describe('Yieldbot Adapter Unit Tests', function() { done(); }); }); + + describe('Adapter Request Timestamps', function() { + let sandbox; + beforeEach(function() { + sandbox = sinon.sandbox.create(); + sandbox.stub(Date, 'now').callsFake(() => { + return new Date(); + }); + }); + + afterEach(function() { + sandbox.restore(); + }); + + it('should have overridden Date.now() function', function() { + expect(Date.now().getTime()).to.match(/^[0-9]+/); + }); + + it('should be milliseconds past epoch query param values', function() { + const request = YieldbotAdapter.buildRequests(FIXTURE_BID_REQUESTS)[0]; + expect(request.data).to.not.equal(undefined); + + const timestampParams = [ + 'cts_ns', + 'cts_js', + 'cts_ini' + ]; + + timestampParams.forEach((item) => { + expect(!isNaN(request.data[item])).to.equal(true); + expect(request.data[item] > 0).to.equal(true); + expect(request.data[item]).to.match(/^[0-9]+/); + }); + }); + + it('should use (new Date()).getTime() for timestamps in ad markup', function() { + FIXTURE_SERVER_RESPONSE.body.url_prefix = 'http://close.edge.adserver.com/'; + const responses = YieldbotAdapter.interpretResponse( + FIXTURE_SERVER_RESPONSE, + FIXTURE_BID_REQUEST + ); + + expect(responses[0].ad).to.match(/cts_rend_.*='\+\(new Date\(\)\)\.getTime\(\)/); + expect(responses[0].ad).to.match(/cts_ad='\+\(new Date\(\)\)\.getTime\(\)/); + expect(responses[0].ad).to.match(/cts_imp='\+\(new Date\(\)\)\.getTime\(\)/); + }); + }); });