Skip to content
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

fix: respect original req.body if it is still a buffer #22

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

robbtraister
Copy link
Contributor

This PR coerces the MockedRequest body to be a buffer from a string (or stringified object). This works well if express.json() middleware has been mounted because we need to un-parse the work the middleware performed.

However, that in effect makes the express.json() middleware required, because otherwise we are undoing work that was never done and we end up stringifying the original Uint8Array buffer, which does not work as expected.

This change preserves the original body buffer if no express.json() (or similar) middleware has been mounted, allowing the subsequent req.json() call to work as expected, regardless.

@robbtraister robbtraister marked this pull request as ready for review October 26, 2022 23:57
const requestBody =
typeof req.body === 'string' ? req.body : JSON.stringify(req.body)
req.body instanceof Uint8Array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

Please, could you also add a test for this behavior? Take a look at the existing tests for inspiration.

This scenario would come down to:

  1. Prepare the setup, have no express.json() middleware.
  2. Perform a request.
  3. Assert the correct request body.

  1. Prepare the setup, have the express.json() middleware.
  2. Perform a request.
  3. Assert that the body is still correct.

We can put these two in a single test file to reflect on this behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, sure thing!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After setting up this test, I discovered that the root cause of my issue was that I was using express.raw() instead of express.json() (as opposed to no middleware, as I originally assumed) as a workaround for #17.

In the end, some body parsing middleware is necessary, so maybe this change is unnecessary. However, I still think this change is worthwhile, as using express.raw() is technically more performant since it doesn't have to parse/unparse.

I also had to make the test setup a bit more complicated since the open-draft test-server automatically applies bodyParser.json() middleware and there is no way to disable it.

@ThijSlim
Copy link

Would be great if this gets merged, running against this issue too 👍

})

/**
* The test server created by @open-draft/test-server automatically
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is peculiar. I need to look into disabling/customizing the default middleware.

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

Successfully merging this pull request may close these issues.

None yet

3 participants