Skip to content

Commit

Permalink
Core: support adAuctionHeaders (#12542)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi authored Dec 5, 2024
1 parent ec420b6 commit 08467c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
12 changes: 8 additions & 4 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ export function toFetchRequest(url, data, options = {}) {
if (options.withCredentials) {
rqOpts.credentials = 'include';
}
if (options.browsingTopics && isSecureContext) {
// the Request constructor will throw an exception if the browser supports topics
// but we're not in a secure context
rqOpts.browsingTopics = true;
if (isSecureContext) {
['browsingTopics', 'adAuctionHeaders'].forEach(opt => {
// the Request constructor will throw an exception if the browser supports topics/fledge
// but we're not in a secure context
if (options[opt]) {
rqOpts[opt] = true;
}
})
}
if (options.keepalive) {
rqOpts.keepalive = true;
Expand Down
44 changes: 23 additions & 21 deletions test/spec/unit/core/ajax_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,28 +185,30 @@ describe('toFetchRequest', () => {
});
});

describe('browsingTopics', () => {
Object.entries({
'browsingTopics = true': [{browsingTopics: true}, true],
'browsingTopics = false': [{browsingTopics: false}, false],
'browsingTopics is undef': [{}, false]
}).forEach(([t, [opts, shouldBeSet]]) => {
describe(`when options has ${t}`, () => {
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
});
describe('chrome options', () => {
['browsingTopics', 'adAuctionHeaders'].forEach(option => {
Object.entries({
[`${option} = true`]: [{[option]: true}, true],
[`${option} = false`]: [{[option]: false}, false],
[`${option} undef`]: [{}, false]
}).forEach(([t, [opts, shouldBeSet]]) => {
describe(`when options has ${t}`, () => {
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
});

it(`should ${!shouldBeSet ? 'not ' : ''}be set when in a secure context`, () => {
sandbox.stub(window, 'isSecureContext').get(() => true);
toFetchRequest(EXAMPLE_URL, null, opts);
sinon.assert.calledWithMatch(dep.makeRequest, sinon.match.any, {browsingTopics: shouldBeSet ? true : undefined});
});
it(`should not be set when not in a secure context`, () => {
sandbox.stub(window, 'isSecureContext').get(() => false);
toFetchRequest(EXAMPLE_URL, null, opts);
sinon.assert.calledWithMatch(dep.makeRequest, sinon.match.any, {browsingTopics: undefined});
});
it(`should ${!shouldBeSet ? 'not ' : ''}be set when in a secure context`, () => {
sandbox.stub(window, 'isSecureContext').get(() => true);
toFetchRequest(EXAMPLE_URL, null, opts);
sinon.assert.calledWithMatch(dep.makeRequest, sinon.match.any, {[option]: shouldBeSet ? true : undefined});
});
it(`should not be set when not in a secure context`, () => {
sandbox.stub(window, 'isSecureContext').get(() => false);
toFetchRequest(EXAMPLE_URL, null, opts);
sinon.assert.calledWithMatch(dep.makeRequest, sinon.match.any, {[option]: undefined});
});
})
})
})
})
Expand Down

0 comments on commit 08467c8

Please sign in to comment.