Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Default Content-Type for POST Requests to 'application/json' #1681

Merged
merged 15 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/adapters/bidderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import { logWarn, logError, parseQueryStringParameters, delayExecution } from 's
* @property {('GET'|'POST')} method The type of request which this is.
* @property {string} url The endpoint for the request. For example, "//bids.example.com".
* @property {string|object} data Data to be sent in the request.
* @property {object} options Content-Type set in the header of the bid request, overrides default 'text/plain'.
* If this is a GET request, they'll become query params. If it's a POST request, they'll be added to the body.
* Strings will be added as-is. Objects will be unpacked into query params based on key/value mappings, or
* JSON-serialized into the Request body.
Expand Down Expand Up @@ -233,10 +234,10 @@ export function newBidder(spec) {
error: onFailure
},
undefined,
{
Object.assign({
method: 'GET',
withCredentials: true
}
}, request.options)
);
break;
case 'POST':
Expand All @@ -247,11 +248,11 @@ export function newBidder(spec) {
error: onFailure
},
typeof request.data === 'string' ? request.data : JSON.stringify(request.data),
{
Object.assign({
method: 'POST',
contentType: 'text/plain',
withCredentials: true
}
}, request.options)
);
break;
default:
Expand Down
49 changes: 49 additions & 0 deletions test/spec/unit/core/bidderFactory_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ describe('bidders created by newBidder', () => {
});
});

it('should make the appropriate POST request when options are passed', () => {
const bidder = newBidder(spec);
const url = 'test.url.com';
const data = { arg: 2 };
const options = { contentType: 'application/json'};
spec.isBidRequestValid.returns(true);
spec.buildRequests.returns({
method: 'POST',
url: url,
data: data,
options: options
});

bidder.callBids(MOCK_BIDS_REQUEST);

expect(ajaxStub.calledOnce).to.equal(true);
expect(ajaxStub.firstCall.args[0]).to.equal(url);
expect(ajaxStub.firstCall.args[2]).to.equal(JSON.stringify(data));
expect(ajaxStub.firstCall.args[3]).to.deep.equal({
method: 'POST',
contentType: 'application/json',
withCredentials: true
});
});

it('should make the appropriate GET request', () => {
const bidder = newBidder(spec);
const url = 'test.url.com';
Expand All @@ -169,6 +194,30 @@ describe('bidders created by newBidder', () => {
});
});

it('should make the appropriate GET request when options are passed', () => {
const bidder = newBidder(spec);
const url = 'test.url.com';
const data = { arg: 2 };
const opt = { withCredentials: false }
spec.isBidRequestValid.returns(true);
spec.buildRequests.returns({
method: 'GET',
url: url,
data: data,
options: opt
});

bidder.callBids(MOCK_BIDS_REQUEST);

expect(ajaxStub.calledOnce).to.equal(true);
expect(ajaxStub.firstCall.args[0]).to.equal(`${url}?arg=2&`);
expect(ajaxStub.firstCall.args[2]).to.be.undefined;
expect(ajaxStub.firstCall.args[3]).to.deep.equal({
method: 'GET',
withCredentials: false
});
});

it('should make multiple calls if the spec returns them', () => {
const bidder = newBidder(spec);
const url = 'test.url.com';
Expand Down