forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SmileWanted : add schain support (prebid#11804)
Co-authored-by: QuentinGallard <quentin.gallard@digitalnolimit.com>
- Loading branch information
1 parent
b57d632
commit 0e5d665
Showing
4 changed files
with
245 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Serialize the SupplyChain for Non-OpenRTB Requests | ||
* https://github.com/InteractiveAdvertisingBureau/openrtb/blob/main/supplychainobject.md | ||
* | ||
* @param {Object} schain The supply chain object. | ||
* @param {string} schain.ver The version of the supply chain. | ||
* @param {number} schain.complete Indicates if the chain is complete (1) or not (0). | ||
* @param {Array<Object>} schain.nodes An array of nodes in the supply chain. | ||
* @param {Array<string>} nodesProperties The list of node properties to include in the serialized string. | ||
* Can include: 'asi', 'sid', 'hp', 'rid', 'name', 'domain', 'ext'. | ||
* @returns {string|null} The serialized supply chain string or null if the nodes are not present. | ||
*/ | ||
export function serializeSupplyChain(schain, nodesProperties) { | ||
if (!schain?.nodes) return null; | ||
|
||
const header = `${schain.ver},${schain.complete}!`; | ||
const nodes = schain.nodes.map( | ||
node => nodesProperties.map( | ||
prop => node[prop] ? encodeURIComponent(node[prop]).replace(/!/g, '%21') : '' | ||
).join(',') | ||
).join('!'); | ||
|
||
return header + nodes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import {serializeSupplyChain} from '../../../libraries/schainSerializer/schainSerializer.js' | ||
describe('serializeSupplyChain', () => { | ||
describe('Single Hop - Chain Complete', () => { | ||
it('should serialize a single hop chain with complete information', () => { | ||
const schain = { | ||
ver: '1.0', | ||
complete: 1, | ||
nodes: [ | ||
{ | ||
asi: 'exchange1.com', | ||
sid: '1234', | ||
hp: 1, | ||
rid: 'bid-request-1', | ||
name: 'publisher', | ||
domain: 'publisher.com' | ||
} | ||
] | ||
}; | ||
const nodesProperties = ['asi', 'sid', 'hp', 'rid', 'name', 'domain']; | ||
const expectedResult = '1.0,1!exchange1.com,1234,1,bid-request-1,publisher,publisher.com'; | ||
expect(serializeSupplyChain(schain, nodesProperties)).to.equal(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('Single Hop - Chain Complete, optional fields missing', () => { | ||
it('should serialize a single hop chain with missing optional fields', () => { | ||
const schain = { | ||
ver: '1.0', | ||
complete: 1, | ||
nodes: [ | ||
{ | ||
asi: 'exchange1.com', | ||
sid: '1234', | ||
hp: 1 | ||
} | ||
] | ||
}; | ||
const nodesProperties = ['asi', 'sid', 'hp', 'rid', 'name', 'domain']; | ||
const expectedResult = '1.0,1!exchange1.com,1234,1,,,'; | ||
expect(serializeSupplyChain(schain, nodesProperties)).to.equal(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('Multiple Hops - With all properties supplied', () => { | ||
it('should serialize multiple hops with all properties supplied', () => { | ||
const schain = { | ||
ver: '1.0', | ||
complete: 1, | ||
nodes: [ | ||
{ | ||
asi: 'exchange1.com', | ||
sid: '1234', | ||
hp: 1, | ||
rid: 'bid-request-1', | ||
name: 'publisher', | ||
domain: 'publisher.com' | ||
}, | ||
{ | ||
asi: 'exchange2.com', | ||
sid: 'abcd', | ||
hp: 1, | ||
rid: 'bid-request-2', | ||
name: 'intermediary', | ||
domain: 'intermediary.com' | ||
} | ||
] | ||
}; | ||
const nodesProperties = ['asi', 'sid', 'hp', 'rid', 'name', 'domain']; | ||
const expectedResult = '1.0,1!exchange1.com,1234,1,bid-request-1,publisher,publisher.com!exchange2.com,abcd,1,bid-request-2,intermediary,intermediary.com'; | ||
expect(serializeSupplyChain(schain, nodesProperties)).to.equal(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('Multiple Hops - Chain Complete, optional fields missing', () => { | ||
it('should serialize multiple hops with missing optional fields', () => { | ||
const schain = { | ||
ver: '1.0', | ||
complete: 1, | ||
nodes: [ | ||
{ | ||
asi: 'exchange1.com', | ||
sid: '1234', | ||
hp: 1 | ||
}, | ||
{ | ||
asi: 'exchange2.com', | ||
sid: 'abcd', | ||
hp: 1 | ||
} | ||
] | ||
}; | ||
const nodesProperties = ['asi', 'sid', 'hp', 'rid', 'name', 'domain']; | ||
const expectedResult = '1.0,1!exchange1.com,1234,1,,,!exchange2.com,abcd,1,,,'; | ||
expect(serializeSupplyChain(schain, nodesProperties)).to.equal(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('Multiple Hops Expected - Chain Incomplete', () => { | ||
it('should serialize multiple hops with chain incomplete', () => { | ||
const schain = { | ||
ver: '1.0', | ||
complete: 0, | ||
nodes: [ | ||
{ | ||
asi: 'exchange2.com', | ||
sid: 'abcd', | ||
hp: 1 | ||
} | ||
] | ||
}; | ||
const nodesProperties = ['asi', 'sid', 'hp', 'rid', 'name', 'domain']; | ||
const expectedResult = '1.0,0!exchange2.com,abcd,1,,,'; | ||
expect(serializeSupplyChain(schain, nodesProperties)).to.equal(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('Single Hop - Chain Complete, encoded values', () => { | ||
it('should serialize a single hop chain with encoded values', () => { | ||
const schain = { | ||
ver: '1.0', | ||
complete: 1, | ||
nodes: [ | ||
{ | ||
asi: 'exchange1.com', | ||
sid: '1234!abcd', | ||
hp: 1, | ||
rid: 'bid-request-1', | ||
name: 'publisher, Inc.', | ||
domain: 'publisher.com' | ||
} | ||
] | ||
}; | ||
const nodesProperties = ['asi', 'sid', 'hp', 'rid', 'name', 'domain']; | ||
const expectedResult = '1.0,1!exchange1.com,1234%21abcd,1,bid-request-1,publisher%2C%20Inc.,publisher.com'; | ||
expect(serializeSupplyChain(schain, nodesProperties)).to.equal(expectedResult); | ||
}); | ||
}); | ||
}); |