diff --git a/modules/appnexusBidAdapter.js b/modules/appnexusBidAdapter.js index 5210d6f7562..bb80b6a8588 100644 --- a/modules/appnexusBidAdapter.js +++ b/modules/appnexusBidAdapter.js @@ -774,6 +774,18 @@ function bidToTag(bid) { } else { tag.code = bid.params.inv_code; } + // Xandr expects GET variable to be in a following format: + // page.html?ast_override_div=divId:creativeId,divId2:creativeId2 + const overrides = getParameterByName('ast_override_div'); + if (isStr(overrides) && overrides !== '') { + const adUnitOverride = overrides.split(',').find((pair) => pair.startsWith(`${bid.adUnitCode}:`)); + if (adUnitOverride) { + const forceCreativeId = adUnitOverride.split(':')[1]; + if (forceCreativeId) { + tag.force_creative_id = parseInt(forceCreativeId, 10); + } + } + } tag.allow_smaller_sizes = bid.params.allow_smaller_sizes || false; tag.use_pmt_rule = (typeof bid.params.use_payment_rule === 'boolean') ? bid.params.use_payment_rule : (typeof bid.params.use_pmt_rule === 'boolean') ? bid.params.use_pmt_rule : false; diff --git a/test/spec/modules/appnexusBidAdapter_spec.js b/test/spec/modules/appnexusBidAdapter_spec.js index cc86a8a0aaa..2a277cf1ba7 100644 --- a/test/spec/modules/appnexusBidAdapter_spec.js +++ b/test/spec/modules/appnexusBidAdapter_spec.js @@ -1442,6 +1442,88 @@ describe('AppNexusAdapter', function () { config.getConfig.restore(); }); + describe('ast_override_div', function () { + let getParamStub; + let bidRequest = Object.assign({}, bidRequests[0]); + let bidRequest2 = deepClone(bidRequests[0]); + bidRequest2.adUnitCode = 'adUnit_code_2'; + let bidRequest3 = deepClone(bidRequests[0]); + bidRequest3.adUnitCode = 'adUnit_code_3'; + + before(function () { + getParamStub = sinon.stub(utils, 'getParameterByName'); + }); + + it('should set forced creative id if one adUnitCode passed', function () { + getParamStub.callsFake(function(par) { + if (par === 'ast_override_div') return 'adunit-code:1234'; + return ''; + }); + + const request = spec.buildRequests([bidRequest, bidRequest2]); + const payload = JSON.parse(request.data); + + expect(payload.tags[0].force_creative_id).to.deep.equal(1234); + expect(payload.tags[1].force_creative_id).to.not.exist; + }); + + it('should set forced creative id if `ast_override_div` is set to override multiple adUnitCode', function () { + getParamStub.callsFake(function(par) { + if (par === 'ast_override_div') return 'adunit-code:1234,adUnit_code_2:5678'; + return ''; + }); + + const request = spec.buildRequests([bidRequest, bidRequest2, bidRequest3]); + const payload = JSON.parse(request.data); + + expect(payload.tags[0].force_creative_id).to.deep.equal(1234); + expect(payload.tags[1].force_creative_id).to.deep.equal(5678); + expect(payload.tags[2].force_creative_id).to.not.exist; + }); + + it('should not set forced creative id if `ast_override_div` is missing creativeId', function () { + getParamStub.callsFake(function(par) { + if (par === 'ast_override_div') return 'adunit-code'; + return ''; + }); + + const request = spec.buildRequests([bidRequest, bidRequest2]); + const payload = JSON.parse(request.data); + + expect(payload.tags[0].force_creative_id).to.not.exist; + expect(payload.tags[1].force_creative_id).to.not.exist; + }); + + it('should not set forced creative id if `ast_override_div` is in the wrong format', function () { + getParamStub.callsFake(function(par) { + if (par === 'ast_override_div') return 'adunit-code;adUnit_code_2:5678'; + return ''; + }); ; + + const request = spec.buildRequests([bidRequest, bidRequest2]); + const payload = JSON.parse(request.data); + + expect(payload.tags[0].force_creative_id).to.not.exist; + expect(payload.tags[1].force_creative_id).to.not.exist; + }); + + it('should not set forced creative id if `ast_override_div` is missing', function () { + getParamStub.callsFake(function(par) { + return ''; + }); ; + + const request = spec.buildRequests([bidRequest, bidRequest2]); + const payload = JSON.parse(request.data); + + expect(payload.tags[0].force_creative_id).to.not.exist; + expect(payload.tags[1].force_creative_id).to.not.exist; + }); + + after(function () { + getParamStub.restore(); + }); + }); + it('should set the X-Is-Test customHeader if test flag is enabled', function () { let bidRequest = Object.assign({}, bidRequests[0]); sinon.stub(config, 'getConfig')