However, I noticed that my tests that were inspecting the Content-Type response header weren't behaving as expected. While debugging, I noticed that after I cloned the Response object with new Response(response.body, response), the resulting object did not have the response header. This seems to be the recommended way to clone the Response so that headers can be mutated, but I'm wondering if there's a way to get the test to behave the same way as Cloudflare does (keeping the headers in this circumstance).
Here's how you can see the issue in action if you run this code within a test:
One possible workaround is to add code that does response.clone() when running a test instead of new Response(response.body, response), but that's not idea since it clutters the actual code. You can't use response.clone() in production because you still can't mutate the headers of the cloned Response object when you run the code in a real Cloudflare Worker.
Is there any way to get the test to behave more like Cloudflare Workers does?
I have some test code that sets up a mock response like this:
However, I noticed that my tests that were inspecting the
Content-Type
response header weren't behaving as expected. While debugging, I noticed that after I cloned theResponse
object withnew Response(response.body, response)
, the resulting object did not have the response header. This seems to be the recommended way to clone theResponse
so that headers can be mutated, but I'm wondering if there's a way to get the test to behave the same way as Cloudflare does (keeping the headers in this circumstance).Here's how you can see the issue in action if you run this code within a test:
One possible workaround is to add code that does
response.clone()
when running a test instead ofnew Response(response.body, response)
, but that's not idea since it clutters the actual code. You can't useresponse.clone()
in production because you still can't mutate the headers of the clonedResponse
object when you run the code in a real Cloudflare Worker.Is there any way to get the test to behave more like Cloudflare Workers does?