Skip to content

Commit

Permalink
feat(web-server): run karma using multiple emulation modes
Browse files Browse the repository at this point in the history
Closes #936, #631
  • Loading branch information
sylvain-hamel committed Mar 31, 2014
1 parent d5eddf1 commit b9a2930
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
32 changes: 29 additions & 3 deletions lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var path = require('path');
var util = require('util');
var urlparse = require('url').parse;

var common = require('./common');

Expand All @@ -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);
Expand All @@ -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<meta http-equiv="X-UA-Compatible" content="' +
urlObj.query['x-ua-compatible'] + '"/>';
}
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) {

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion static/client.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
-->
<html>
<head>
%X_UA_COMPATIBLE%
<title>Karma</title>
<!-- TOOD(vojta): create simple favicon and cache it -->
<link href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=" rel="icon" type="image/x-icon">
Expand Down Expand Up @@ -101,7 +102,7 @@
</head>
<body>
<div id="banner" class="offline">
<a href="debug.html" target="_blank" class="btn-debug">DEBUG</a>
<a href="debug.html%X_UA_COMPATIBLE_URL%" target="_blank" class="btn-debug">DEBUG</a>
<h1 id="title">Karma - starting</h1>
</div>

Expand Down
1 change: 1 addition & 0 deletions static/debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
-->
<html>
<head>
%X_UA_COMPATIBLE%
<title>Karma DEBUG RUNNER</title>
<!-- TOOD(vojta): create simple favicon and cache it -->
<link href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=" rel="icon" type="image/x-icon" />
Expand Down
26 changes: 24 additions & 2 deletions test/unit/middleware/karma.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>' +
'?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<meta http-equiv="X-UA-Compatible" content="xxx=yyy"/>'
done()

callHandlerWith '/__karma__/debug.html?x-ua-compatible=xxx%3Dyyy'

it 'should serve karma.js with version and urlRoot variables', (done) ->
response.once 'end', ->
Expand Down

0 comments on commit b9a2930

Please sign in to comment.