-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): add middleware() to RestServer/RestApplication classes
- Loading branch information
1 parent
75b32a2
commit 009cbc0
Showing
8 changed files
with
234 additions
and
9 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
51 changes: 51 additions & 0 deletions
51
packages/rest/src/__tests__/acceptance/middleware/middleware-registeration.acceptance.ts
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,51 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: @loopback/rest | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {expect} from '@loopback/testlab'; | ||
import {spy, SPY, SpyConfig, TestFunction, TestHelper} from './test-helpers'; | ||
|
||
describe('app.middleware()', () => { | ||
let helper: TestHelper; | ||
|
||
function runTests(action: 'log' | 'mock' | 'reject', testFn: TestFunction) { | ||
describe(`app.middleware - ${action}`, () => { | ||
const spyConfig: SpyConfig = {action}; | ||
beforeEach(givenTestApp); | ||
afterEach(() => helper?.stop()); | ||
|
||
it('registers a middleware interceptor provider class by factory', () => { | ||
const binding = helper.app.middleware(spy, spyConfig); | ||
return testFn(binding); | ||
}); | ||
|
||
it('registers a middleware interceptor as handler function', () => { | ||
const binding = helper.app.restServer.middleware(spy, spyConfig, { | ||
injectConfiguration: false, | ||
key: 'interceptors.middleware.spy', | ||
}); | ||
expect(binding.key).to.eql('interceptors.middleware.spy'); | ||
return testFn(binding); | ||
}); | ||
|
||
it('registers a middleware interceptor provider class by module name', () => { | ||
const binding = helper.app.middleware(SPY, spyConfig, { | ||
className: 'spy', | ||
}); | ||
expect(binding.key).to.eql('middleware.spy'); | ||
return testFn(binding); | ||
}); | ||
}); | ||
} | ||
|
||
runTests('log', binding => helper.testSpyLog(binding)); | ||
runTests('mock', binding => helper.testSpyMock(binding)); | ||
runTests('reject', binding => helper.testSpyReject(binding)); | ||
|
||
function givenTestApp() { | ||
helper = new TestHelper(); | ||
helper.bindController(); | ||
return helper.start(); | ||
} | ||
}); |
89 changes: 89 additions & 0 deletions
89
packages/rest/src/__tests__/acceptance/middleware/middleware-sequence.acceptance.ts
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,89 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: @loopback/rest | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {BindingScope, CoreTags, inject} from '@loopback/core'; | ||
import {Middleware, MiddlewareProvider} from '../../../middleware'; | ||
import {RequestContext} from '../../../request-context'; | ||
import {DefaultSequence} from '../../../sequence'; | ||
import {spy, TestHelper} from './test-helpers'; | ||
|
||
const POST_INVOCATION_MIDDLEWARE = 'middleware.postInvocation'; | ||
describe('Middleware in sequence', () => { | ||
let helper: TestHelper; | ||
|
||
beforeEach(givenTestApp); | ||
afterEach(() => helper?.stop()); | ||
|
||
it('registers a middleware in default slot', () => { | ||
const binding = helper.app.middleware(spy, undefined); | ||
return helper.testSpyLog(binding); | ||
}); | ||
|
||
it('registers a middleware in afterInvoke slot', () => { | ||
const binding = helper.app.middleware(spy, undefined, { | ||
extensionPointName: POST_INVOCATION_MIDDLEWARE, | ||
}); | ||
return helper.testSpyLog(binding); | ||
}); | ||
|
||
it('registers a middleware in both slots', async () => { | ||
const firstSpy = helper.app.middleware(spy, undefined, { | ||
key: 'middleware.firstSpy', | ||
}); | ||
const secondSpy = helper.app | ||
.middleware(spy, undefined, { | ||
key: 'middleware.secondSpy', | ||
extensionPointName: POST_INVOCATION_MIDDLEWARE, | ||
}) | ||
// Set the scope to be `TRANSIENT` so that the new config can be loaded | ||
.inScope(BindingScope.TRANSIENT); | ||
await helper.testSpyLog(firstSpy); | ||
await helper.testSpyReject(secondSpy); | ||
}); | ||
|
||
function givenTestApp() { | ||
helper = new TestHelper(); | ||
// Create another middleware phase | ||
helper.app | ||
.bind('middleware.afterInvoke') | ||
.toProvider(MiddlewareProvider) | ||
.tag({[CoreTags.EXTENSION_POINT]: POST_INVOCATION_MIDDLEWARE}); | ||
helper.app.sequence(SequenceWithMiddleware); | ||
helper.bindController(); | ||
return helper.start(); | ||
} | ||
|
||
class SequenceWithMiddleware extends DefaultSequence { | ||
/** | ||
* Optional middleware chain | ||
* Invokes registered middleware (injected via SequenceActions.MIDDLEWARE). | ||
*/ | ||
@inject('middleware.afterInvoke', {optional: true}) | ||
protected middlewareAfterInvoke?: Middleware; | ||
|
||
async handle(context: RequestContext): Promise<void> { | ||
try { | ||
const {request, response} = context; | ||
if (this.middleware != null) { | ||
await this.middleware(context); | ||
} | ||
const route = this.findRoute(request); | ||
const args = await this.parseParams(request, route); | ||
|
||
const result = await this.invoke(route, args); | ||
|
||
// Invoke middleware after invoke | ||
context.bind('invocation.result').to(result); | ||
if (this.middlewareAfterInvoke != null) { | ||
await this.middlewareAfterInvoke(context); | ||
} | ||
|
||
this.send(response, result); | ||
} catch (error) { | ||
this.reject(context, error); | ||
} | ||
} | ||
} | ||
}); |
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
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
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