Override split test locals (#5960)

GitOrigin-RevId: 9a5372f2c4c6d79e6d77a2f72f6dc6a86437f577
This commit is contained in:
Miguel Serrano 2021-12-02 11:14:10 +01:00 committed by Copybot
parent d5bf5b0614
commit d48dbed536
2 changed files with 59 additions and 3 deletions

View file

@ -10,9 +10,17 @@ function loadAssignmentsInLocals(splitTestNames) {
req.session.cachedSplitTestAssignments = {}
}
for (const splitTestName of splitTestNames) {
const splitTest = await SplitTestCache.get(splitTestName)
if (splitTest) {
await _loadAssignmentInLocals(splitTest, req.session, res.locals)
if (req.query[splitTestName]) {
LocalsHelper.setSplitTestVariant(
res.locals,
splitTestName,
req.query[splitTestName]
)
} else {
const splitTest = await SplitTestCache.get(splitTestName)
if (splitTest) {
await _loadAssignmentInLocals(splitTest, req.session, res.locals)
}
}
}
} catch (error) {

View file

@ -101,6 +101,54 @@ describe('SplitTestMiddleware', function () {
sinon.assert.calledOnce(this.next)
})
it('variants are overridden in locals with query parameters', async function () {
this.SplitTestCache.get.withArgs('active-split-test').resolves({
name: 'active-split-test',
getCurrentVersion: () => ({
versionNumber: 1,
active: true,
}),
})
this.SplitTestV2Handler.promises.getAssignmentForSession
.withArgs(this.req.session, 'active-split-test')
.resolves({
variant: 'default',
})
const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([
'active-split-test',
])
this.req.query['active-split-test'] = 'variant'
await middleware(this.req, this.res, this.next)
assert.equal(
this.res.locals.splitTestVariants['active-split-test'],
'variant'
)
assert.deepEqual(this.req.session.cachedSplitTestAssignments, {}) // variants overriden using req.query are not cached
sinon.assert.calledOnce(this.next)
})
it('non-active split tests can be set in locals with query parameters', async function () {
const middleware = this.SplitTestMiddleware.loadAssignmentsInLocals([
'non-active-split-test',
])
this.req.query['non-active-split-test'] = 'variant'
await middleware(this.req, this.res, this.next)
assert.equal(
this.res.locals.splitTestVariants['non-active-split-test'],
'variant'
)
assert.deepEqual(this.req.session.cachedSplitTestAssignments, {}) // variants overriden using req.query are not cached
sinon.assert.calledOnce(this.next)
})
it('cached assignment in session is used', async function () {
this.req.session.cachedSplitTestAssignments = {
'ui-overhaul-1': 'cached-variant',