Skip to content

Commit

Permalink
feat: adding support for before middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ibash committed May 30, 2016
1 parent ca95553 commit 51b4206
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
8 changes: 8 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,14 @@ mime: {
```


## beforeMiddleware
**Type:** Array

**Default:** `[]`

**Description:** This is the same as middleware except that these middleware will be run before karma's own middleware.


## plugins
**Type:** Array

Expand Down
27 changes: 17 additions & 10 deletions lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,23 @@ var createWebServer = function (injector, emitter, fileList) {

log.debug('Instantiating middleware')
var handler = connect()
.use(injector.invoke(runnerMiddleware.create))
.use(injector.invoke(stopperMiddleware.create))
.use(injector.invoke(stripHostMiddleware.create))
.use(injector.invoke(karmaMiddleware.create))
.use(injector.invoke(sourceFilesMiddleware.create))
// TODO(vojta): extract the proxy into a plugin
.use(proxyMiddlewareInstance)
// TODO(vojta): remove, this is only here because of karma-dart
// we need a better way of custom handlers
.use(injector.invoke(createCustomHandler))

if (config.beforeMiddleware) {
config.beforeMiddleware.forEach(function (middleware) {
handler.use(injector.get('middleware:' + middleware))
})
}

handler.use(injector.invoke(runnerMiddleware.create))
handler.use(injector.invoke(stopperMiddleware.create))
handler.use(injector.invoke(stripHostMiddleware.create))
handler.use(injector.invoke(karmaMiddleware.create))
handler.use(injector.invoke(sourceFilesMiddleware.create))
// TODO(vojta): extract the proxy into a plugin
handler.use(proxyMiddlewareInstance)
// TODO(vojta): remove, this is only here because of karma-dart
// we need a better way of custom handlers
handler.use(injector.invoke(createCustomHandler))

if (config.middleware) {
config.middleware.forEach(function (middleware) {
Expand Down
38 changes: 38 additions & 0 deletions test/unit/web-server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('web-server', () => {
// this relies on the fact that none of these tests mutate fs
var m = mocks.loadFile(path.join(__dirname, '/../../lib/web-server.js'), _mocks, _globals)
var customFileHandlers = server = emitter = null
var beforeMiddlewareActive = false
var middlewareActive = false
var servedFiles = (files) => {
emitter.emit('file_list_modified', {included: [], served: files})
Expand All @@ -45,6 +46,7 @@ describe('web-server', () => {
var config = {
basePath: '/base/path',
urlRoot: '/',
beforeMiddleware: ['beforeCustom'],
middleware: ['custom'],
middlewareResponse: 'hello middleware!',
mime: {'custom/custom': ['custom']}
Expand All @@ -59,6 +61,15 @@ describe('web-server', () => {
reporter: ['value', null],
executor: ['value', null],
proxies: ['value', null],
'middleware:beforeCustom': ['factory', function (config) {
return function (request, response, next) {
if (beforeMiddlewareActive) {
response.writeHead(223)
return response.end('hello from before middleware!')
}
next()
}
}],
'middleware:custom': ['factory', function (config) {
return function (request, response, next) {
if (middlewareActive) {
Expand Down Expand Up @@ -106,12 +117,39 @@ describe('web-server', () => {
.expect(200, 'new-js-source')
})

describe('beforeMiddleware', () => {
beforeEach(() => {
servedFiles(new Set([new File('/base/path/one.js')]))
beforeMiddlewareActive = true
})

afterEach(() => {
beforeMiddlewareActive = false
})

it('should use injected middleware', () => {
return request(server)
.get('/base/other.js')
.expect(223, 'hello from before middleware!')
})

it('should inject middleware before served files', () => {
return request(server)
.get('/base/one.js')
.expect(223, 'hello from before middleware!')
})
})

describe('middleware', () => {
beforeEach(() => {
servedFiles(new Set([new File('/base/path/one.js')]))
middlewareActive = true
})

afterEach(() => {
middlewareActive = false
})

it('should use injected middleware', () => {
return request(server)
.get('/base/other.js')
Expand Down

0 comments on commit 51b4206

Please sign in to comment.