Skip to content

Commit

Permalink
Merge pull request #567 from PubMatic-OpenWrap/ns/UOE-8382
Browse files Browse the repository at this point in the history
Ns/uoe 8382
  • Loading branch information
pm-nitin-shirsat authored Dec 7, 2022
2 parents bfc0ea3 + dc2b091 commit be0e2be
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 4 deletions.
39 changes: 35 additions & 4 deletions modules/pubmaticBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { logWarn, _each, isBoolean, isStr, isArray, inIframe, mergeDeep, deepAccess, isNumber, deepSetValue, logInfo, logError, deepClone, convertTypes } from '../src/utils.js';
import { logWarn, _each, isBoolean, isStr, isArray, inIframe, mergeDeep, deepAccess, isNumber, deepSetValue, logInfo, logError, deepClone, convertTypes, parseQueryStringParameters } from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER, VIDEO, NATIVE } from '../src/mediaTypes.js';
import {config} from '../src/config.js';
Expand All @@ -7,7 +7,7 @@ import { bidderSettings } from '../src/bidderSettings.js';

const BIDDER_CODE = 'pubmatic';
const LOG_WARN_PREFIX = 'PubMatic: ';
const ENDPOINT = 'https://hbopenbid.pubmatic.com/translator?source=ow-client';
const ENDPOINT = 'https://hbopenbid.pubmatic.com/translator';
const USER_SYNC_URL_IFRAME = 'https://ads.pubmatic.com/AdServer/js/user_sync.html?kdntuid=1&p=';
const USER_SYNC_URL_IMAGE = 'https://image8.pubmatic.com/AdServer/ImgSync?p=';
const DEFAULT_CURRENCY = 'USD';
Expand Down Expand Up @@ -1077,6 +1077,17 @@ export function prepareMetaObject(br, bid, seat) {
}
}

/**
* returns, boolean value according to translator get request is enabled
* and random value should be less than or equal to testGroupPercentage
* @returns boolean
*/
function hasGetRequestEnabled() {
if (!(config.getConfig('translatorGetRequest.enabled') === true)) return false;
const randomValue100 = Math.ceil(Math.random() * 100);
const testGroupPercentage = config.getConfig('translatorGetRequest.testGroupPercentage') || 0;
return randomValue100 <= testGroupPercentage;
}

