Skip to content

Commit

Permalink
fix(web-server): Allow karma to run in project which path contains HT…
Browse files Browse the repository at this point in the history
…ML URL encoded characters. Karma fails on Jenkins when it checks out branches containing '/' as it converts it to '%2F'. Fixes errors seen on karma-runner#1751, karma-runner#61.
  • Loading branch information
dam-ien committed Aug 3, 2016
1 parent e5d792d commit da1930f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/middleware/source_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ var findByPath = function (files, path) {
})
}

var composeUrl = function (url, basePath, urlRoot, mustEscape) {
return (mustEscape ? querystring.unescape(url) : url)
.replace(urlRoot, '/')
.replace(/\?.*$/, '')
.replace(/^\/absolute/, '')
.replace(/^\/base/, basePath)
}

// Source Files middleware is responsible for serving all the source files under the test.
var createSourceFilesMiddleware = function (filesPromise, serveFile, basePath, urlRoot) {
return function (request, response, next) {
var requestedFilePath = querystring.unescape(request.url)
.replace(urlRoot, '/')
.replace(/\?.*$/, '')
.replace(/^\/absolute/, '')
.replace(/^\/base/, basePath)
var requestedFilePath = composeUrl(request.url, basePath, urlRoot, true)
// When a path contains HTML-encoded characters (e.g %2F used by Jenkins for branches with /)
var requestedFilePathUnescaped = composeUrl(request.url, basePath, urlRoot, false)

request.pause()

Expand All @@ -29,7 +35,8 @@ var createSourceFilesMiddleware = function (filesPromise, serveFile, basePath, u

return filesPromise.then(function (files) {
// TODO(vojta): change served to be a map rather then an array
var file = findByPath(files.served, requestedFilePath)
var file = findByPath(files.served, requestedFilePath) ||
findByPath(files.served, requestedFilePathUnescaped)
var rangeHeader = request.headers['range']

if (file) {
Expand Down
19 changes: 19 additions & 0 deletions test/unit/middleware/source_files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ describe('middleware.source_files', function () {
},
'utf8ášč': {
'some.js': mocks.fs.file(0, 'utf8-file')
},
'jenkins%2Fbranch': {
'some.js': mocks.fs.file(0, 'utf8-file')
}
})

Expand Down Expand Up @@ -209,6 +212,22 @@ describe('middleware.source_files', function () {
)
})

it('should serve js source file from paths containing HTML URL encoded chars', function () {
servedFiles([
new File('/jenkins%2Fbranch/some.js')
])

server = createServer(files, serveFile, '')

return request(server)
.get('/base/jenkins%2Fbranch/some.js')
.expect(200, 'utf8-file')
.then(function () {
return expect(next).not.to.have.been.called
}
)
})

it('should set content-type headers', function () {
servedFiles([
new File('/base/path/index.html')
Expand Down

0 comments on commit da1930f

Please sign in to comment.