From b9a2930a7fead5f29eb5f62b1a87739c4cf2e04b Mon Sep 17 00:00:00 2001 From: sylvain-hamel Date: Thu, 13 Mar 2014 10:54:58 -0400 Subject: [PATCH] feat(web-server): run karma using multiple emulation modes Closes #936, #631 --- lib/middleware/karma.js | 32 +++++++++++++++++++++++--- static/client.html | 3 ++- static/debug.html | 1 + test/unit/middleware/karma.spec.coffee | 26 +++++++++++++++++++-- 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index ec4c26184..fbd8956c4 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -13,6 +13,7 @@ var path = require('path'); var util = require('util'); +var urlparse = require('url').parse; var common = require('./common'); @@ -25,7 +26,6 @@ var SCRIPT_TYPE = { '.dart': 'application/dart' }; - var filePathToUrlPath = function(filePath, basePath) { if (filePath.indexOf(basePath) === 0) { return '/base' + filePath.substr(basePath.length); @@ -34,6 +34,25 @@ var filePathToUrlPath = function(filePath, basePath) { return '/absolute' + filePath; }; +var getXUACompatibleMetaElement = function(url) { + var tag = ''; + var urlObj = urlparse(url, true); + if (urlObj.query['x-ua-compatible']) { + tag = '\n'; + } + return tag; +}; + +var getXUACompatibleUrl = function(url) { + var value = ''; + var urlObj = urlparse(url, true); + if (urlObj.query['x-ua-compatible']) { + value = '?x-ua-compatible=' + encodeURIComponent(urlObj.query['x-ua-compatible']); + } + return value; +}; + var createKarmaMiddleware = function(filesPromise, serveStaticFile, /* config.basePath */ basePath, /* config.urlRoot */ urlRoot) { @@ -57,7 +76,11 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile, // serve client.html if (requestUrl === '/') { - return serveStaticFile('/client.html', response); + return serveStaticFile('/client.html', response, function(data) { + return data + .replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url)) + .replace('%X_UA_COMPATIBLE_URL%', getXUACompatibleUrl(request.url)); + }); } // serve karma.js @@ -109,7 +132,10 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile, mappings = 'window.__karma__.files = {\n' + mappings.join(',\n') + '\n};\n'; - return data.replace('%SCRIPTS%', scriptTags.join('\n')).replace('%MAPPINGS%', mappings); + return data. + replace('%SCRIPTS%', scriptTags.join('\n')) + .replace('%MAPPINGS%', mappings) + .replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url)); }); }, function(errorFiles) { serveStaticFile(requestUrl, response, function(data) { diff --git a/static/client.html b/static/client.html index 5a139223f..72f8dce13 100644 --- a/static/client.html +++ b/static/client.html @@ -5,6 +5,7 @@ --> +%X_UA_COMPATIBLE% Karma @@ -101,7 +102,7 @@ diff --git a/static/debug.html b/static/debug.html index 772c1fd9d..22b6c6f34 100644 --- a/static/debug.html +++ b/static/debug.html @@ -6,6 +6,7 @@ --> +%X_UA_COMPATIBLE% Karma DEBUG RUNNER diff --git a/test/unit/middleware/karma.spec.coffee b/test/unit/middleware/karma.spec.coffee index e95997286..4252ce5ec 100644 --- a/test/unit/middleware/karma.spec.coffee +++ b/test/unit/middleware/karma.spec.coffee @@ -16,9 +16,9 @@ describe 'middleware.karma', -> fsMock = mocks.fs.create karma: static: - 'client.html': mocks.fs.file(0, 'CLIENT HTML') + 'client.html': mocks.fs.file(0, 'CLIENT HTML\n%X_UA_COMPATIBLE%%X_UA_COMPATIBLE_URL%') 'context.html': mocks.fs.file(0, 'CONTEXT\n%SCRIPTS%') - 'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%') + 'debug.html': mocks.fs.file(0, 'DEBUG\n%SCRIPTS%\n%X_UA_COMPATIBLE%') 'karma.js': mocks.fs.file(0, 'root: %KARMA_URL_ROOT%, v: %KARMA_VERSION%') serveFile = require('../../../lib/middleware/common').createServeFile fsMock, '/karma/static' @@ -92,6 +92,28 @@ describe 'middleware.karma', -> callHandlerWith '/?id=123' + it 'should serve /?x-ua-compatible with replaced values', (done) -> + handler = createKarmaMiddleware null, serveFile, '/base', '/' + + response.once 'end', -> + expect(nextSpy).not.to.have.been.called + expect(response).to.beServedAs 200, + 'CLIENT HTML\n' + + '?x-ua-compatible=xxx%3Dyyy' + done() + + callHandlerWith '/?x-ua-compatible=xxx%3Dyyy' + + it 'should serve debug.html/?x-ua-compatible with replaced values', (done) -> + includedFiles [] + + response.once 'end', -> + expect(nextSpy).not.to.have.been.called + expect(response).to.beServedAs 200, + 'DEBUG\n\n' + done() + + callHandlerWith '/__karma__/debug.html?x-ua-compatible=xxx%3Dyyy' it 'should serve karma.js with version and urlRoot variables', (done) -> response.once 'end', ->