Skip to content

Commit

Permalink
fix(web-server): inline the config, when serving debug.html
Browse files Browse the repository at this point in the history
  • Loading branch information
maksimr committed Apr 11, 2014
1 parent 5aaa4c1 commit 1eb3643
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
1 change: 0 additions & 1 deletion Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module.exports = (grunt) ->
files: '<%= files.client %>'
tasks: 'browserify:client'


simplemocha:
options:
ui: 'bdd'
Expand Down
11 changes: 9 additions & 2 deletions lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var getXUACompatibleUrl = function(url) {
};

var createKarmaMiddleware = function(filesPromise, serveStaticFile,
/* config.basePath */ basePath, /* config.urlRoot */ urlRoot) {
/* config.basePath */ basePath, /* config.urlRoot */ urlRoot, /* config.client */ client) {

return function(request, response, next) {
var requestUrl = request.url.replace(/\?.*/, '');
Expand Down Expand Up @@ -130,17 +130,24 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile,
return util.format(' \'%s\': \'%s\'', filePath, file.sha);
});

var clientConfig = '';

if (requestUrl === '/debug.html') {
clientConfig = 'window.__karma__.config = ' + JSON.stringify(client) + ';\n';
}

mappings = 'window.__karma__.files = {\n' + mappings.join(',\n') + '\n};\n';

return data.
replace('%SCRIPTS%', scriptTags.join('\n'))
.replace('%CLIENT_CONFIG%', clientConfig)
.replace('%MAPPINGS%', mappings)
.replace('\n%X_UA_COMPATIBLE%', getXUACompatibleMetaElement(request.url));
});
}, function(errorFiles) {
serveStaticFile(requestUrl, response, function(data) {
common.setNoCacheHeaders(response);
return data.replace('%SCRIPTS%', '').replace('%MAPPINGS%',
return data.replace('%SCRIPTS%', '').replace('%CLIENT_CONFIG%', '').replace('%MAPPINGS%',
'window.__karma__.error("TEST RUN WAS CANCELLED because ' +
(errorFiles.length > 1 ? 'these files contain' : 'this file contains') +
' some errors:\\n ' + errorFiles.join('\\n ') + '");');
Expand Down
2 changes: 2 additions & 0 deletions static/debug.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
}
};

%CLIENT_CONFIG%

// All served files with the latest timestamps
%MAPPINGS%
</script>
Expand Down
17 changes: 16 additions & 1 deletion test/unit/middleware/karma.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ describe 'middleware.karma', ->
handler = filesDeferred = nextSpy = response = null

beforeEach ->
clientConfig = foo: 'bar'
nextSpy = sinon.spy()
response = new HttpResponseMock
filesDeferred = q.defer()
handler = createKarmaMiddleware filesDeferred.promise, serveFile, '/base/path', '/__karma__/'
handler = createKarmaMiddleware filesDeferred.promise, serveFile,
'/base/path', '/__karma__/', clientConfig

# helpers
includedFiles = (files) ->
Expand Down Expand Up @@ -276,6 +278,19 @@ describe 'middleware.karma', ->
callHandlerWith '/__karma__/debug.html'


it 'should inline client config to debug.html', (done) ->
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'
done()

callHandlerWith '/__karma__/debug.html'


it 'should not serve other files even if they are in urlRoot', (done) ->
includedFiles []

Expand Down

0 comments on commit 1eb3643

Please sign in to comment.