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

BaseAdapter for the Prebid 0.x -> 1.x transition #1494

Merged
merged 22 commits into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c1080dd
Added a base adapter for single-request adapters, and ported the appn…
dbemiller Aug 17, 2017
70fcf51
Renamed the SingleRequestBidder to BidderFactory. Updated it to handl…
dbemiller Aug 18, 2017
211e7fc
Added a unit test for the delayExecution function.
dbemiller Aug 18, 2017
6246eaf
Merged from master. Fixed conflicts.
dbemiller Aug 18, 2017
d509150
Made newBidder a default import. Added some unit tests.
dbemiller Aug 18, 2017
8d4ebc1
Added more tests.
dbemiller Aug 21, 2017
7ffc16f
Merge branch 'master' of https://github.com/prebid/Prebid.js into sin…
dbemiller Aug 21, 2017
1f23044
Added more tests, and fixed a few bugs.
dbemiller Aug 22, 2017
8d1efa6
Merged from master. Fixed a conflict.
dbemiller Aug 23, 2017
a627a43
Changed an error to a log message. Fixed a small bug.
dbemiller Aug 23, 2017
ff99ff6
Merged from master, and fixed conflicts.
dbemiller Aug 31, 2017
c246aa6
Did the no-brainer improvements from PR comments.
dbemiller Aug 31, 2017
6c80c3d
Added spec-level support for aliases and mediaTypes. Aliases may stil…
dbemiller Aug 31, 2017
37399c7
Added support for aliases. Added more tests
dbemiller Sep 1, 2017
1b42995
Cleaned up some unnecessary code.
dbemiller Sep 1, 2017
5c3d645
Removed the GET/POST constants. Fixed some typos, and renamed some Re…
dbemiller Sep 1, 2017
31899f4
Merged from master. Fixed conflicts.
dbemiller Sep 6, 2017
735f2bc
Re-added some code for outstream rendering, which was apparently lost…
dbemiller Sep 6, 2017
dd4e595
Removed confusing use of this
dbemiller Sep 6, 2017
25f822a
Fixed lint error
dbemiller Sep 6, 2017
e63f2d6
Moved JSON parsing into the bidderFactory, and moved the JSDocs to th…
dbemiller Sep 6, 2017
13d3911
Removed placementCode from everywhere I could.
dbemiller Sep 7, 2017
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
Prev Previous commit
Next Next commit
Made newBidder a default import. Added some unit tests.
  • Loading branch information
dbemiller committed Aug 18, 2017
commit d509150e47a8b187b2e5326e378d6706e9b50aca
2 changes: 1 addition & 1 deletion modules/appnexusAstBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Renderer } from 'src/Renderer';
import * as utils from 'src/utils';
import adaptermanager from 'src/adaptermanager';
import { newBidder } from 'src/adapters/bidderFactory';
import newBidder from 'src/adapters/bidderFactory';
import { POST } from '../src/ajax';

