Msw: Access to Original Request on handlers.

Created on 28 Sep 2020  路  5Comments  路  Source: mswjs/msw

Hi. Thanks for this awesome tool. It is making my life easier. 馃榾

Is your feature request related to a problem? Please describe.

Given the request handler below:

rest.post('/path', (req, res, ctx) => {
    // ...
}),

The req object is slightly different in the Browser and Node environments.

In Node:

  • If the content-type is json it parses it and makes the data available on req.body.
  • Otherwise it pass the original Body from the request. With this we have the chance to receive other object responses like FormData.

In the Browser:

  • msw parses the body as text before pass it to the handler.

Describe the solution you'd like

Have a clone of the original request in the req object. This way the handler has access to the Body mixin methods to read the data in other formats: FormData, Blob, ArrayBuffer, etc.

Describe alternatives you've considered

To mock a multipart/form-data request I had to create a custom parser. It works just fine but if we have this feature available for free I guess it makes sense to passed it to the handler.

Does this makes sense? I can submit a PR with the suggested changes.

feature

Most helpful comment

Hi @marcosvega91 and @kettanaito,

First, thanks for the very fast response. I was not completely aware of the MessageChannel (I just read the WebWorker docs).
Now knowing that it was not an easy win like I initially thought, I agree with @kettanaito. Maybe this is a customization to my use case: I am saving some data in-memory to simulate the interactions when running on the browser for fast prototyping.

All 5 comments

Hi @otaciliolacerda thanks for raising this 馃槃 .

I think that the line you are referring is the following

https://github.com/mswjs/msw/blob/7585f5440fec589524626bfa70ec8ed666faf986/src/mockServiceWorker.js#L117

Under the hood MSW exchange messages from worker to client through a MessageChannel using JSON.

Supporting formData on worker side I think is not possible at the moment. Maybe the body can be parsed back in the
parseBody function. Btw I think that we should support it in someway.

Hey, @otaciliolacerda. Thank you for reaching out.

The req object is slightly different in the Browser and Node environments.

Yes, this is an intentional design decision. Requests in browser and Node are inevitably two different instances with some data unavailable in one, and only present in another. See Request.

In regards to accessing request body mixins, you can only transfer text in the MessageChannel between the worker and the client. That鈥檚 why we always resolve the body to text and occasionally add some transformations like parsing it to JSON, given the right request headers.

Adding things like req.blob()/req.formData() would be artificially recreating those mixins for the sake of only seeming compatibility. I鈥檇 be careful with decisions like this one, unless we find a suitable way not to rewrite each mixing by hand.

Hi @marcosvega91 and @kettanaito,

First, thanks for the very fast response. I was not completely aware of the MessageChannel (I just read the WebWorker docs).
Now knowing that it was not an easy win like I initially thought, I agree with @kettanaito. Maybe this is a customization to my use case: I am saving some data in-memory to simulate the interactions when running on the browser for fast prototyping.

If need be, you can re-create a Blob or FormData from the req.body text in your handlers.

Yes, this is what I am currently doing. 馃榾

Was this page helpful?
0 / 5 - 0 ratings

Related issues

veronesecoms picture veronesecoms  路  3Comments

lukesmurray picture lukesmurray  路  3Comments

luistak picture luistak  路  3Comments

abrudin picture abrudin  路  3Comments

mainfraame picture mainfraame  路  3Comments