Skip to content

Commit

Permalink
fix(web-server): strip scheme, host and port
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikbonander committed Mar 4, 2014
1 parent abdcb45 commit 06a0da0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var createKarmaMiddleware = function(filesPromise, serveStaticFile,
/* config.basePath */ basePath, /* config.urlRoot */ urlRoot) {

return function(request, response, next) {
var requestUrl = request.url.replace(/\?.*/, '');
var requestUrl = request.normalizedUrl.replace(/\?.*/, '');

// redirect /__karma__ to /__karma__ (trailing slash)
if (requestUrl === urlRoot.substr(0, urlRoot.length - 1)) {
Expand Down
18 changes: 18 additions & 0 deletions lib/middleware/strip_host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Strip host middleware is responsible for stripping hostname from request path
* This to handle requests that uses (normally over proxies) an absolutURI as request path
*/

var createStripHostMiddleware = function() {
return function(request, response, next) {
function stripHostFromUrl(url) {
return url.replace(/^http[s]?:\/\/([a-z\-\.\:\d]+)\//, '/');
}

request.normalizedUrl = stripHostFromUrl(request.url) || request.url;
next();
};
};

// PUBLIC API
exports.create = createStripHostMiddleware;
2 changes: 2 additions & 0 deletions lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var connect = require('connect');

var common = require('./middleware/common');
var runnerMiddleware = require('./middleware/runner');
var stripHostMiddleware = require('./middleware/strip_host');
var karmaMiddleware = require('./middleware/karma');
var sourceFilesMiddleware = require('./middleware/source_files');
var proxyMiddleware = require('./middleware/proxy');
Expand Down Expand Up @@ -46,6 +47,7 @@ var createWebServer = function(injector, emitter) {

var handler = connect()
.use(injector.invoke(runnerMiddleware.create))
.use(injector.invoke(stripHostMiddleware.create))
.use(injector.invoke(karmaMiddleware.create))
.use(injector.invoke(sourceFilesMiddleware.create))
// TODO(vojta): extract the proxy into a plugin
Expand Down
15 changes: 10 additions & 5 deletions test/unit/middleware/karma.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ describe 'middleware.karma', ->
servedFiles = (files) ->
filesDeferred.resolve {included: [], served: files}

normalizedHttpRequest = (urlPath) ->
req = new HttpRequestMock(urlPath)
req.normalizedUrl = req.url
return req

callHandlerWith = (urlPath, next) ->
promise = handler new HttpRequestMock(urlPath), response, next or nextSpy
promise = handler normalizedHttpRequest(urlPath), response, next or nextSpy
if promise and promise.done then promise.done()


Expand All @@ -55,19 +60,19 @@ describe 'middleware.karma', ->


it 'should not serve outside of urlRoot', ->
handler new HttpRequestMock('/'), null, nextSpy
handler normalizedHttpRequest('/'), null, nextSpy
expect(nextSpy).to.have.been.called
nextSpy.reset()

handler new HttpRequestMock('/client.html'), null, nextSpy
handler normalizedHttpRequest('/client.html'), null, nextSpy
expect(nextSpy).to.have.been.called
nextSpy.reset()

handler new HttpRequestMock('/debug.html'), null, nextSpy
handler normalizedHttpRequest('/debug.html'), null, nextSpy
expect(nextSpy).to.have.been.called
nextSpy.reset()

handler new HttpRequestMock('/context.html'), null, nextSpy
handler normalizedHttpRequest('/context.html'), null, nextSpy
expect(nextSpy).to.have.been.called


Expand Down
70 changes: 70 additions & 0 deletions test/unit/middleware/strip_host.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
describe 'middleware.strip_host', ->
q = require 'q'

mocks = require 'mocks'
HttpResponseMock = mocks.http.ServerResponse
HttpRequestMock = mocks.http.ServerRequest

File = require('../../../lib/file_list').File
Url = require('../../../lib/file_list').Url

fsMock = mocks.fs.create
base:
path:
'a.js': mocks.fs.file(0, 'js-src-a')
'index.html': mocks.fs.file(0, '<html>')
src:
'some.js': mocks.fs.file(0, 'js-source')
'utf8ášč':
'some.js': mocks.fs.file(0, 'utf8-file')


serveFile = require('../../../lib/middleware/common').createServeFile fsMock, null
createStripHostMiddleware = require('../../../lib/middleware/strip_host').create

handler = filesDeferred = nextSpy = response = null

beforeEach ->
nextSpy = sinon.spy()
request = null
handler = createStripHostMiddleware null, null, '/base/path'

it 'should strip request with IP number', (done) ->
request = new HttpRequestMock('http://192.12.31.100/base/a.js?123345')
handler request, null, nextSpy

expect(request.normalizedUrl).to.equal '/base/a.js?123345'
expect(nextSpy).to.have.been.called
done()

it 'should strip request with absoluteURI', (done) ->
request = new HttpRequestMock('http://localhost/base/a.js?123345')
handler request, null, nextSpy

expect(request.normalizedUrl).to.equal '/base/a.js?123345'
expect(nextSpy).to.have.been.called
done()

it 'should strip request with absoluteURI and port', (done) ->
request = new HttpRequestMock('http://localhost:9876/base/a.js?123345')
handler request, null, nextSpy

expect(request.normalizedUrl).to.equal '/base/a.js?123345'
expect(nextSpy).to.have.been.called
done()

it 'should strip request with absoluteURI over HTTPS', (done) ->
request = new HttpRequestMock('https://karma-runner.github.io/base/a.js?123345')
handler request, null, nextSpy

expect(request.normalizedUrl).to.equal '/base/a.js?123345'
expect(nextSpy).to.have.been.called
done()

it 'should return same url as passed one', (done) ->
request = new HttpRequestMock('/base/b.js?123345')
handler request, null, nextSpy

expect(request.normalizedUrl).to.equal '/base/b.js?123345'
expect(nextSpy).to.have.been.called
done()
5 changes: 5 additions & 0 deletions test/unit/web-server.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ describe 'web-server', ->

requestPath '/'

it 'should serve client.html with a absoluteURI request path ', (done) ->
response.once 'end', ->
expect(response).to.beServedAs 200, 'CLIENT HTML'
done()
requestPath 'http://localhost:9876/'

it 'should serve source files', (done) ->
response.once 'end', ->
Expand Down

0 comments on commit 06a0da0

Please sign in to comment.