Skip to content

Commit

Permalink
Add support to pass custom key in Prebid cache request (#3437)
Browse files Browse the repository at this point in the history
* add support to pass custom key in PBC request

* update logic to allow individual custom keys per bid
  • Loading branch information
jsnellbaker authored and jaiminpanchal27 committed Feb 6, 2019
1 parent e730457 commit bdcd0c2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/videoCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ function wrapURI(uri, impUrl) {
*/
function toStorageRequest(bid) {
const vastValue = bid.vastXml ? bid.vastXml : wrapURI(bid.vastUrl, bid.vastImpUrl);
return {
let payload = {
type: 'xml',
value: vastValue,
ttlseconds: Number(bid.ttl)
};

if (typeof bid.customCacheKey === 'string' && bid.customCacheKey !== '') {
payload.key = bid.customCacheKey;
}

return payload;
}

/**
Expand Down Expand Up @@ -114,7 +120,7 @@ function shimStorageCallback(done) {
*
* @param {CacheableBid[]} bids A list of bid objects which should be cached.
* @param {videoCacheStoreCallback} [done] An optional callback which should be executed after
* the data has been stored in the cache.
* the data has been stored in the cache.
*/
export function store(bids, done) {
const requestData = {
Expand Down
37 changes: 37 additions & 0 deletions test/spec/videoCache_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ describe('The video cache', function () {
assertRequestMade({ vastXml: vastXml, ttl: 25 }, vastXml);
});

it('should make the expected request when store() is called while supplying a custom key param', function () {
const customKey1 = 'keyword_abc_123';
const customKey2 = 'other_xyz_789';
const vastXml1 = '<VAST version="3.0">test1</VAST>';
const vastXml2 = '<VAST version="3.0">test2</VAST>';

const bids = [{
vastXml: vastXml1,
ttl: 25,
customCacheKey: customKey1
}, {
vastXml: vastXml2,
ttl: 25,
customCacheKey: customKey2
}];

store(bids, function () { });
const request = requests[0];
request.method.should.equal('POST');
request.url.should.equal('https://prebid.adnxs.com/pbc/v1/cache');
request.requestHeaders['Content-Type'].should.equal('text/plain;charset=utf-8');
let payload = {
puts: [{
type: 'xml',
value: vastXml1,
ttlseconds: 25,
key: customKey1
}, {
type: 'xml',
value: vastXml2,
ttlseconds: 25,
key: customKey2
}]
};
JSON.parse(request.requestBody).should.deep.equal(payload);
});

function assertRequestMade(bid, expectedValue) {
store([bid], function() { });

Expand Down

0 comments on commit bdcd0c2

Please sign in to comment.