-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(middleware): simplify stripHost. (#3115)
The stripHost middleware only adds a modified url to the request. That modified url is only used one place. By converting the middleware to a module, the code is simpler and beforeMiddleware modules can reuse karma middleware. (One alternative considered was to move the stripHost in the chain before the beforeMiddleware, but this form seems better). Clean up regex per offline suggestion from zzo@
- Loading branch information
1 parent
68b37d3
commit d65e911
Showing
4 changed files
with
26 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,11 @@ | ||
/** | ||
* Strip host middleware is responsible for stripping hostname from request path | ||
* Strip hostname from request path | ||
* This to handle requests that uses (normally over proxies) an absoluteURI as request path | ||
*/ | ||
|
||
function createStripHostMiddleware () { | ||
return function (request, response, next) { | ||
function stripHostFromUrl (url) { | ||
return url.replace(/^http[s]?:\/\/([a-z\-.:\d]+)\//, '/') | ||
} | ||
|
||
request.normalizedUrl = stripHostFromUrl(request.url) || request.url | ||
next() | ||
} | ||
function stripHostFromUrl (url) { | ||
return url.replace(/^https?:\/\/[a-z.:\d-]+\//, '/') | ||
} | ||
|
||
// PUBLIC API | ||
exports.create = createStripHostMiddleware | ||
exports.stripHost = stripHostFromUrl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,28 @@ | ||
var mocks = require('mocks') | ||
|
||
describe('middleware.strip_host', function () { | ||
var nextSpy | ||
var HttpRequestMock = mocks.http.ServerRequest | ||
|
||
var createStripHostMiddleware = require('../../../lib/middleware/strip_host').create | ||
|
||
var handler = nextSpy = null | ||
|
||
beforeEach(function () { | ||
nextSpy = sinon.spy() | ||
handler = createStripHostMiddleware(null, null, '/base/path') | ||
return handler | ||
}) | ||
const stripHost = require('../../../lib/middleware/strip_host').stripHost | ||
|
||
it('should strip request with IP number', function (done) { | ||
var 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 | ||
return done() | ||
it('should strip request with IP number', function () { | ||
const normalizedUrl = stripHost('http://192.12.31.100/base/a.js?123345') | ||
expect(normalizedUrl).to.equal('/base/a.js?123345') | ||
}) | ||
|
||
it('should strip request with absoluteURI', function (done) { | ||
var 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 | ||
return done() | ||
it('should strip request with absoluteURI', function () { | ||
const normalizedUrl = stripHost('http://localhost/base/a.js?123345') | ||
expect(normalizedUrl).to.equal('/base/a.js?123345') | ||
}) | ||
|
||
it('should strip request with absoluteURI and port', function (done) { | ||
var 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 | ||
return done() | ||
it('should strip request with absoluteURI and port', function () { | ||
const normalizedUrl = stripHost('http://localhost:9876/base/a.js?123345') | ||
expect(normalizedUrl).to.equal('/base/a.js?123345') | ||
}) | ||
|
||
it('should strip request with absoluteURI over HTTPS', function (done) { | ||
var 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 | ||
return done() | ||
it('should strip request with absoluteURI over HTTPS', function () { | ||
const normalizedUrl = stripHost('https://karma-runner.github.io/base/a.js?123345') | ||
expect(normalizedUrl).to.equal('/base/a.js?123345') | ||
}) | ||
|
||
return it('should return same url as passed one', function (done) { | ||
var 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 | ||
return done() | ||
it('should return same url as passed one', function () { | ||
const normalizedUrl = stripHost('/base/b.js?123345') | ||
expect(normalizedUrl).to.equal('/base/b.js?123345') | ||
}) | ||
}) |