From 1319b32de244a8501e4e00b7334a96783778d977 Mon Sep 17 00:00:00 2001 From: Nick Malaguti Date: Mon, 20 Apr 2015 20:25:13 -0400 Subject: [PATCH] feat(web-server): Serve all files under urlRoot Serves all files under urlRoot. --- lib/middleware/karma.js | 11 ++++---- lib/middleware/source_files.js | 5 ++-- test/unit/middleware/karma.spec.coffee | 38 +++++++++++++------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/middleware/karma.js b/lib/middleware/karma.js index 246502888..9c111b77e 100644 --- a/lib/middleware/karma.js +++ b/lib/middleware/karma.js @@ -32,12 +32,12 @@ var SCRIPT_TYPE = { '.dart': 'application/dart' }; -var filePathToUrlPath = function(filePath, basePath) { +var filePathToUrlPath = function(filePath, basePath, urlRoot) { if (filePath.indexOf(basePath) === 0) { - return '/base' + filePath.substr(basePath.length); + return urlRoot + 'base' + filePath.substr(basePath.length); } - return '/absolute' + filePath; + return urlRoot + 'absolute' + filePath; }; var getXUACompatibleMetaElement = function(url) { @@ -109,8 +109,7 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile, var fileExt = path.extname(filePath); if (!file.isUrl) { - // TODO(vojta): serve these files from within urlRoot as well - filePath = filePathToUrlPath(filePath, basePath); + filePath = filePathToUrlPath(filePath, basePath, urlRoot); if (requestUrl === '/context.html') { filePath += '?' + file.sha; @@ -131,7 +130,7 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile, // TODO(vojta): don't compute if it's not in the template var mappings = files.served.map(function(file) { //Windows paths contain backslashes and generate bad IDs if not escaped - var filePath = filePathToUrlPath(file.path, basePath).replace(/\\/g,'\\\\'); + var filePath = filePathToUrlPath(file.path, basePath, urlRoot).replace(/\\/g,'\\\\'); return util.format(' \'%s\': \'%s\'', filePath, file.sha); }); diff --git a/lib/middleware/source_files.js b/lib/middleware/source_files.js index e20f4d16f..5f87fc1b5 100644 --- a/lib/middleware/source_files.js +++ b/lib/middleware/source_files.js @@ -19,11 +19,12 @@ var findByPath = function(files, path) { var createSourceFilesMiddleware = function(filesPromise, serveFile, - /* config.basePath */ basePath) { + /* config.basePath */ basePath, + /* config.urlRoot */ urlRoot) { return function(request, response, next) { - // TODO(vojta): serve files under urlRoot (also update the formatError) var requestedFilePath = querystring.unescape(request.url) + .replace(urlRoot, '/') .replace(/\?.*$/, '') .replace(/^\/absolute/, '') .replace(/^\/base/, basePath); diff --git a/test/unit/middleware/karma.spec.coffee b/test/unit/middleware/karma.spec.coffee index dbb58c59f..b609d467f 100644 --- a/test/unit/middleware/karma.spec.coffee +++ b/test/unit/middleware/karma.spec.coffee @@ -142,8 +142,8 @@ describe 'middleware.karma', -> response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '\n' + - '' + '\n' + + '' done() callHandlerWith '/__karma__/context.html' @@ -158,8 +158,8 @@ describe 'middleware.karma', -> response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '\n' + - '' + '\n' + + '' done() callHandlerWith '/__karma__/context.html' @@ -173,8 +173,8 @@ describe 'middleware.karma', -> response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '\n' + - '' + '\n' + + '' done() callHandlerWith '/__karma__/context.html' @@ -191,10 +191,10 @@ describe 'middleware.karma', -> response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'CONTEXT\n' + - '\n' + - '\n' + - '\n' + - '' + '\n' + + '\n' + + '\n' + + '' done() callHandlerWith '/__karma__/context.html' @@ -239,9 +239,9 @@ describe 'middleware.karma', -> response.once 'end', -> expect(response).to.beServedAs 200, 'window.__karma__.files = {\n' + - " '/absolute/some/abc/a.js': 'sha_a',\n" + - " '/base/b.js': 'sha_b',\n" + - " '/absolute\\\\windows\\\\path\\\\uuu\\\\c.js': 'sha_c'\n" + + " '/__karma__/absolute/some/abc/a.js': 'sha_a',\n" + + " '/__karma__/base/b.js': 'sha_b',\n" + + " '/__karma__/absolute\\\\windows\\\\path\\\\uuu\\\\c.js': 'sha_c'\n" + "};\n" done() @@ -257,8 +257,8 @@ describe 'middleware.karma', -> response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'DEBUG\n' + - '\n' + - '' + '\n' + + '' done() callHandlerWith '/__karma__/debug.html' @@ -275,10 +275,10 @@ describe 'middleware.karma', -> response.once 'end', -> expect(nextSpy).not.to.have.been.called expect(response).to.beServedAs 200, 'DEBUG\n' + - '\n' + - '\n' + - '\n' + - '' + '\n' + + '\n' + + '\n' + + '' done() callHandlerWith '/__karma__/debug.html'