-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Debugging #2687
Merged
Merged
Debugging #2687
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2cdb316
new debugging functionality with bid overrides
snapwich 473c45a
update name from bidderOverrides to debugging
snapwich d48c7c4
change sessionStorage to window.sessionStorage
snapwich 9c5ba91
solve sinon stubbing sessionStorage issue with dependency injection
snapwich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
|
||
import { config } from 'src/config'; | ||
import { logMessage as utilsLogMessage, logWarn as utilsLogWarn } from 'src/utils'; | ||
import { addBidResponse } from 'src/auction'; | ||
|
||
const OVERRIDE_KEY = '$$PREBID_GLOBAL$$:debugging'; | ||
|
||
export let boundHook; | ||
|
||
function logMessage(msg) { | ||
utilsLogMessage('DEBUG: ' + msg); | ||
} | ||
|
||
function logWarn(msg) { | ||
utilsLogWarn('DEBUG: ' + msg); | ||
} | ||
|
||
function enableOverrides(overrides, fromSession = false) { | ||
config.setConfig({'debug': true}); | ||
logMessage(`bidder overrides enabled${fromSession ? ' from session' : ''}`); | ||
|
||
if (boundHook) { | ||
addBidResponse.removeHook(boundHook); | ||
} | ||
|
||
boundHook = addBidResponseHook.bind(null, overrides); | ||
addBidResponse.addHook(boundHook, 5); | ||
} | ||
|
||
export function disableOverrides() { | ||
if (boundHook) { | ||
addBidResponse.removeHook(boundHook); | ||
logMessage('bidder overrides disabled'); | ||
} | ||
} | ||
|
||
export function addBidResponseHook(overrides, adUnitCode, bid, next) { | ||
if (Array.isArray(overrides.bidders) && overrides.bidders.indexOf(bid.bidderCode) === -1) { | ||
logWarn(`bidder '${bid.bidderCode}' excluded from auction by bidder overrides`); | ||
return; | ||
} | ||
|
||
if (Array.isArray(overrides.bids)) { | ||
overrides.bids.forEach(overrideBid => { | ||
if (overrideBid.bidder && overrideBid.bidder !== bid.bidderCode) { | ||
return; | ||
} | ||
if (overrideBid.adUnitCode && overrideBid.adUnitCode !== adUnitCode) { | ||
return; | ||
} | ||
|
||
bid = Object.assign({}, bid); | ||
|
||
Object.keys(overrideBid).filter(key => ['bidder', 'adUnitCode'].indexOf(key) === -1).forEach((key) => { | ||
let value = overrideBid[key]; | ||
logMessage(`bidder overrides changed '${adUnitCode}/${bid.bidderCode}' bid.${key} from '${bid[key]}' to '${value}'`); | ||
bid[key] = value; | ||
}); | ||
}); | ||
} | ||
|
||
next(adUnitCode, bid); | ||
} | ||
|
||
export function getConfig(debugging) { | ||
if (!debugging.enabled) { | ||
disableOverrides(); | ||
try { | ||
window.sessionStorage.removeItem(OVERRIDE_KEY); | ||
} catch (e) {} | ||
} else { | ||
try { | ||
window.sessionStorage.setItem(OVERRIDE_KEY, JSON.stringify(debugging)); | ||
} catch (e) {} | ||
enableOverrides(debugging); | ||
} | ||
} | ||
config.getConfig('debugging', ({debugging}) => getConfig(debugging)); | ||
|
||
export function sessionLoader(storage = window.sessionStorage) { | ||
let overrides; | ||
try { | ||
overrides = JSON.parse(storage.getItem(OVERRIDE_KEY)); | ||
} catch (e) { | ||
} | ||
if (overrides) { | ||
enableOverrides(overrides, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
|
||
import { expect } from 'chai'; | ||
import { sessionLoader, addBidResponseHook, getConfig, disableOverrides, boundHook } from 'src/debugging'; | ||
import { addBidResponse } from 'src/auction'; | ||
import { config } from 'src/config'; | ||
|
||
describe('bid overrides', () => { | ||
let sandbox; | ||
|
||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
}); | ||
|
||
afterEach(() => { | ||
window.sessionStorage.clear(); | ||
sandbox.restore(); | ||
}); | ||
|
||
describe('initialization', () => { | ||
beforeEach(() => { | ||
sandbox.stub(config, 'setConfig'); | ||
}); | ||
|
||
afterEach(() => { | ||
disableOverrides(); | ||
}); | ||
|
||
it('should happen when enabled with setConfig', () => { | ||
getConfig({ | ||
enabled: true | ||
}); | ||
|
||
expect(addBidResponse.hasHook(boundHook)).to.equal(true); | ||
}); | ||
|
||
it('should happen when configuration found in sessionStorage', () => { | ||
sessionLoader({ | ||
getItem: () => ('{"enabled": true}') | ||
}); | ||
expect(addBidResponse.hasHook(boundHook)).to.equal(true); | ||
}); | ||
|
||
it('should not throw if sessionStorage is inaccessible', () => { | ||
expect(() => { | ||
sessionLoader({ | ||
getItem() { | ||
throw new Error('test'); | ||
} | ||
}); | ||
}).not.to.throw(); | ||
}); | ||
}); | ||
|
||
describe('hook', () => { | ||
let mockBids; | ||
let bids; | ||
|
||
beforeEach(() => { | ||
let baseBid = { | ||
'bidderCode': 'rubicon', | ||
'width': 970, | ||
'height': 250, | ||
'statusMessage': 'Bid available', | ||
'mediaType': 'banner', | ||
'source': 'client', | ||
'currency': 'USD', | ||
'cpm': 0.5, | ||
'ttl': 300, | ||
'netRevenue': false, | ||
'adUnitCode': '/19968336/header-bid-tag-0' | ||
}; | ||
mockBids = []; | ||
mockBids.push(baseBid); | ||
mockBids.push(Object.assign({}, baseBid, { | ||
bidderCode: 'appnexus' | ||
})); | ||
|
||
bids = []; | ||
}); | ||
|
||
function run(overrides) { | ||
mockBids.forEach(bid => { | ||
addBidResponseHook(overrides, bid.adUnitCode, bid, (adUnitCode, bid) => { | ||
bids.push(bid); | ||
}) | ||
}); | ||
} | ||
|
||
it('should allow us to exclude bidders', () => { | ||
run({ | ||
enabled: true, | ||
bidders: ['appnexus'] | ||
}); | ||
|
||
expect(bids.length).to.equal(1); | ||
expect(bids[0].bidderCode).to.equal('appnexus'); | ||
}); | ||
|
||
it('should allow us to override all bids', () => { | ||
run({ | ||
enabled: true, | ||
bids: [{ | ||
cpm: 2 | ||
}] | ||
}); | ||
|
||
expect(bids.length).to.equal(2); | ||
expect(bids[0].cpm).to.equal(2); | ||
expect(bids[1].cpm).to.equal(2); | ||
}); | ||
|
||
it('should allow us to override bids by bidder', () => { | ||
run({ | ||
enabled: true, | ||
bids: [{ | ||
bidder: 'rubicon', | ||
cpm: 2 | ||
}] | ||
}); | ||
|
||
expect(bids.length).to.equal(2); | ||
expect(bids[0].cpm).to.equal(2); | ||
expect(bids[1].cpm).to.equal(0.5); | ||
}); | ||
|
||
it('should allow us to override bids by adUnitCode', () => { | ||
mockBids[1].adUnitCode = 'test'; | ||
|
||
run({ | ||
enabled: true, | ||
bids: [{ | ||
adUnitCode: 'test', | ||
cpm: 2 | ||
}] | ||
}); | ||
|
||
expect(bids.length).to.equal(2); | ||
expect(bids[0].cpm).to.equal(0.5); | ||
expect(bids[1].cpm).to.equal(2); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra line break here.