-
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.
feat: Allow frameworks to inject middleware
The Server#_start function was injected with `webServer` and `socketServer`. This prevents frameworks from being able to manipulate `config.middleware` before those servers are created. The solution is simply to defer injection until after all frameworks have loaded, and then fetch those values with `injector.get`. There were no e2e tests involving middleware. I have added two, including one that verifies the new capabilities.
- Loading branch information
1 parent
eb94739
commit d972f3d
Showing
7 changed files
with
99 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Feature: Middleware | ||
In order to use Karma | ||
As a person who wants to write great tests | ||
I want to use custom middleware with Karma. | ||
|
||
Scenario: Simple middleware | ||
Given a configuration with: | ||
""" | ||
files = ['middleware/test.js']; | ||
browsers = ['PhantomJS']; | ||
plugins = [ | ||
'karma-jasmine', | ||
'karma-phantomjs-launcher', | ||
_resolve('middleware/middleware') | ||
]; | ||
middleware = [ | ||
'foo' | ||
] | ||
""" | ||
When I start Karma | ||
Then it passes with: | ||
""" | ||
. | ||
PhantomJS | ||
""" | ||
|
||
Scenario: Frameworks can add middleware | ||
Given a configuration with: | ||
""" | ||
files = ['middleware/test.js']; | ||
browsers = ['PhantomJS']; | ||
plugins = [ | ||
'karma-jasmine', | ||
'karma-phantomjs-launcher', | ||
_resolve('middleware/middleware') | ||
]; | ||
frameworks = ['jasmine', 'foo'] | ||
""" | ||
When I start Karma | ||
Then it passes with: | ||
""" | ||
. | ||
PhantomJS | ||
""" |
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 |
---|---|---|
|
@@ -19,5 +19,4 @@ Feature: Passing Options | |
Then it passes with no debug: | ||
""" | ||
. | ||
PhantomJS | ||
""" |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function middleware (request, response, next) { | ||
if (/\/foo\.js/.test(request.normalizedUrl)) { | ||
response.setHeader('Content-Type', 'text/plain') | ||
response.writeHead(200) | ||
response.end('this is the middleware response') | ||
return | ||
} | ||
next() | ||
} | ||
|
||
function framework (config) { | ||
config.middleware = config.middleware || [] | ||
config.middleware.push('foo') | ||
} | ||
|
||
framework.$inject = ['config'] | ||
|
||
module.exports = { | ||
'framework:foo': ['factory', framework], | ||
'middleware:foo': ['value', middleware] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function httpGet (url) { | ||
var xmlHttp = new XMLHttpRequest() | ||
|
||
xmlHttp.open('GET', url, false) | ||
xmlHttp.send(null) | ||
|
||
return xmlHttp.responseText | ||
} | ||
|
||
describe('foo', function () { | ||
it('should should serve /foo.js', function () { | ||
expect(httpGet('/foo.js')).toBe('this is the middleware response') | ||
}) | ||
}) |
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