-
-
Notifications
You must be signed in to change notification settings - Fork 473
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { setupWorker, rest } from 'msw' | ||
|
||
const stream = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue('line1\n') | ||
|
||
setTimeout(() => { | ||
controller.enqueue('line2\n') | ||
controller.close() | ||
}, 0) | ||
}, | ||
}) | ||
|
||
const worker = setupWorker( | ||
rest.get('/sse', (req, res, ctx) => { | ||
return res( | ||
ctx.status(200), | ||
ctx.set('Content-Type', 'text/plain'), | ||
ctx.body(stream), | ||
) | ||
}), | ||
) | ||
|
||
worker.start() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
test('Node does not support ReadableStream yet', () => { | ||
expect(typeof ReadableStream).toBe('undefined') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! |
||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as path from 'path' | ||
import { pageWith } from 'page-with' | ||
|
||
test('responds with a ReadableStream response body', async () => { | ||
const { request } = await pageWith({ | ||
example: path.resolve(__dirname, 'body-stream.mocks.ts'), | ||
}) | ||
|
||
const res = await request('/sse') | ||
const status = res.status() | ||
const headers = await res.allHeaders() | ||
const text = await res.text() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to ensure that |
||
|
||
expect(status).toBe(200) | ||
expect(headers['content-type']).toBe('text/plain') | ||
expect(text).toBe('line1\nline2\n') | ||
}) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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..