-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(xhr): clone the request before calculating its body size (#632)
Co-authored-by: Artem Zakharchenko <[email protected]>
- Loading branch information
1 parent
4227c6e
commit 44276ef
Showing
2 changed files
with
43 additions
and
2 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
41 changes: 41 additions & 0 deletions
41
test/modules/XMLHttpRequest/regressions/xhr-request-body-length.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,41 @@ | ||
// @vitest-environment jsdom | ||
import { vi, beforeAll, afterEach, afterAll, it, expect } from 'vitest' | ||
import { XMLHttpRequestInterceptor } from '../../../../src/interceptors/XMLHttpRequest' | ||
import { createXMLHttpRequest } from '../../../helpers' | ||
|
||
const interceptor = new XMLHttpRequestInterceptor() | ||
|
||
beforeAll(() => { | ||
interceptor.apply() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor.removeAllListeners() | ||
}) | ||
|
||
afterAll(() => { | ||
interceptor.dispose() | ||
}) | ||
|
||
it('does not lock the request body stream when calculating the body size', async () => { | ||
interceptor.on('request', async ({ request, controller }) => { | ||
// Read the request body in the interceptor. | ||
const buffer = await request.arrayBuffer() | ||
controller.respondWith(new Response(buffer)) | ||
}) | ||
|
||
const uploadLoadStartListener = vi.fn() | ||
const request = await createXMLHttpRequest((request) => { | ||
request.upload.addEventListener('loadstart', uploadLoadStartListener) | ||
request.open('POST', '/resource') | ||
request.send('request-body') | ||
}) | ||
|
||
// Must calculate the total request body size for the upload event. | ||
const progressEvent = uploadLoadStartListener.mock.calls[0][0] | ||
expect(progressEvent.total).toBe(12) | ||
|
||
// Must be able to read the request in the interceptor | ||
// and use its body as the mocked response body. | ||
expect(request.responseText).toBe('request-body') | ||
}) |