From 6e87eadc704145345882ee880024f7d94eafa85f Mon Sep 17 00:00:00 2001 From: David Ortner Date: Tue, 12 Mar 2024 00:20:10 +0100 Subject: [PATCH] fix: [#1294] Fixes problem related to cloning a cloned Response in Response.clone() --- packages/happy-dom/src/fetch/Response.ts | 4 +++- .../happy-dom/test/fetch/Response.test.ts | 22 ++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/happy-dom/src/fetch/Response.ts b/packages/happy-dom/src/fetch/Response.ts index 098387825..3f5a47f21 100644 --- a/packages/happy-dom/src/fetch/Response.ts +++ b/packages/happy-dom/src/fetch/Response.ts @@ -45,7 +45,7 @@ export default class Response implements IResponse { public readonly ok: boolean; public readonly headers: IHeaders; public [PropertySymbol.cachedResponse]: ICachedResponse | null = null; - public readonly [PropertySymbol.buffer]: Buffer | null = null; + public [PropertySymbol.buffer]: Buffer | null = null; readonly #window: IBrowserWindow; readonly #browserFrame: IBrowserFrame; @@ -275,6 +275,8 @@ export default class Response implements IResponse { headers: this.headers }); + response[PropertySymbol.cachedResponse] = this[PropertySymbol.cachedResponse]; + response[PropertySymbol.buffer] = this[PropertySymbol.buffer]; (response.ok) = this.ok; (response.redirected) = this.redirected; (response.type) = this.type; diff --git a/packages/happy-dom/test/fetch/Response.test.ts b/packages/happy-dom/test/fetch/Response.test.ts index 12ef46e3a..dca724b32 100644 --- a/packages/happy-dom/test/fetch/Response.test.ts +++ b/packages/happy-dom/test/fetch/Response.test.ts @@ -488,7 +488,24 @@ describe('Response', () => { expect(bodyText).toBe('Hello World'); }); - it('Can use the body of the cloned Response independently (cached).', async () => { + it('Can use the body of the cloned Response with a buffer independently.', async () => { + const originalResponse = new window.Response('Hello World', { + status: 200, + statusText: 'OK', + headers: { 'Content-Type': 'text/plain' } + }); + const clonedResponse = originalResponse.clone(); + const clonedResponse2 = clonedResponse.clone(); + + const originalResponseText = await originalResponse.text(); + const clonedResponseText = await clonedResponse.text(); + const clonedResponseText2 = await clonedResponse2.text(); + expect(originalResponseText).toBe('Hello World'); + expect(clonedResponseText).toBe('Hello World'); + expect(clonedResponseText2).toBe('Hello World'); + }); + + it('Can use the body of the cloned Response with a buffer independently when cloned multiple times.', async () => { const originalResponse = new window.Response('Hello World', { status: 200, statusText: 'OK', @@ -536,11 +553,14 @@ describe('Response', () => { headers: { 'Content-Type': 'text/plain' } }); const clonedResponse = originalResponse.clone(); + const clonedResponse2 = clonedResponse.clone(); const originalResponseText = await originalResponse.text(); const clonedResponseText = await clonedResponse.text(); + const clonedResponseText2 = await clonedResponse2.text(); expect(originalResponseText).toBe('chunk1chunk2chunk3'); expect(clonedResponseText).toBe('chunk1chunk2chunk3'); + expect(clonedResponseText2).toBe('chunk1chunk2chunk3'); }); it('Fails if the body of the original Response is already used.', async () => {