diff --git a/modules/currency.js b/modules/currency.js index f66c33bbed8..fbb7a0cf30d 100644 --- a/modules/currency.js +++ b/modules/currency.js @@ -5,7 +5,7 @@ import * as utils from 'src/utils'; import { config } from 'src/config'; import { hooks } from 'src/hook.js'; -const DEFAULT_CURRENCY_RATE_URL = 'https://currency.prebid.org/latest.json'; +const DEFAULT_CURRENCY_RATE_URL = 'https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=$$TODAY$$'; const CURRENCY_RATE_PRECISION = 4; var bidResponseQueue = []; @@ -34,7 +34,7 @@ var defaultRates; * { * rubicon: 'USD' * } - * @param {string} [config.conversionRateFile = 'http://currency.prebid.org/latest.json'] + * @param {string} [config.conversionRateFile = 'URL pointing to conversion file'] * Optional path to a file containing currency conversion data. Prebid.org hosts a file that is used as the default, * if not specified. * @param {object} [config.rates] @@ -60,6 +60,10 @@ export function setConfig(config) { if (typeof config.defaultRates === 'object') { defaultRates = config.defaultRates; + + // set up the default rates to be used if the rate file doesn't get loaded in time + currencyRates.conversions = defaultRates; + currencyRatesLoaded = true; } if (typeof config.adServerCurrency === 'string') { @@ -70,6 +74,25 @@ export function setConfig(config) { utils.logInfo('currency using override conversionRateFile:', config.conversionRateFile); url = config.conversionRateFile; } + + // see if the url contains a date macro + // this is a workaround to the fact that jsdelivr doesn't currently support setting a 24-hour HTTP cache header + // So this is an approach to let the browser cache a copy of the file each day + // We should remove the macro once the CDN support a day-level HTTP cache setting + const macroLocation = url.indexOf('$$TODAY$$'); + if (macroLocation !== -1) { + // get the date to resolve the macro + const d = new Date(); + let month = `${d.getMonth() + 1}`; + let day = `${d.getDate()}`; + if (month.length < 2) month = `0${month}`; + if (day.length < 2) day = `0${day}`; + const todaysDate = `${d.getFullYear()}${month}${day}`; + + // replace $$TODAY$$ with todaysDate + url = `${url.substring(0, macroLocation)}${todaysDate}${url.substring(macroLocation + 9, url.length)}`; + } + initCurrency(url); } else { // currency support is disabled, setting defaults @@ -84,8 +107,6 @@ config.getConfig('currency', config => setConfig(config.currency)); function errorSettingsRates(msg) { if (defaultRates) { - currencyRates.conversions = defaultRates; - currencyRatesLoaded = true; utils.logWarn(msg); utils.logWarn('Currency failed loading rates, falling back to currency.defaultRates'); } else { diff --git a/test/spec/modules/currency_spec.js b/test/spec/modules/currency_spec.js index e96b15d11e9..64c966bdf18 100644 --- a/test/spec/modules/currency_spec.js +++ b/test/spec/modules/currency_spec.js @@ -17,6 +17,8 @@ var expect = require('chai').expect; describe('currency', function () { let fakeCurrencyFileServer; + let sandbox; + let clock; let fn = sinon.spy(); let hookFn = createHook('asyncSeries', fn, 'addBidResponse'); @@ -30,6 +32,16 @@ describe('currency', function () { }); describe('setConfig', function () { + beforeEach(function() { + sandbox = sinon.sandbox.create(); + clock = sinon.useFakeTimers(1047010195974); + }); + + afterEach(function () { + sandbox.restore(); + clock.restore(); + }); + it('results in currencySupportEnabled = false when currency not configured', function () { setConfig({}); expect(currencySupportEnabled).to.equal(false); @@ -42,6 +54,40 @@ describe('currency', function () { expect(currencyRates.dataAsOf).to.equal('2017-04-25'); expect(currencySupportEnabled).to.equal(true); }); + + it('date macro token $$TODAY$$ is replaced by current date (formatted as yyyymmdd)', function () { + // RESET to request currency file (specifically url value for this test) + setConfig({ 'adServerCurrency': undefined }); + + // date macro should replace $$TODAY$$ with date when DEFAULT_CURRENCY_RATE_URL is used + setConfig({ 'adServerCurrency': 'JPY' }); + fakeCurrencyFileServer.respond(); + expect(fakeCurrencyFileServer.requests[0].url).to.equal('https://cdn.jsdelivr.net/gh/prebid/currency-file@1/latest.json?date=20030306'); + + // date macro should not modify 'conversionRateFile' if TOKEN is not found + setConfig({ + 'adServerCurrency': 'JPY', + 'conversionRateFile': 'http://test.net/currency.json?date=foobar' + }); + fakeCurrencyFileServer.respond(); + expect(fakeCurrencyFileServer.requests[1].url).to.equal('http://test.net/currency.json?date=foobar'); + + // date macro should replace $$TODAY$$ with date for 'conversionRateFile' is configured + setConfig({ + 'adServerCurrency': 'JPY', + 'conversionRateFile': 'http://test.net/currency.json?date=$$TODAY$$' + }); + fakeCurrencyFileServer.respond(); + expect(fakeCurrencyFileServer.requests[2].url).to.equal('http://test.net/currency.json?date=20030306'); + + // MULTIPLE TOKENS used in a url is not supported. Only the TOKEN at left-most position is REPLACED + setConfig({ + 'adServerCurrency': 'JPY', + 'conversionRateFile': 'http://test.net/$$TODAY$$/currency.json?date=$$TODAY$$' + }); + fakeCurrencyFileServer.respond(); + expect(fakeCurrencyFileServer.requests[3].url).to.equal('http://test.net/20030306/currency.json?date=$$TODAY$$'); + }); }); describe('bidder override', function () {