export const spec = {
code: BIDDER_CODE,
Expand Down Expand Up @@ -1316,12 +1327,30 @@ export const spec = {
delete payload.site;
}

return {
let serverRequest = {
method: 'POST',
url: ENDPOINT,
url: ENDPOINT + '?source=ow-client',
data: JSON.stringify(payload),
bidderRequest: bidderRequest
};

// Allow translator request to execute it as GET Methoid if flag is set.
if (hasGetRequestEnabled()) {
const maxUrlLength = config.getConfig('translatorGetRequest.maxUrlLength') || 63000;
const configuredEndPoint = config.getConfig('translatorGetRequest.endPoint') || ENDPOINT;
const urlEncodedPayloadStr = parseQueryStringParameters({ 'source': 'ow-client', 'payload': JSON.stringify(payload) });
if ((configuredEndPoint + '?' + urlEncodedPayloadStr)?.length <= maxUrlLength) {
serverRequest = {
method: 'GET',
url: configuredEndPoint,
data: urlEncodedPayloadStr,
bidderRequest: bidderRequest,
payloadStr: JSON.stringify(payload)
};
}
}

return serverRequest;
},

/**
Expand All @@ -1333,6 +1362,8 @@ export const spec = {
interpretResponse: (response, request) => {
const bidResponses = [];
var respCur = DEFAULT_CURRENCY;
// In case of Translator GET request, will copy the actual json data from payloadStr to data.
if(request?.payloadStr) request.data = request.payloadStr;
let parsedRequest = JSON.parse(request.data);
let parsedReferrer = parsedRequest.site && parsedRequest.site.ref ? parsedRequest.site.ref : '';
try {
Expand Down
113 changes: 113 additions & 0 deletions test/spec/modules/pubmaticBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('PubMatic adapter', function () {
let outstreamBidRequest;
let validOutstreamBidRequest;
let outstreamVideoBidResponse;
const ENDPOINT = 'https://hbopenbid.pubmatic.com/translator';

beforeEach(() => {
schainConfig = {
Expand Down Expand Up @@ -2513,6 +2514,118 @@ describe('PubMatic adapter', function () {
}]);
});
});

describe('buildRequests function: Translator POST or GET request, depends on config', function() {
it('should return POST request when config is not set', function () {
let request = spec.buildRequests(bidRequests, {
auctionId: 'new-auction-id'
});
expect(request.method).to.equal('POST');
expect(request.url).to.equal(ENDPOINT + '?source=ow-client');
});

it('should return POST request, when translatorGetRequest config is enabled=false', () => {
let sandbox = sinon.sandbox.create();
sandbox.stub(config, 'getConfig').callsFake(key => {
const config = {
translatorGetRequest: {
enabled: false
}
};
return utils.deepAccess(config, key);
});
const request = spec.buildRequests(bidRequests, {});
expect(request.method).to.equal('POST');
expect(request.url).to.equal(ENDPOINT + '?source=ow-client');
sandbox.restore();
});

it('should return POST request, when translatorGetRequest config is enabled=true, testGroupPercentage is not provided so considering default 0', () => {
let sandbox = sinon.sandbox.create();
sandbox.stub(config, 'getConfig').callsFake(key => {
const config = {
translatorGetRequest: {
enabled: true
}
};
return utils.deepAccess(config, key);
});
const request = spec.buildRequests(bidRequests, {});
expect(request.method).to.equal('POST');
expect(request.url).to.equal(ENDPOINT + '?source=ow-client');
sandbox.restore();
});

it('should return POST request, when translatorGetRequest config is enabled=true, testGroupPercentage: 0', () => {
let sandbox = sinon.sandbox.create();
sandbox.stub(config, 'getConfig').callsFake(key => {
const config = {
translatorGetRequest: {
enabled: true,
testGroupPercentage: 0
}
};
return utils.deepAccess(config, key);
});
const request = spec.buildRequests(bidRequests, {});
expect(request.method).to.equal('POST');
expect(request.url).to.equal(ENDPOINT + '?source=ow-client');
sandbox.restore();
});

it('should return POST request, when translatorGetRequest config is enabled=true, testGroupPercentage: 100, maxUrlLength is small than URL length', () => {
let sandbox = sinon.sandbox.create();
sandbox.stub(config, 'getConfig').callsFake(key => {
const config = {
translatorGetRequest: {
enabled: true,
testGroupPercentage: 100,
maxUrlLength: 10
}
};
return utils.deepAccess(config, key);
});
const request = spec.buildRequests(bidRequests, {});
expect(request.method).to.equal('POST');
expect(request.url).to.equal(ENDPOINT + '?source=ow-client');
sandbox.restore();
});

it('should return GET request, when translatorGetRequest config is enabled=true, testGroupPercentage: 100, maxUrlLength is not provided should take default', () => {
let sandbox = sinon.sandbox.create();
sandbox.stub(config, 'getConfig').callsFake(key => {
const config = {
translatorGetRequest: {
enabled: true,
testGroupPercentage: 100
}
};
return utils.deepAccess(config, key);
});
const request = spec.buildRequests(bidRequests, {});
expect(request.method).to.equal('GET');
expect(request.url).to.equal(ENDPOINT);
sandbox.restore();
});

it('should return GET request, when translatorGetRequest config is enabled=true, testGroupPercentage: 100, maxUrlLength: 63000', () => {
let sandbox = sinon.sandbox.create();
sandbox.stub(config, 'getConfig').callsFake(key => {
const config = {
translatorGetRequest: {
enabled: true,
testGroupPercentage: 100,
maxUrlLength: 63000
}
};
return utils.deepAccess(config, key);
});
const request = spec.buildRequests(bidRequests, {});
expect(request.method).to.equal('GET');
expect(request.url).to.equal(ENDPOINT);
sandbox.restore();
});
});
});

it('Request params check for video ad', function () {
Expand Down

0 comments on commit be0e2be

Please sign in to comment.