-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: treat thrown responses as mocked responses (#553)
- Loading branch information
1 parent
1913599
commit 58a089a
Showing
7 changed files
with
145 additions
and
34 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
51 changes: 51 additions & 0 deletions
51
test/modules/http/compliance/http-unhandled-exception.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,51 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
import { vi, it, expect, beforeAll, afterEach, afterAll } from 'vitest' | ||
import http from 'node:http' | ||
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest' | ||
import { waitForClientRequest } from '../../../helpers' | ||
|
||
const interceptor = new ClientRequestInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('handles a thrown Response as a mocked response', async () => { | ||
interceptor.on('request', () => { | ||
throw new Response('hello world') | ||
}) | ||
|
||
const request = http.get('http://localhost/resource') | ||
const { res, text } = await waitForClientRequest(request) | ||
|
||
expect(res.statusCode).toBe(200) | ||
expect(res.statusMessage).toBe('OK') | ||
expect(await text()).toBe('hello world') | ||
}) | ||
|
||
it('treats unhandled interceptor errors as 500 responses', async () => { | ||
interceptor.on('request', () => { | ||
throw new Error('Custom error') | ||
}) | ||
|
||
const request = http.get('http://localhost/resource') | ||
const { res, text } = await waitForClientRequest(request) | ||
|
||
expect(res.statusCode).toBe(500) | ||
expect(res.statusMessage).toBe('Unhandled Exception') | ||
expect(JSON.parse(await text())).toEqual({ | ||
name: 'Error', | ||
message: 'Custom error', | ||
stack: expect.any(String), | ||
}) | ||
}) |