-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracting route handler context for easier testing
- Loading branch information
Showing
4 changed files
with
60 additions
and
6 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
x-pack/plugins/licensing/server/licensing_route_handler_context.test.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,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { BehaviorSubject } from 'rxjs'; | ||
import { ILicense } from './types'; | ||
import { setup } from './__fixtures__/setup'; | ||
import { createRouteHandlerContext } from './licensing_route_handler_context'; | ||
|
||
describe('licensingRouteHandlerContext', () => { | ||
it('provides the initial license value', async () => { | ||
const { license } = await setup(); | ||
const license$ = new BehaviorSubject<ILicense>(license); | ||
|
||
const context = createRouteHandlerContext(license$); | ||
|
||
const { license: contextResult } = await context({}); | ||
|
||
expect(contextResult).toBe(license); | ||
}); | ||
|
||
it('provides the latest license value', async () => { | ||
const { license } = await setup(); | ||
const license$ = new BehaviorSubject<ILicense>(license); | ||
|
||
const context = createRouteHandlerContext(license$); | ||
|
||
const latestLicense = (Symbol() as unknown) as ILicense; | ||
license$.next(latestLicense); | ||
|
||
const { license: contextResult } = await context({}); | ||
|
||
expect(contextResult).toBe(latestLicense); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
x-pack/plugins/licensing/server/licensing_route_handler_context.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,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { RequestHandlerContext, IContextProvider } from 'src/core/server'; | ||
import { Observable } from 'rxjs'; | ||
import { take } from 'rxjs/operators'; | ||
import { ILicense } from './types'; | ||
|
||
export function createRouteHandlerContext( | ||
license$: Observable<ILicense> | ||
): IContextProvider<RequestHandlerContext, 'licensing'> { | ||
return async function licensingRouteHandlerContext() { | ||
const license = await license$.pipe(take(1)).toPromise(); | ||
return { license }; | ||
}; | ||
} |
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