-
Notifications
You must be signed in to change notification settings - Fork 88
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
Any example to implement event stream mocking #98
Comments
Hi @nazarhussain. To be honest, this isn't well supported, but I think it should be possible for simple cases using If not, can you explain more about what you're trying to do? I'm definitely interested in better supporting this in future, and it'd be useful to understand the use cases that are important. |
Yes, I tried
The use-case I have is the following:
We are using fastify for actual implementation and I wanted to mock to test some complex scenarios. Here you can find the reference to real handling of those event streams. |
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 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 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. |
How about we have |
I'm not sure exactly what you mean, can you show an example? |
Currently the |
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 It's also better to start with just 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? |
Hi @pimterry! I took a stab at this and tried to change Would having a stream be useful at all in EDIT: Or, maybe read the whole stream into a buffer in |
Opened a PR for this feature. I'd love to get some feedback on it. Thanks! |
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
?The text was updated successfully, but these errors were encountered: