Skip to content

Commit ce3ab1f

Browse files
authored
fix(getResponse): support resolutionContext argument (#2543)
1 parent 13e52aa commit ce3ab1f

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/core/getResponse.test.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
1-
/**
2-
* @vitest-environment node
3-
*/
1+
// @vitest-environment node
42
import { http } from './http'
53
import { getResponse } from './getResponse'
64

75
it('returns undefined given empty headers array', async () => {
8-
expect(
9-
await getResponse([], new Request('http://localhost/')),
10-
).toBeUndefined()
6+
await expect(
7+
getResponse([], new Request('http://localhost/')),
8+
).resolves.toBeUndefined()
119
})
1210

1311
it('returns undefined given no matching handlers', async () => {
14-
expect(
15-
await getResponse(
12+
await expect(
13+
getResponse(
1614
[http.get('/product', () => void 0)],
1715
new Request('http://localhost/user'),
1816
),
19-
).toBeUndefined()
17+
).resolves.toBeUndefined()
2018
})
2119

2220
it('returns undefined given a matching handler that returned no response', async () => {
23-
expect(
24-
await getResponse(
21+
await expect(
22+
getResponse(
2523
[http.get('*/user', () => void 0)],
2624
new Request('http://localhost/user'),
2725
),
28-
).toBeUndefined()
26+
).resolves.toBeUndefined()
2927
})
3028

3129
it('returns undefined given a matching handler that returned explicit undefined', async () => {
32-
expect(
33-
await getResponse(
30+
await expect(
31+
getResponse(
3432
[http.get('*/user', () => undefined)],
3533
new Request('http://localhost/user'),
3634
),
37-
).toBeUndefined()
35+
).resolves.toBeUndefined()
3836
})
3937

4038
it('returns the response returned from a matching handler', async () => {
@@ -45,7 +43,7 @@ it('returns the response returned from a matching handler', async () => {
4543

4644
expect(response?.status).toBe(200)
4745
expect(response?.headers.get('Content-Type')).toBe('application/json')
48-
expect(await response?.json()).toEqual({ name: 'John' })
46+
await expect(response?.json()).resolves.toEqual({ name: 'John' })
4947
})
5048

5149
it('returns the response from the first matching handler if multiple match', async () => {
@@ -59,5 +57,18 @@ it('returns the response from the first matching handler if multiple match', asy
5957

6058
expect(response?.status).toBe(200)
6159
expect(response?.headers.get('Content-Type')).toBe('application/json')
62-
expect(await response?.json()).toEqual({ name: 'John' })
60+
await expect(response?.json()).resolves.toEqual({ name: 'John' })
61+
})
62+
63+
it('supports custom base url', async () => {
64+
const response = await getResponse(
65+
[http.get('/resource', () => new Response('hello world'))],
66+
new Request('https://localhost:3000/resource'),
67+
{
68+
baseUrl: 'https://localhost:3000/',
69+
},
70+
)
71+
72+
expect(response?.status).toBe(200)
73+
await expect(response?.text()).resolves.toBe('hello world')
6374
})

src/core/getResponse.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
import { createRequestId } from '@mswjs/interceptors'
22
import type { RequestHandler } from './handlers/RequestHandler'
3-
import { executeHandlers } from './utils/executeHandlers'
3+
import {
4+
executeHandlers,
5+
type ResponseResolutionContext,
6+
} from './utils/executeHandlers'
47

58
/**
69
* Finds a response for the given request instance
710
* in the array of request handlers.
811
* @param handlers The array of request handlers.
912
* @param request The `Request` instance.
13+
* @param resolutionContext Request resolution options.
1014
* @returns {Response} A mocked response, if any.
1115
*/
1216
export const getResponse = async (
1317
handlers: Array<RequestHandler>,
1418
request: Request,
19+
resolutionContext?: ResponseResolutionContext,
1520
): Promise<Response | undefined> => {
1621
const result = await executeHandlers({
1722
request,
1823
requestId: createRequestId(),
1924
handlers,
25+
resolutionContext,
2026
})
2127

2228
return result?.response

0 commit comments

Comments
 (0)