Any example to implement event stream mocking
See original GitHub issueHi, Working on a project which requires HTTP mocking. Things works so far very well with the mockttp
.
The only thing I am struggling with right now to implement the event stream. If we can have access to the response
object that we can pass to our code which write events periodically that would solve the problem.
Any suggestions on how to do it the right way with mockttp
?
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Mocking server sent events: Development and CI
In Javascript you can make use of the EventSource API . A backend should respond with some specific headers: Content-Type -> 'text/event-stream' ...
Read more >How to stub a stream function - Simple Engineering
This blog layout some key elements necessary to be successful when mocking stream API. We keep in mind that there is a clear...
Read more >Some mocking tricks with jest - Ben Smithgall
Read streams. Read streams are relatively straightforward to use. For example, a GCS File could be mocked as follows:.
Read more >node.js - How to mock streams in NodeJS - Stack Overflow
There's a simpler way: stream.PassThrough. I've just found Node's very easy to miss stream.PassThrough class, which I believe is what you're ...
Read more >Stubbing and Mocking in Java with the Spock Testing ...
Learn how to create true Java unit tests by mocking all external dependencies in your unit tests with the Spock testing framework.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, exactly. I think maybe you’ve misunderstood my example? That’s effectively the same as what I’m proposing in the example above.
The only difference is that in that case, we return the stream as another type of value for the existing body field, instead of a adding a new separate
stream
field. Does that make sense? It’s useful that way because it’s never meaningful to return both at the same time, and returning a stream there is unambiguous (because it’s currently not a valid value for that field).It’s also better to start with just
rawBody
, notbody
, to make it clear that the stream data will not be automatically encoded (we can do that later, but it’s not needed immediately, and it’s not easy). We can do both later though, to support both pre-encoded & unencoded body streams.Anyway, yust to make it clearer, I’m proposing that this would work:
Is that clearer?
Ah, I see, that makes sense. Yes, I think that’s not really supported right now. You could try creating many rules each matching different sets of parameters and returning different streams, but that won’t work for many cases (e.g. completely unknown freeform query params) and it’s quite awkward.
In future, I think this would probably be best supported by allowing it within
thenCallback
, with something like:Doing this perfectly is a little harder than it sounds in some places - for example in the general case, the stream needs to be automatically encoded, whilst it’s streaming, using the appropriate compression algorithm (e.g. gzip). I think we can avoid that initially, but we’d need to do so explicitly…
How about we extend the result type from callbacks, so that the
rawBody
field can be returned with either a Buffer, or a Buffer stream?rawBody
is never encoded - if you use it, you’re responsible for handling encoding yourself - but I think that’s probably fine for your case. We could then add stream support with automatic encoding in thebody
parameter later on, if necessary, with no breaking changes.I’m open to something like this. I’m unlikely to have much time in the short-term to look into it myself, but PRs are very welcome.