-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Make IDP routes independent of handlers
- Loading branch information
Showing
17 changed files
with
156 additions
and
123 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
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
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
16 changes: 16 additions & 0 deletions
16
src/identity/interaction/routing/AbsolutePathInteractionRoute.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,16 @@ | ||
import type { InteractionRoute } from './InteractionRoute'; | ||
|
||
/** | ||
* A route that returns the input string as path. | ||
*/ | ||
export class AbsolutePathInteractionRoute implements InteractionRoute { | ||
private readonly path: string; | ||
|
||
public constructor(path: string) { | ||
this.path = path; | ||
} | ||
|
||
public getPath(): string { | ||
return this.path; | ||
} | ||
} |
26 changes: 9 additions & 17 deletions
26
...eraction/routing/BasicInteractionRoute.ts → ...action/routing/InteractionRouteHandler.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
10 changes: 4 additions & 6 deletions
10
...ction/routing/RelativeInteractionRoute.ts → ...n/routing/RelativePathInteractionRoute.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 |
---|---|---|
@@ -1,18 +1,16 @@ | ||
import { joinUrl } from '../../../util/PathUtil'; | ||
import type { InteractionHandler } from '../InteractionHandler'; | ||
import { BasicInteractionRoute } from './BasicInteractionRoute'; | ||
import { AbsolutePathInteractionRoute } from './AbsolutePathInteractionRoute'; | ||
import type { InteractionRoute } from './InteractionRoute'; | ||
|
||
/** | ||
* A route that is relative to another route. | ||
* The relative path will be joined to the input base, | ||
* which can either be an absolute URL or an InteractionRoute of which the path will be used. | ||
* The source handler will be called for all operation requests | ||
*/ | ||
export class RelativeInteractionRoute extends BasicInteractionRoute { | ||
public constructor(base: InteractionRoute | string, relativePath: string, source?: InteractionHandler) { | ||
export class RelativePathInteractionRoute extends AbsolutePathInteractionRoute { | ||
public constructor(base: InteractionRoute | string, relativePath: string) { | ||
const url = typeof base === 'string' ? base : base.getPath(); | ||
const path = joinUrl(url, relativePath); | ||
super(path, source); | ||
super(path); | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
test/unit/identity/interaction/routing/AbsolutePathInteractionRoute.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,12 @@ | ||
import { | ||
AbsolutePathInteractionRoute, | ||
} from '../../../../../src/identity/interaction/routing/AbsolutePathInteractionRoute'; | ||
|
||
describe('An AbsolutePathInteractionRoute', (): void => { | ||
const path = 'http://example.com/idp/path/'; | ||
const route = new AbsolutePathInteractionRoute(path); | ||
|
||
it('returns the given path.', async(): Promise<void> => { | ||
expect(route.getPath()).toBe('http://example.com/idp/path/'); | ||
}); | ||
}); |
38 changes: 16 additions & 22 deletions
38
...ion/routing/BasicInteractionRoute.test.ts → ...n/routing/InteractionRouteHandler.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 |
---|---|---|
@@ -1,59 +1,53 @@ | ||
import type { Operation } from '../../../../../src/http/Operation'; | ||
import { BasicRepresentation } from '../../../../../src/http/representation/BasicRepresentation'; | ||
import type { Representation } from '../../../../../src/http/representation/Representation'; | ||
import type { | ||
InteractionHandler, | ||
} from '../../../../../src/identity/interaction/InteractionHandler'; | ||
import { BasicInteractionRoute } from '../../../../../src/identity/interaction/routing/BasicInteractionRoute'; | ||
import type { InteractionHandler } from '../../../../../src/identity/interaction/InteractionHandler'; | ||
import type { InteractionRoute } from '../../../../../src/identity/interaction/routing/InteractionRoute'; | ||
import { InteractionRouteHandler } from '../../../../../src/identity/interaction/routing/InteractionRouteHandler'; | ||
import { APPLICATION_JSON } from '../../../../../src/util/ContentTypes'; | ||
import { NotFoundHttpError } from '../../../../../src/util/errors/NotFoundHttpError'; | ||
import { createPostJsonOperation } from '../email-password/handler/Util'; | ||
|
||
describe('A BasicInteractionRoute', (): void => { | ||
describe('An InteractionRouteHandler', (): void => { | ||
const path = 'http://example.com/idp/path/'; | ||
let operation: Operation; | ||
let representation: Representation; | ||
let route: InteractionRoute; | ||
let source: jest.Mocked<InteractionHandler>; | ||
let route: BasicInteractionRoute; | ||
let handler: InteractionRouteHandler; | ||
|
||
beforeEach(async(): Promise<void> => { | ||
operation = createPostJsonOperation({}, 'http://example.com/idp/path/'); | ||
operation = createPostJsonOperation({}, path); | ||
|
||
representation = new BasicRepresentation(JSON.stringify({}), APPLICATION_JSON); | ||
|
||
route = { | ||
getPath: jest.fn().mockReturnValue(path), | ||
}; | ||
|
||
source = { | ||
canHandle: jest.fn(), | ||
handle: jest.fn().mockResolvedValue(representation), | ||
} as any; | ||
|
||
route = new BasicInteractionRoute(path, source); | ||
}); | ||
|
||
it('returns the given path.', async(): Promise<void> => { | ||
expect(route.getPath()).toBe('http://example.com/idp/path/'); | ||
handler = new InteractionRouteHandler(route, source); | ||
}); | ||
|
||
it('rejects other paths.', async(): Promise<void> => { | ||
operation = createPostJsonOperation({}, 'http://example.com/idp/otherPath/'); | ||
await expect(route.canHandle({ operation })).rejects.toThrow(NotFoundHttpError); | ||
await expect(handler.canHandle({ operation })).rejects.toThrow(NotFoundHttpError); | ||
}); | ||
|
||
it('rejects input its source cannot handle.', async(): Promise<void> => { | ||
source.canHandle.mockRejectedValueOnce(new Error('bad data')); | ||
await expect(route.canHandle({ operation })).rejects.toThrow('bad data'); | ||
await expect(handler.canHandle({ operation })).rejects.toThrow('bad data'); | ||
}); | ||
|
||
it('can handle requests its source can handle.', async(): Promise<void> => { | ||
await expect(route.canHandle({ operation })).resolves.toBeUndefined(); | ||
await expect(handler.canHandle({ operation })).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('lets its source handle requests.', async(): Promise<void> => { | ||
await expect(route.handle({ operation })).resolves.toBe(representation); | ||
}); | ||
|
||
it('defaults to an UnsupportedAsyncHandler if no source is provided.', async(): Promise<void> => { | ||
route = new BasicInteractionRoute(path); | ||
await expect(route.canHandle({ operation })).rejects.toThrow('This route has no associated handler.'); | ||
await expect(route.handle({ operation })).rejects.toThrow('This route has no associated handler.'); | ||
await expect(handler.handle({ operation })).resolves.toBe(representation); | ||
}); | ||
}); |
Oops, something went wrong.