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(rest): support ReadableStream as response body #974

Closed
wants to merge 1 commit into from

Conversation

Aprillion
Copy link
Sponsor Contributor

@Aprillion Aprillion commented Nov 7, 2021

Fixes #581 .. but in a totally hacky* way and only tested with a stream of string chunks.

If improvements are needed before merge, please feel free to edit the PR or close it and just take inspiration for some better solution 😅 But I'm afraid I won't have more time until next weekend.

*hacky => original code counts that there will be only 1 message from client, and that will resolve a Promise... but when multiple messages come back, only one of them can resolve a promise - but even after the Promise is resolved, the onmessage handler lives inside the closure of a new Promise function, so it's a bit unexpected that we can handle multiple messages there even AFTER the Promise is resolved

src/mockServiceWorker.js Outdated Show resolved Hide resolved
@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 7, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 11152dc:

Sandbox Source
MSW React Configuration

@Aprillion
Copy link
Sponsor Contributor Author

yarn test:smoke is passing locally, but failing in CI with Chrome Headless 83.0.4103.61 .. is there some setup I can do to reproduce the failure?

src/mockServiceWorker.js Outdated Show resolved Hide resolved
*/

test('Node does not support ReadableStream yet', () => {
expect(typeof ReadableStream).toBe('undefined')
Copy link
Member

Choose a reason for hiding this comment

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

This test needs to be rewritten, it doesn't test anything as of yet.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

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

the idea was to fail the test if Node adds ReadableStream ¯\_(ツ)_/¯ if you have something in mind that can be actually tested, that would be awesome!

rest.get('/sse', (req, res, ctx) => {
return res(
ctx.status(200),
ctx.set('Content-Type', 'text/plain'),
Copy link
Member

Choose a reason for hiding this comment

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

I believe that such a response must also specify that its body is a stream.

Copy link
Sponsor Contributor Author

@Aprillion Aprillion Nov 8, 2021

Choose a reason for hiding this comment

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

apparently not necessary for a stream of text/plain, at least in the integration test setup..

const res = await request('/sse')
const status = res.status()
const headers = await res.allHeaders()
const text = await res.text()
Copy link
Member

Choose a reason for hiding this comment

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

We need to ensure that res.text() actually verifies that you can read that response stream once more on the usage surface (assert that we're correctly piping the original response stream into PassThrough in the worker).

@kettanaito
Copy link
Member

I'm going to close this in favor of #1288. This proposed change has served as an excellent example of how to reconstruct a readable stream within the worker. I've gone with a slightly different approach but this was tremendously helpful.

@kettanaito kettanaito closed this Jun 22, 2022
@kettanaito
Copy link
Member

Released: v0.43.0 🎉

This has been released in v0.43.0!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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.

ReadableStream object could not be cloned when trying to use stream as a mocked response body
2 participants