service-workers icon indicating copy to clipboard operation
service-workers copied to clipboard

cloning a response copies headers by reference

Open jamesarosen opened this issue 3 years ago • 0 comments

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

jamesarosen avatar Nov 03 '21 20:11 jamesarosen