question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any example to implement event stream mocking

See original GitHub issue

Hi, 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:open
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pimterrycommented, Aug 1, 2022

Currently the thenCallback supports different options to return, e.g. json, body. What if we can return stream from there? We can prepare the stream within thenCallback and return like return { stream: myStream }.

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, not body, 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:

server.forGet('/')
  .thenCallback((req) => {
    // Inside the callback, use 'req' to build a readable stream
    // for the body data:
    const yourStream = generateAStream(req);

    return {
        statusCode: 200,
        // If you return a stream as the body here, then the HTTP
        // response body will stream from this, and continue until
        // your stream ends:
        rawBody: yourStream
    };
  );

Is that clearer?

1reaction
pimterrycommented, Jul 28, 2022

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:

server.forGet('/')
  .thenCallback((req) => {
    return {
      statusCode: 200,
      body: buildCustomStream(req)
    };
  );

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 the body 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found