Skip to content

Commit

Permalink
fix(web-server): Update config on every request
Browse files Browse the repository at this point in the history
This updates the `client`, `customContextFile` and
`customDebugFile` config properties on every request in the
karma middleware.

Closes #1972
  • Loading branch information
dignifiedquire committed Mar 7, 2016
1 parent b765c33 commit 8ef475f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 12 deletions.
26 changes: 19 additions & 7 deletions lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var path = require('path')
var util = require('util')
var url = require('url')

var log = require('../logger').create('middleware:karma')

var urlparse = function (urlStr) {
var urlObj = url.parse(urlStr, true)
urlObj.query = urlObj.query || {}
Expand Down Expand Up @@ -59,10 +61,20 @@ var getXUACompatibleUrl = function (url) {
return value
}

var createKarmaMiddleware = function (filesPromise, serveStaticFile, serveFile,
/* config.basePath */ basePath, /* config.urlRoot */ urlRoot, /* config.client */ client,
/* config.customContextFile */ customContextFile, /* config.customDebugFile */ customDebugFile) {
var createKarmaMiddleware = function (
filesPromise,
serveStaticFile,
serveFile,
injector,
/* config.basePath */ basePath,
/* config.urlRoot */ urlRoot
) {
return function (request, response, next) {
// These config values should be up to date on every request
var client = injector.get('config.client')
var customContextFile = injector.get('config.customContextFile')
var customDebugFile = injector.get('config.customDebugFile')

var requestUrl = request.normalizedUrl.replace(/\?.*/, '')

// redirect /__karma__ to /__karma__ (trailing slash)
Expand Down Expand Up @@ -110,13 +122,17 @@ var createKarmaMiddleware = function (filesPromise, serveStaticFile, serveFile,
return filesPromise.then(function (files) {
var fileServer
var requestedFileUrl
log.debug('custom files', customContextFile, customDebugFile)
if (isRequestingContextFile && customContextFile) {
log.debug('Serving customContextFile %s', customContextFile)
fileServer = serveFile
requestedFileUrl = customContextFile
} else if (isRequestingDebugFile && customDebugFile) {
log.debug('Serving customDebugFile %s', customDebugFile)
fileServer = serveFile
requestedFileUrl = customDebugFile
} else {
log.debug('Serving static request %s', requestUrl)
fileServer = serveStaticFile
requestedFileUrl = requestUrl
}
Expand Down Expand Up @@ -197,9 +213,5 @@ var createKarmaMiddleware = function (filesPromise, serveStaticFile, serveFile,
}
}

createKarmaMiddleware.$inject = ['filesPromise', 'serveStaticFile', 'serveFile',
'config.basePath', 'config.urlRoot', 'config.client', 'config.customContextFile',
'config.customDebugFile']

// PUBLIC API
exports.create = createKarmaMiddleware
1 change: 1 addition & 0 deletions lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var createWebServer = function (injector, emitter, fileList) {

var proxyMiddlewareInstance = injector.invoke(proxyMiddleware.create)

log.debug('Instantiating middleware')
var handler = connect()
.use(injector.invoke(runnerMiddleware.create))
.use(injector.invoke(stopperMiddleware.create))
Expand Down
92 changes: 87 additions & 5 deletions test/unit/middleware/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,31 @@ describe('middleware.karma', () => {

var handler = serveFile = filesDeferred = nextSpy = response = null

var clientConfig = {foo: 'bar'}
var injector = {
get (val) {
switch (val) {
case 'config.client':
return clientConfig
default:
return null
}
}
}

beforeEach(() => {
var clientConfig = {foo: 'bar'}
nextSpy = sinon.spy()
response = new HttpResponseMock()
filesDeferred = helper.defer()
serveFile = createServeFile(fsMock, '/karma/static')
handler = createKarmaMiddleware(filesDeferred.promise, serveFile, null, '/base/path', '/__karma__/', clientConfig)
handler = createKarmaMiddleware(
filesDeferred.promise,
serveFile,
null,
injector,
'/base/path',
'/__karma__/'
)
})

// helpers
Expand Down Expand Up @@ -92,7 +110,14 @@ describe('middleware.karma', () => {
})

it('should serve client.html', (done) => {
handler = createKarmaMiddleware(null, serveFile, null, '/base', '/')
handler = createKarmaMiddleware(
null,
serveFile,
null,
injector,
'/base',
'/'
)

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
Expand All @@ -104,7 +129,14 @@ describe('middleware.karma', () => {
})

it('should serve /?id=xxx', (done) => {
handler = createKarmaMiddleware(null, serveFile, null, '/base', '/')
handler = createKarmaMiddleware(
null,
serveFile,
null,
injector,
'/base',
'/'
)

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
Expand All @@ -116,7 +148,14 @@ describe('middleware.karma', () => {
})

it('should serve /?x-ua-compatible with replaced values', (done) => {
handler = createKarmaMiddleware(null, serveFile, null, '/base', '/')
handler = createKarmaMiddleware(
null,
serveFile,
null,
injector,
'/base',
'/'
)

response.once('end', () => {
expect(nextSpy).not.to.have.been.called
Expand Down Expand Up @@ -351,4 +390,47 @@ describe('middleware.karma', () => {
done()
})
})

it('should update handle updated configs', (done) => {
let i = 0
handler = createKarmaMiddleware(
filesDeferred.promise,
serveFile,
null,
{
get (val) {
if (val === 'config.client') {
i++
if (i === 1) {
return {foo: 'bar'}
} else {
return {foo: 'baz'}
}
} else {
return null
}
}
},
'/base/path',
'/__karma__/'
)

includedFiles([
new MockFile('/first.js')
])
fsMock._touchFile('/karma/static/debug.html', 1, '%CLIENT_CONFIG%')

response.once('end', () => {
expect(response).to.beServedAs(200, 'window.__karma__.config = {"foo":"bar"};\n')

response = new HttpResponseMock()
callHandlerWith('/__karma__/debug.html')
response.once('end', () => {
expect(response).to.beServedAs(200, 'window.__karma__.config = {"foo":"baz"};\n')
done()
})
})

callHandlerWith('/__karma__/debug.html')
})
})

0 comments on commit 8ef475f

Please sign in to comment.