const BIDDER_CODE = 'appnexusAst';
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/bidderFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { logWarn, logError, parseQueryStringParameters, delayExecution } from 's
*
* @param {BidderSpec} spec An object containing the bare-bones functions we need to make a Bidder.
*/
export function newBidder(spec) {
export default function newBidder(spec) {
return Object.assign(new Adapter(spec.code), {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to figure out how aliasing is going to work with this new pattern. Currently I have it calling new on the .constructor link of an existing instance. In this case it would be an instance of just plain Adapter, which means it wouldn't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support for these. I'm not sure this is the best way, though... so let me know if you have better ideas.

If we think this API is general enough to be the only base class, I might re-organize the code a bit so that "bidder creation" is in a separate file from "bidder registration".

I still haven't heard anyone weigh in on whether or not this handles all the 1.0 use-cases, though, or whether i'm overly-constraining adapters with it.

callBids: function(bidsRequest) {
if (!bidsRequest.bids || !bidsRequest.bids.filter) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like an array check. Would be more clear as if (!Array.isArray(bidsRequest.bids) ) {

Expand Down
127 changes: 127 additions & 0 deletions test/spec/unit/core/bidderFactory_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import newBidder from 'src/adapters/bidderFactory';
import bidmanager from 'src/bidmanager';
import * as ajax from 'src/ajax';
import { expect } from 'chai';

const CODE = 'sampleBidder';
const MOCK_BIDS_REQUEST = {
bids: [
{
placementCode: 'mock/placement',
params: {
param: 5
}
},
{
placementCode: 'mock/placement2',
params: {
badParam: 6
}
}
]
}

describe('The bidder factory', () => {
let spec;
let bidder;
let addBidRequestStub;
let ajaxMock;

beforeEach(() => {
spec = {
code: CODE,
areParamsValid: sinon.stub(),
buildRequests: sinon.stub(),
interpretResponse: sinon.stub()
};
bidder = newBidder(spec);
addBidRequestStub = sinon.stub(bidmanager, 'addBidResponse');
ajaxMock = sinon.mock(ajax);
});

afterEach(() => {
addBidRequestStub.restore();
ajaxMock.restore();
})

it('should handle bad bid requests gracefully', () => {
ajaxMock.expects('ajax').never();

bidder.callBids({});
bidder.callBids({ bids: 'nothing useful' });
expect(spec.areParamsValid.called).to.equal(false);
expect(spec.buildRequests.called).to.equal(false);
expect(spec.interpretResponse.called).to.equal(false);
});

it('should call buildRequests(bidRequest) the params are valid.', () => {
spec.areParamsValid.returns(true);
spec.buildRequests.returns([]);
ajaxMock.expects('ajax').never();

bidder.callBids(MOCK_BIDS_REQUEST);

expect(spec.areParamsValid.calledTwice).to.equal(true);
expect(spec.buildRequests.calledOnce).to.equal(true);
expect(spec.buildRequests.firstCall.args[0]).to.deep.equal(MOCK_BIDS_REQUEST.bids);
});

it('should not call buildRequests the params are invalid.', () => {
spec.areParamsValid.returns(false);
spec.buildRequests.returns([]);
ajaxMock.expects('ajax').never();

bidder.callBids(MOCK_BIDS_REQUEST);

expect(spec.areParamsValid.calledTwice).to.equal(true);
expect(spec.buildRequests.called).to.equal(false);
});

it('should filter out invalid bids before calling buildRequests.', () => {
spec.areParamsValid.onFirstCall().returns(true);
spec.areParamsValid.onSecondCall().returns(false);
spec.buildRequests.returns([]);
ajaxMock.expects('ajax').never();

bidder.callBids(MOCK_BIDS_REQUEST);

expect(spec.areParamsValid.calledTwice).to.equal(true);
expect(spec.buildRequests.calledOnce).to.equal(true);
expect(spec.buildRequests.firstCall.args[0]).to.deep.equal([MOCK_BIDS_REQUEST.bids[0]]);
});

it('should make the appropriate POST requests.', () => {
const url = 'test.url.com';
const data = { arg: 2 };
spec.areParamsValid.returns(true);
spec.buildRequests.returns({
type: 'POST',
endpoint: url,
data: data
});
ajaxMock.expects('ajax').once().withArgs(url, sinon.match.object, JSON.stringify(data), {
method: 'POST',
contentType: 'text/plain',
withCredentials: true
});

bidder.callBids(MOCK_BIDS_REQUEST);
});

it('should make the appropriate GET requests.', () => {
const url = 'test.url.com';
const data = { arg: 2 };
spec.areParamsValid.returns(true);
spec.buildRequests.returns({
type: 'GET',
endpoint: url,
data: data
});
ajaxMock.expects('ajax').once().withArgs(`${url}?arg=2&`, sinon.match.object, undefined, {
method: 'GET',
withCredentials: true
});

bidder.callBids(MOCK_BIDS_REQUEST);
});
});