-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(xhr): handle responses without body correctly (#411)
Co-authored-by: avivasyuta <[email protected]> Co-authored-by: Artem Zakharchenko <[email protected]>
- Loading branch information
1 parent
bfac018
commit d4257a5
Showing
2 changed files
with
104 additions
and
1 deletion.
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
89 changes: 89 additions & 0 deletions
89
test/modules/XMLHttpRequest/response/xhr-response-without-body.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,89 @@ | ||
// @vitest-environment jsdom | ||
import { afterAll, afterEach, beforeAll, expect, it, vi } from 'vitest' | ||
import { HttpServer } from '@open-draft/test-server/http' | ||
import { XMLHttpRequestInterceptor } from '../../../../src/interceptors/XMLHttpRequest' | ||
import { createXMLHttpRequest, useCors } from '../../../helpers' | ||
import type { HttpRequestEventMap } from '../../../../src' | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.use(useCors) | ||
app.get('/:statusCode', (req, res) => | ||
res.status(+req.params.statusCode).end() | ||
) | ||
}) | ||
|
||
const interceptor = new XMLHttpRequestInterceptor() | ||
|
||
const responseListener = vi.fn<HttpRequestEventMap['response']>() | ||
interceptor.on('response', responseListener) | ||
|
||
beforeAll(async () => { | ||
await httpServer.listen() | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
await httpServer.close() | ||
}) | ||
|
||
it('represents a 204 response without body using fetch api response', async () => { | ||
const request = await createXMLHttpRequest((request) => { | ||
request.open('GET', httpServer.http.url('/204')) | ||
request.send() | ||
}) | ||
|
||
expect(request.response).toBe('') | ||
expect(responseListener).toHaveBeenNthCalledWith( | ||
1, | ||
expect.objectContaining({ | ||
response: expect.objectContaining({ | ||
status: 204, | ||
body: null, | ||
} satisfies Partial<Response>), | ||
}) | ||
) | ||
expect(responseListener).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('represents a 205 response without body using fetch api response', async () => { | ||
const request = await createXMLHttpRequest((request) => { | ||
request.open('GET', httpServer.http.url('/205')) | ||
request.send() | ||
}) | ||
|
||
expect(request.response).toBe('') | ||
expect(responseListener).toHaveBeenNthCalledWith( | ||
1, | ||
expect.objectContaining({ | ||
response: expect.objectContaining({ | ||
status: 205, | ||
body: null, | ||
} satisfies Partial<Response>), | ||
}) | ||
) | ||
expect(responseListener).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('represents a 304 response without body using fetch api response', async () => { | ||
const request = await createXMLHttpRequest((request) => { | ||
request.open('GET', httpServer.http.url('/304')) | ||
request.send() | ||
}) | ||
|
||
expect(request.response).toBe('') | ||
expect(responseListener).toHaveBeenNthCalledWith( | ||
1, | ||
expect.objectContaining({ | ||
response: expect.objectContaining({ | ||
status: 304, | ||
body: null, | ||
} satisfies Partial<Response>), | ||
}) | ||
) | ||
expect(responseListener).toHaveBeenCalledTimes(1) | ||
}) |