Skip to content

Commit

Permalink
KRKPD-1667: replaces triggerPixel with fetch (#39) (#12387)
Browse files Browse the repository at this point in the history
* replaces triggerPixel with fetch

* replaces triggerPixel test
  • Loading branch information
nickllerandi authored Oct 30, 2024
1 parent 2fdecc6 commit 16642a5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
29 changes: 14 additions & 15 deletions modules/kargoBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _each, isEmpty, buildUrl, deepAccess, pick, triggerPixel, logError } from '../src/utils.js';
import { _each, isEmpty, buildUrl, deepAccess, pick, logError } from '../src/utils.js';
import { config } from '../src/config.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { getStorageManager } from '../src/storageManager.js';
Expand Down Expand Up @@ -452,21 +452,20 @@ function getRequestCount() {
}

function sendTimeoutData(auctionId, auctionTimeout) {
let params = {
aid: auctionId,
ato: auctionTimeout
};

try {
let timeoutRequestUrl = buildUrl({
protocol: 'https',
hostname: BIDDER.HOST,
pathname: BIDDER.TIMEOUT_ENDPOINT,
search: params
});
const params = { aid: auctionId, ato: auctionTimeout };
const timeoutRequestUrl = buildUrl({
protocol: 'https',
hostname: BIDDER.HOST,
pathname: BIDDER.TIMEOUT_ENDPOINT,
search: params,
});

triggerPixel(timeoutRequestUrl);
} catch (e) {}
fetch(timeoutRequestUrl, {
method: 'GET',
keepalive: true,
}).catch((e) => {
logError('Kargo: sendTimeoutData/fetch threw an error: ', e);
});
}

function getImpression(bid) {
Expand Down
46 changes: 32 additions & 14 deletions test/spec/modules/kargoBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2030,31 +2030,49 @@ describe('kargo adapter tests', function() {
});
});

describe('onTimeout', function() {
describe('onTimeout', function () {
let fetchStub;

beforeEach(function () {
sinon.stub(utils, 'triggerPixel');
fetchStub = sinon.stub(global, 'fetch').resolves(); // Stub fetch globally
});

afterEach(function () {
utils.triggerPixel.restore();
fetchStub.restore(); // Restore the original fetch function
});

it('does not call triggerPixel if timeout data is not provided', function() {
it('does not call fetch if timeout data is not provided', function () {
spec.onTimeout(null);
expect(utils.triggerPixel.callCount).to.equal(0);
expect(fetchStub.callCount).to.equal(0);
});

it('calls triggerPixel if any timeout data is provided', function() {
it('calls fetch with the correct URLs if timeout data is provided', function () {
spec.onTimeout([
{auctionId: 'test-auction-id', timeout: 400},
{auctionId: 'test-auction-id-2', timeout: 100},
{auctionId: 'test-auction-id-3', timeout: 450},
{auctionId: 'test-auction-id-4', timeout: 500},
{ auctionId: 'test-auction-id', timeout: 400 },
{ auctionId: 'test-auction-id-2', timeout: 100 },
{ auctionId: 'test-auction-id-3', timeout: 450 },
{ auctionId: 'test-auction-id-4', timeout: 500 },
]);
expect(utils.triggerPixel.calledWith('https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id&ato=400')).to.be.true;
expect(utils.triggerPixel.calledWith('https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id-2&ato=100')).to.be.true;
expect(utils.triggerPixel.calledWith('https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id-3&ato=450')).to.be.true;
expect(utils.triggerPixel.calledWith('https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id-4&ato=500')).to.be.true;

expect(fetchStub.calledWith(
'https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id&ato=400',
{ method: 'GET', keepalive: true }
)).to.be.true;

expect(fetchStub.calledWith(
'https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id-2&ato=100',
{ method: 'GET', keepalive: true }
)).to.be.true;

expect(fetchStub.calledWith(
'https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id-3&ato=450',
{ method: 'GET', keepalive: true }
)).to.be.true;

expect(fetchStub.calledWith(
'https://krk2.kargo.com/api/v1/event/timeout?aid=test-auction-id-4&ato=500',
{ method: 'GET', keepalive: true }
)).to.be.true;
});
});
});

0 comments on commit 16642a5

Please sign in to comment.