Skip to content

Commit

Permalink
Merge pull request #1296 from capricorn86/1294-cloning-a-response-twi…
Browse files Browse the repository at this point in the history
…ce-locks-the-readablestream

fix: [#1294] Fixes problem related to cloning a cloned Response in Re…
  • Loading branch information
capricorn86 committed Mar 11, 2024
2 parents efce30d + 6e87ead commit 54d1ae0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/happy-dom/src/fetch/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
(<boolean>response.ok) = this.ok;
(<boolean>response.redirected) = this.redirected;
(<string>response.type) = this.type;
Expand Down
22 changes: 21 additions & 1 deletion packages/happy-dom/test/fetch/Response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 () => {
Expand Down

0 comments on commit 54d1ae0

Please sign in to comment.