Skip to content

Commit

Permalink
Merge pull request #1327 from capricorn86/1325-symbolnodestream-error…
Browse files Browse the repository at this point in the history
…-message-with-msw

fix: [#1325] Fixes problem related to cloning a response without a body
  • Loading branch information
capricorn86 committed Mar 18, 2024
2 parents 06a558f + d924104 commit bedb752
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/happy-dom/src/fetch/utilities/FetchBodyUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class FetchBodyUtility {
* @returns New stream.
*/
public static cloneBodyStream(requestOrResponse: {
body: ReadableStream;
body: ReadableStream | null;
bodyUsed: boolean;
}): ReadableStream {
if (requestOrResponse.bodyUsed) {
Expand All @@ -113,6 +113,10 @@ export default class FetchBodyUtility {
);
}

if (requestOrResponse.body === null || requestOrResponse.body === undefined) {
return null;
}

// If a buffer is set, use it to create a new stream.
if (requestOrResponse[PropertySymbol.buffer]) {
return this.toReadableStream(requestOrResponse[PropertySymbol.buffer]);
Expand Down
34 changes: 34 additions & 0 deletions packages/happy-dom/test/fetch/Response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,40 @@ describe('Response', () => {
expect(bodyText).toBe('Hello World');
});

it('Can clone Response without body.', async () => {
const response = new window.Response(null, {
status: 404,
statusText: 'Not Found',
headers: { 'Content-Type': 'text/plain' }
});

const clone = response.clone();

expect(clone).not.toBe(response);
expect(clone.status).toBe(404);
expect(clone.statusText).toBe('Not Found');
expect(clone.headers.get('Content-Type')).toBe('text/plain');
});

it('Can clone Response with empty string as body.', async () => {
const response = new window.Response('', {
status: 404,
statusText: 'Not Found',
headers: { 'Content-Type': 'text/plain' }
});

const clone = response.clone();

expect(clone).not.toBe(response);
expect(clone.status).toBe(404);
expect(clone.statusText).toBe('Not Found');
expect(clone.headers.get('Content-Type')).toBe('text/plain');

const bodyText = await clone.text();

expect(bodyText).toBe('');
});

it('Can use the body of the cloned Response with a buffer independently.', async () => {
const originalResponse = new window.Response('Hello World', {
status: 200,
Expand Down

0 comments on commit bedb752

Please sign in to comment.