Skip to content

cloning a response copies headers by reference #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jamesarosen opened this issue Nov 3, 2021 · 0 comments
Open

cloning a response copies headers by reference #142

jamesarosen opened this issue Nov 3, 2021 · 0 comments

Comments

@jamesarosen
Copy link

jamesarosen commented Nov 3, 2021

What I expect:

// given a response:
const responseA = new Response('hello', { status: 200, headers: { Foo: 'Bar' } })

// when you clone it:
const responseB = new Response(responseA.body, responseA)

// and change the original:
responseA.headers.set('Foo', 'Baz')

// then the clone shouldn't change:
responseB.headers.get('Foo') // 'Bar'

What happens with service-worker-mock:

// given a response:
const responseA = new Response('hello', { status: 200, headers: { Foo: 'Bar' } })

// when you clone it:
const responseB = new Response(responseA.body, responseA)

// and change the original:
responseA.headers.set('Foo', 'Baz')

// then the clone changes
responseB.headers.get('Foo') // 'Baz'

The same thing happens with Request.

I believe the solution is to change

if (options.headers instanceof Headers) {
  this.headers = options.headers;

to

if (options.headers instanceof Headers) {
  this.headers = new Headers(options.headers);

in Request and Response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant