From 8ef475f7d1a06562e659347dcc53c2e474cbddc7 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Mon, 7 Mar 2016 13:26:28 +0100 Subject: [PATCH] fix(web-server): Update config on every request This updates the `client`, `customContextFile` and `customDebugFile` config properties on every request in the karma middleware. Closes #1972 --- lib/middleware/karma.js | 26 ++++++--- lib/web-server.js | 1 + test/unit/middleware/karma.spec.js | 92 ++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 12 deletions(-) diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index 2e603b382..0c965a521 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -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 || {} @@ -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) @@ -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 } @@ -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 diff --git a/lib/web-server.js b/lib/web-server.js index dc200fda6..1405032c0 100644 --- a/lib/web-server.js +++ b/lib/web-server.js @@ -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)) diff --git a/test/unit/middleware/karma.spec.js b/test/unit/middleware/karma.spec.js index 534a33453..ef78829cb 100644 --- a/test/unit/middleware/karma.spec.js +++ b/test/unit/middleware/karma.spec.js @@ -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 @@ -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 @@ -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 @@ -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 @@ -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') + }) })