Skip to content

Commit

Permalink
updated tests to not require exposure of new method
Browse files Browse the repository at this point in the history
  • Loading branch information
monofonik committed Jan 21, 2025
1 parent 2c43c3a commit 07e2a3c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
6 changes: 6 additions & 0 deletions modules/gumgumBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ function _getBrowserParams(topWindowUrl, mosttopLocation) {
return browserParams;
}

/**
* Some bid responses have been observed to contain non-Latin characters, which causes the browser
* to throw an error when trying to decode the base64 string using only the atob function.
* @param {*} str
* @returns {string} base64 encoded string
*/
function safeEncodeBase64(str) {
const encoder = new TextEncoder();

Expand Down
51 changes: 34 additions & 17 deletions test/spec/modules/gumgumBidAdapter_spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BANNER, VIDEO } from 'src/mediaTypes.js';
import { safeEncodeBase64, spec } from 'modules/gumgumBidAdapter.js';

import { config } from 'src/config.js';
import { expect } from 'chai';
import { newBidder } from 'src/adapters/bidderFactory.js';
import { spec } from 'modules/gumgumBidAdapter.js';

const ENDPOINT = 'https://g2.gumgum.com/hbid/imp';
const JCSI = { t: 0, rq: 8, pbv: '$prebid.version$' }
Expand Down Expand Up @@ -1034,7 +1034,39 @@ describe('gumgumAdapter', function () {
const videoBidResponse = spec.interpretResponse({ body: serverResponse }, { ...bidRequest, data: { pi: 7 } })[0];
expect(videoBidResponse.vastXml).to.exist;
});
})

it('should encode a string with an emdash character without failing', function () {
let serverResponse = {
'ad': {
'id': 2065333,
'height': 90,
'ipd': 2000,
'markup': 'This is a test string with an emdash – character', // non-Latin character
'ii': true,
'du': null,
'price': 1,
'zi': 0,
'impurl': 'http://g2.gumgum.com/ad/view',
'clsurl': 'http://g2.gumgum.com/ad/close'
},
'pag': {
't': 'ggumtest',
'pvid': 'aa8bbb65-427f-4689-8cee-e3eed0b89eec',
},
'thms': 10000
}

let result = spec.interpretResponse({ body: serverResponse }, bidRequest);
console.log(JSON.stringify(result));
expect(result[0].ad).to.exist;
expect(result[0].ad).to.contain('This is a test string with an emdash – character');

// Confirm that native btoa would have thrown an error on this response if used
const nativeBtoa = () => btoa(JSON.stringify(serverResponse));
expect(nativeBtoa).to.throw(Error);
});
});

describe('getUserSyncs', function () {
const syncOptions = {
'iframeEnabled': 'true'
Expand All @@ -1057,19 +1089,4 @@ describe('gumgumAdapter', function () {
expect(result[0].type).to.equal('image')
expect(result[1].type).to.equal('iframe')
})

describe('safeEncodeBase64', function() {
it('should encode a string with an emdash character without failing', function() {
const testString = 'This is a test string with an emdash – character.';
const nativeBtoa = () => btoa(testString);
const safeEncodedString = safeEncodeBase64(testString);

// Check that native btoa throws an error
expect(nativeBtoa).to.throw(Error);

// Check that safeEncodeBase64 does not throw an error and returns a valid base64 string
expect(safeEncodedString).to.be.a('string');
expect(() => atob(safeEncodedString)).to.not.throw(Error);
});
});
});

0 comments on commit 07e2a3c

Please sign in to comment.