Skip to content

Commit

Permalink
fix(watcher): use correct file sha in url query string
Browse files Browse the repository at this point in the history
Use the most recently computed file sha present on the served file
when setting the file sha in the scripts tags added to the context.html.

This fixes an issue where the sha query arg would always be the sha of
the file when karma initially started and would not take into account
changes to the file when the watcher is running.

This may also address the issues in #2317 and #2264
  • Loading branch information
chriscasola committed Mar 30, 2017
1 parent de55bc6 commit ad87a88
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* - setting propert caching headers
*/

var _ = require('lodash')
var from = require('core-js/library/fn/array/from')
var path = require('path')
var util = require('util')
var url = require('url')
Expand Down Expand Up @@ -74,6 +76,12 @@ var isFirefox = function (req) {
return firefox
}

var findByPath = function (files, path) {
return _.find(from(files), function (file) {
return file.path === path
})
}

var createKarmaMiddleware = function (
filesPromise,
serveStaticFile,
Expand Down Expand Up @@ -183,7 +191,12 @@ var createKarmaMiddleware = function (
filePath = filePathToUrlPath(filePath, basePath, urlRoot, proxyPath)

if (requestUrl === '/context.html') {
filePath += '?' + file.sha
var servedFile = findByPath(files.served, file.path)
if (servedFile) {
filePath += '?' + servedFile.sha
} else {
filePath += '?' + file.sha
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/unit/middleware/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,27 @@ describe('middleware.karma', () => {
callHandlerWith('/__karma__/context.html')
})

it('should serve the sha of the served file if the file is in served files', (done) => {
var include = [
new MockFile('/some/abc/a.js', 'sha1'),
new MockFile('/base/path/b.js', 'sha2')
]

var serve = [
new MockFile('/some/abc/a.js', 'sha3'),
new MockFile('/base/path/b.js', 'sha4')
]

filesDeferred.resolve({included: include, served: serve})

response.once('end', () => {
expect(response).to.beServedAs(200, 'CONTEXT\n<script type="text/javascript" src="/__proxy__/__karma__/absolute/some/abc/a.js?sha3" crossorigin="anonymous"></script>\n<script type="text/javascript" src="/__proxy__/__karma__/base/b.js?sha4" crossorigin="anonymous"></script>')
done()
})

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

it('should serve context.html with the correct path for link tags', (done) => {
includedFiles([
new MockFile('/some/abc/a.css', 'sha1'),
Expand Down

0 comments on commit ad87a88

Please sign in to comment.