Skip to content

Commit

Permalink
fixes for bugs in test suite (#810)
Browse files Browse the repository at this point in the history
* use utils log methods

* Use ajax library for cross-browser compatibility

* Remove assertions that always fail on IE9

* Wrap XHR construction in try/catch block

* Attach handler inline to avoid JSHint error about creating functions inside of a block
  • Loading branch information
Nate Cozi authored and Matt Kendall committed Dec 1, 2016
1 parent 5a1541c commit fbfdce6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/adapters/aol.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const AolAdapter = function AolAdapter() {
let server;

if (!SERVER_MAP.hasOwnProperty(regionParam)) {
console.warn(`Unknown region '${regionParam}' for AOL bidder.`);
utils.logWarn(`Unknown region '${regionParam}' for AOL bidder.`);
regionParam = 'us'; // Default region.
}

Expand Down
18 changes: 5 additions & 13 deletions src/adapters/conversant.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var VERSION = '2.0.1',
utils = require('../utils.js'),
bidfactory = require('../bidfactory.js'),
bidmanager = require('../bidmanager.js'),
adloader = require('../adloader');
adloader = require('../adloader'),
ajax = require('../ajax').ajax;

/**
* Adapter for requesting bids from Conversant
Expand All @@ -31,17 +32,6 @@ var ConversantAdapter = function () {
}
};

var httpPOSTAsync = function (url, data) {
var xmlHttp = new w.XMLHttpRequest();

xmlHttp.onload = function () {
appendScript(xmlHttp.responseText);
};
xmlHttp.open('POST', url, true); // true for asynchronous
xmlHttp.withCredentials = true;
xmlHttp.send(data);
};

var getDNT = function () {
return n.doNotTrack === '1' || w.doNotTrack === '1' || n.msDoNotTrack === '1' || n.doNotTrack === 'yes';
};
Expand Down Expand Up @@ -120,7 +110,9 @@ var ConversantAdapter = function () {
};

var url = secure ? 'https:' + conversantUrl : location.protocol + conversantUrl;
httpPOSTAsync(url, JSON.stringify(conversantBidReqs));
ajax(url, appendScript, JSON.stringify(conversantBidReqs), {
withCredentials : true
});
};

var addEmptyBidResponses = function (placementsWithBidsBack) {
Expand Down
99 changes: 49 additions & 50 deletions src/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,63 @@ const XHR_DONE = 4;
*/

export function ajax(url, callback, data, options = {}) {
try {
let x;
let useXDomainRequest = false;
let method = options.method || (data ? 'POST' : 'GET');

let x;
let useXDomainRequest =false;
let method = options.method || (data ? 'POST' : 'GET');
if(!window.XMLHttpRequest){
useXDomainRequest = true;
}
else{
x = new window.XMLHttpRequest();
if(x.responseType === undefined){
if (!window.XMLHttpRequest) {
useXDomainRequest = true;
} else{
x = new window.XMLHttpRequest();
if (x.responseType === undefined) {
useXDomainRequest = true;
}
}
}

if (useXDomainRequest) {
x = new window.XDomainRequest();
x.onload = function () {
callback(x.responseText, x);
};

// http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
x.onerror = function () {
utils.logMessage('xhr onerror');
};
x.ontimeout = function () {
utils.logMessage('xhr timeout');
};
x.onprogress = function() {
utils.logMessage('xhr onprogress');
};

} else {
x.onreadystatechange = handler;
}

if (method === 'GET' && data) {
let urlInfo = parseURL(url);
Object.assign(urlInfo.search, data);
url = formatURL(urlInfo);
}

x.open(method, url);
if (useXDomainRequest) {
x = new window.XDomainRequest();
x.onload = function () {
callback(x.responseText, x);
};

if (!useXDomainRequest) {
if (options.withCredentials) {
x.withCredentials = true;
// http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
x.onerror = function () {
utils.logMessage('xhr onerror');
};
x.ontimeout = function () {
utils.logMessage('xhr timeout');
};
x.onprogress = function() {
utils.logMessage('xhr onprogress');
};
} else {
x.onreadystatechange = function () {
if (x.readyState === XHR_DONE && callback) {
callback(x.responseText, x);
}
};
}
if (options.preflight) {
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

if (method === 'GET' && data) {
let urlInfo = parseURL(url);
Object.assign(urlInfo.search, data);
url = formatURL(urlInfo);
}
x.setRequestHeader('Content-Type', options.contentType || 'text/plain');
}
x.send(method === 'POST' && data);

function handler() {
if (x.readyState === XHR_DONE && callback) {
callback(x.responseText, x);
x.open(method, url);

if (!useXDomainRequest) {
if (options.withCredentials) {
x.withCredentials = true;
}
if (options.preflight) {
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
}
x.setRequestHeader('Content-Type', options.contentType || 'text/plain');
}
x.send(method === 'POST' && data);
} catch (error) {
utils.logError('xhr construction', error);
}

}
24 changes: 12 additions & 12 deletions test/spec/adapters/aol_spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import _ from 'lodash';
import { cloneDeep } from 'lodash';
import * as utils from 'src/utils';
import AolAdapter from 'src/adapters/aol';
import bidmanager from 'src/bidmanager';
Expand Down Expand Up @@ -29,7 +29,7 @@ const DEFAULT_PUBAPI_RESPONSE = {
"id": 1,
"impid": "245730051428950632",
"price": 0.09,
"adm": "<script>console.log('ad');</script>",
"adm": "<script>logInfo('ad');</script>",
"crid": "0",
"h": 90,
"w": 728,
Expand All @@ -45,7 +45,7 @@ describe('AolAdapter', () => {
beforeEach(() => adapter = new AolAdapter());

function createBidderRequest({bids, params} = {}) {
var bidderRequest = _.cloneDeep(DEFAULT_BIDDER_REQUEST);
var bidderRequest = cloneDeep(DEFAULT_BIDDER_REQUEST);
if (bids && Array.isArray(bids)) {
bidderRequest.bids = bids;
}
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('AolAdapter', () => {
"bid": [{
"id": 1,
"impid": "245730051428950632",
"adm": "<script>console.log('ad');</script>",
"adm": "<script>logInfo('ad');</script>",
"crid": "0",
"h": 90,
"w": 728,
Expand All @@ -326,7 +326,7 @@ describe('AolAdapter', () => {
"id": 1,
"impid": "245730051428950632",
"price": 0.09,
"adm": "<script>console.log('ad');</script>",
"adm": "<script>logInfo('ad');</script>",
"crid": "12345",
"h": 90,
"w": 728,
Expand All @@ -338,7 +338,7 @@ describe('AolAdapter', () => {
server.respond();
expect(bidmanager.addBidResponse.calledOnce).to.be.true;
var bidResponse = bidmanager.addBidResponse.firstCall.args[1];
expect(bidResponse.ad).to.equal("<script>console.log('ad');</script>");
expect(bidResponse.ad).to.equal("<script>logInfo('ad');</script>");
expect(bidResponse.cpm).to.equal(0.09);
expect(bidResponse.width).to.equal(728);
expect(bidResponse.height).to.equal(90);
Expand All @@ -355,7 +355,7 @@ describe('AolAdapter', () => {
"id": 1,
"impid": "245730051428950632",
"price": 0.09,
"adm": "<script>console.log('ad');</script>",
"adm": "<script>logInfo('ad');</script>",
"crid": "12345",
"h": 90,
"w": 728,
Expand All @@ -371,7 +371,7 @@ describe('AolAdapter', () => {
expect(bidmanager.addBidResponse.calledOnce).to.be.true;
var bidResponse = bidmanager.addBidResponse.firstCall.args[1];
expect(bidResponse.ad).to.equal(
"<script>console.log('ad');</script>" +
"<script>logInfo('ad');</script>" +
"<script>document.write('<img src=\"pixel.gif\">');</script>"
);
});
Expand All @@ -386,7 +386,7 @@ describe('AolAdapter', () => {
"impid": "245730051428950632",
"dealid": "12345",
"price": 0.09,
"adm": "<script>console.log('ad');</script>",
"adm": "<script>logInfo('ad');</script>",
"crid": "12345",
"h": 90,
"w": 728,
Expand All @@ -413,7 +413,7 @@ describe('AolAdapter', () => {
"impid": "245730051428950632",
"dealid": "12345",
"price": 0.09,
"adm": "<script>console.log('ad');</script>",
"adm": "<script>logInfo('ad');</script>",
"crid": "12345",
"h": 90,
"w": 728,
Expand Down Expand Up @@ -444,8 +444,8 @@ describe('AolAdapter', () => {
afterEach(() => {
$$PREBID_GLOBAL$$.bidderSettings = bidderSettingsBackup;
server.restore();
if (console.warn.restore) {
console.warn.restore();
if (utils.logWarn.restore) {
utils.logWarn.restore();
}
});

Expand Down
5 changes: 2 additions & 3 deletions test/spec/adapters/conversant_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,13 @@ describe('Conversant adapter tests', function () {
var resp = [200, {'Content-type': 'text/javascript'}, '$$PREBID_GLOBAL$$.conversantResponse(\'' + JSON.stringify(bidResponse) + '\')'];
server.respondWith('POST', new RegExp('media.msg.dotomi.com/s2s/header'), resp);
});

it('Should contain valid request header properties', function () {
adapter.callBids(bidderRequest);
server.respond();

var request = server.requests[0];
expect(request.requestBody).to.not.be.empty;
expect(request.withCredentials).to.equal(true); // allows for request cookies
});
});
});
4 changes: 0 additions & 4 deletions test/spec/adapters/rubicon_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,6 @@ describe('the rubicon adapter', () => {

let request = xhr.requests[0];

expect(request instanceof sinon.FakeXMLHttpRequest).to.equal(true);

expect(request.withCredentials).to.equal(true);

let [path, query] = request.url.split('?');
query = parseQuery(query);

Expand Down

0 comments on commit fbfdce6

Please sign in to comment.