Skip to content

Commit

Permalink
feat(web-server): Serve all files under urlRoot
Browse files Browse the repository at this point in the history
Serves all files under urlRoot.
  • Loading branch information
nmalaguti committed Jun 1, 2015
1 parent b8e1bf1 commit 1319b32
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
11 changes: 5 additions & 6 deletions lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
});
Expand Down
5 changes: 3 additions & 2 deletions lib/middleware/source_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 19 additions & 19 deletions test/unit/middleware/karma.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ describe 'middleware.karma', ->
response.once 'end', ->
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs 200, 'CONTEXT\n' +
'<script type="text/javascript" src="/absolute/first.js?sha123"></script>\n' +
'<script type="application/dart" src="/absolute/second.dart?sha456"></script>'
'<script type="text/javascript" src="/__karma__/absolute/first.js?sha123"></script>\n' +
'<script type="application/dart" src="/__karma__/absolute/second.dart?sha456"></script>'
done()

callHandlerWith '/__karma__/context.html'
Expand All @@ -158,8 +158,8 @@ describe 'middleware.karma', ->
response.once 'end', ->
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs 200, 'CONTEXT\n' +
'<link type="text/css" href="/absolute/first.css?sha007" rel="stylesheet">\n' +
'<link href="/absolute/second.html?sha678" rel="import">'
'<link type="text/css" href="/__karma__/absolute/first.css?sha007" rel="stylesheet">\n' +
'<link href="/__karma__/absolute/second.html?sha678" rel="import">'
done()

callHandlerWith '/__karma__/context.html'
Expand All @@ -173,8 +173,8 @@ describe 'middleware.karma', ->
response.once 'end', ->
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs 200, 'CONTEXT\n' +
'<script type="text/javascript" src="/absolute/some/abc/a.js?sha"></script>\n' +
'<script type="text/javascript" src="/base/b.js?shaaa"></script>'
'<script type="text/javascript" src="/__karma__/absolute/some/abc/a.js?sha"></script>\n' +
'<script type="text/javascript" src="/__karma__/base/b.js?shaaa"></script>'
done()

callHandlerWith '/__karma__/context.html'
Expand All @@ -191,10 +191,10 @@ describe 'middleware.karma', ->
response.once 'end', ->
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs 200, 'CONTEXT\n' +
'<link type="text/css" href="/absolute/some/abc/a.css?sha1" rel="stylesheet">\n' +
'<link type="text/css" href="/base/b.css?sha2" rel="stylesheet">\n' +
'<link href="/absolute/some/abc/c.html?sha3" rel="import">\n' +
'<link href="/base/d.html?sha4" rel="import">'
'<link type="text/css" href="/__karma__/absolute/some/abc/a.css?sha1" rel="stylesheet">\n' +
'<link type="text/css" href="/__karma__/base/b.css?sha2" rel="stylesheet">\n' +
'<link href="/__karma__/absolute/some/abc/c.html?sha3" rel="import">\n' +
'<link href="/__karma__/base/d.html?sha4" rel="import">'
done()

callHandlerWith '/__karma__/context.html'
Expand Down Expand Up @@ -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()

Expand All @@ -257,8 +257,8 @@ describe 'middleware.karma', ->
response.once 'end', ->
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs 200, 'DEBUG\n' +
'<script type="text/javascript" src="/absolute/first.js"></script>\n' +
'<script type="text/javascript" src="/base/b.js"></script>'
'<script type="text/javascript" src="/__karma__/absolute/first.js"></script>\n' +
'<script type="text/javascript" src="/__karma__/base/b.js"></script>'
done()

callHandlerWith '/__karma__/debug.html'
Expand All @@ -275,10 +275,10 @@ describe 'middleware.karma', ->
response.once 'end', ->
expect(nextSpy).not.to.have.been.called
expect(response).to.beServedAs 200, 'DEBUG\n' +
'<link type="text/css" href="/absolute/first.css" rel="stylesheet">\n' +
'<link type="text/css" href="/base/b.css" rel="stylesheet">\n' +
'<link href="/absolute/second.html" rel="import">\n' +
'<link href="/base/d.html" rel="import">'
'<link type="text/css" href="/__karma__/absolute/first.css" rel="stylesheet">\n' +
'<link type="text/css" href="/__karma__/base/b.css" rel="stylesheet">\n' +
'<link href="/__karma__/absolute/second.html" rel="import">\n' +
'<link href="/__karma__/base/d.html" rel="import">'
done()

callHandlerWith '/__karma__/debug.html'
Expand Down

0 comments on commit 1319b32

Please sign in to comment.