-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement a
RequestController
class (#595)
- Loading branch information
1 parent
1c4aa13
commit 73dd07a
Showing
94 changed files
with
1,229 additions
and
877 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
export class InterceptorError extends Error { | ||
constructor(message?: string) { | ||
super(message) | ||
this.name = 'InterceptorError' | ||
Object.setPrototypeOf(this, InterceptorError.prototype) | ||
} | ||
} |
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,49 @@ | ||
import { it, expect } from 'vitest' | ||
import { kResponsePromise, RequestController } from './RequestController' | ||
|
||
it('creates a pending response promise on construction', () => { | ||
const controller = new RequestController(new Request('http://localhost')) | ||
expect(controller[kResponsePromise]).toBeInstanceOf(Promise) | ||
expect(controller[kResponsePromise].state).toBe('pending') | ||
}) | ||
|
||
it('resolves the response promise with the response provided to "respondWith"', async () => { | ||
const controller = new RequestController(new Request('http://localhost')) | ||
controller.respondWith(new Response('hello world')) | ||
|
||
const response = (await controller[kResponsePromise]) as Response | ||
|
||
expect(response).toBeInstanceOf(Response) | ||
expect(response.status).toBe(200) | ||
expect(await response.text()).toBe('hello world') | ||
}) | ||
|
||
it('resolves the response promise with the error provided to "errorWith"', async () => { | ||
const controller = new RequestController(new Request('http://localhost')) | ||
const error = new Error('Oops!') | ||
controller.errorWith(error) | ||
|
||
await expect(controller[kResponsePromise]).resolves.toEqual(error) | ||
}) | ||
|
||
it('throws when calling "respondWith" multiple times', () => { | ||
const controller = new RequestController(new Request('http://localhost')) | ||
controller.respondWith(new Response('hello world')) | ||
|
||
expect(() => { | ||
controller.respondWith(new Response('second response')) | ||
}).toThrow( | ||
'Failed to respond to the "GET http://localhost/" request: the "request" event has already been handled.' | ||
) | ||
}) | ||
|
||
it('throws when calling "errorWith" multiple times', () => { | ||
const controller = new RequestController(new Request('http://localhost')) | ||
controller.errorWith(new Error('Oops!')) | ||
|
||
expect(() => { | ||
controller.errorWith(new Error('second error')) | ||
}).toThrow( | ||
'Failed to error the "GET http://localhost/" request: the "request" event has already been handled.' | ||
) | ||
}) |
Oops, something went wrong.