The other day I was working on some unit tests for a minimal Cloudflare Worker that would redirect the incoming request while forwarding along some of the original request headers. I setup a test to call the worker's fetch
handler with a new Request
that had some headers provided:
I then called the worker's fetch
handler with this request:
However, the headers weren't propagating into the request that my worker's fetch
handler was seeing - since I was calling the method directly I figured it had to be something with the runtime that I was writing the tests within (in this case, Bun).
I did some sleuthing online and found this issue. The creator of the issue noted something about this being baked into the spec for new Request
and new Response
objects, and that they don't "hydrate" from a HeadersInit
object.
To instead make it work, I had to write my test like this: