-
Notifications
You must be signed in to change notification settings - Fork 551
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
onBodySent is not called in mockDispatch #3843
Comments
Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. |
FWIW, This can be achieved in the same way with the current interceptor setup. e.g. return (dispatch: Dispatcher['dispatch']) => {
return function intercept(requestOptions: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandlers) {
// if body is stream, attach to it
requestOptions.body.on('data', () => { /* do something */ })
return dispatch(requestOptions, new LoggingHandler(requestOptions, handler));
}; |
@metcoder95 I appreciate the quick response. I don't think this works for fetch since it's requestOptions.body is an AsyncGenerator: Line 1845 in 444a0af
I tried wrapping in a Readable.from, but ran into other issues with content-length being set in the headers. |
This feels pretty bad, but it seems to work: export function createLoggingInterceptor({ projectId, name }: LoggingOptions) {
const logging = new Logging({ projectId, autoRetry: true, maxRetries: 3 });
const log = logging.log(name);
return (dispatch: Dispatcher['dispatch']) => {
return function Logging(requestOptions: Dispatcher.DispatchOptions, handler: Dispatcher.DispatchHandlers) {
let body1: PassThrough | undefined;
let body2: PassThrough | undefined;
if (requestOptions.body) {
body1 = new PassThrough();
body2 = new PassThrough();
const body = Readable.from(requestOptions.body); // requestOptions.body is an AsyncGenerator and can only be read once
body.pipe(body1);
body.pipe(body2);
}
return dispatch(
{ ...requestOptions, body: body1 },
new LoggingHandler({ log }, { ...requestOptions, body: body2 }, handler),
);
};
};
} |
Yeah, AsyncGenerators cannot be re-used/consumed in parallel. Can you elaborate why bad? The interceptors API has been made for these kind of purposes |
The onBodySent callback is more straightforward than having to tee the body that is passed into dispatch/handler. Advocating for keeping the event since request/fetch have different request bodies. onBodySent(chunk: any, totalBytesSent: number) {
this.requestBody.push(chunk);
} |
cc: @ronag |
I think we have dropped |
Bug Description
onBodySent is not called when using the MockAgent
Reproducible By
Expected Behavior
onBodySent would be called when making a POST request
Additional context
Looks like it should be called here:
undici/lib/mock/mock-utils.js
Line 317 in a73fd09
onBodySent future?
Concerned about these future changes which remove onBodySent. Not sure how I'd easily get the request body in an interceptor in the next branch. Mostly creating this issue to figure out what I should be doing instead going forward.
#2723 (comment)
The text was updated successfully, but these errors were encountered: