Skip to content
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(ClientRequest): prevent requests issued by "follow-redirects" from hanging infinitely #260

Merged
merged 6 commits into from
Jun 9, 2022

Conversation

95th
Copy link
Contributor

@95th 95th commented Jun 5, 2022

Changes

  • Invokes each individual .write() callback immediately vs doing so in the .end() of NodeClientRequest.

@95th 95th changed the title fix: fix the hung request in https request call fix: fix the hung request in follow-redirect's https request call Jun 5, 2022
expect(request.body).toBe(JSON.stringify({ todo: 'Buy the milk' }))

// Response (original).
expect(await text()).toBe('hello from the server')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added assertion for the received response so we're sure that follow-redirects was able to redirect the request to the server.


req.end(payload)

const { text } = await waitForClientRequest(req as any)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this await line so whenever this request rejects, the test rejects also. Previously, the test would pass if requesting a non-existing resource/hostname.

const { address } = server.https
const payload = JSON.stringify({ todo: 'Buy the milk' })

const req = https.request({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@95th, do you know what's the intention behind using follow-redirect usually is?

I somehow suspected that this package is meant for HTTP -> HTTPS automatic redirects but I'm probably wrong. I wonder if we're reflecting how people use follow-redirects in this test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, my initial assumption about follow-redirects was wrong.

Having read their readme, I can see that the purpose it exists is to automatically follow redirects that occur during requests. For example:

GET https://bit.ly/abc-123
- (redirects to https://api.github.com/user

RESPONSE:
- From GitHub, not bit.ly

I will restructure our test so reflect this usage scenario.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that it may feel unnecessary to assert that follow-redirects actually perform a redirect request but we should still do so. In this assertion, we're making sure that Interceptors don't interfere with that behavior, and that the redirects still happen.

if (!chunk || chunk.length === 0) {
this.log('written chunk is empty, skipping callback...')
} else {
callback?.()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for posterity: so instead of scoping the custom write callback in the this.chunks and replaying those in .end(), we're calling write callbacks immediately now. Since we do, now intercepted requests won't get stuck due to follow-redirects calling req.end() as a part of req.write() callback (previously, pending indefinitely).

@kettanaito kettanaito changed the title fix: fix the hung request in follow-redirect's https request call fix(ClientRequest): prevent requests issued by "follow-redirect" from hanging infinitely Jun 9, 2022
@kettanaito kettanaito changed the title fix(ClientRequest): prevent requests issued by "follow-redirect" from hanging infinitely fix(ClientRequest): prevent requests issued by "follow-redirects" from hanging infinitely Jun 9, 2022
Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for tackling this, @95th! 🎉 Welcome to contributors.

If you find anything else in the MSW ecosystem that interests you, or you feel you could improve, don't hesitate to ping me in the respective issues, or open a pull request.

@kettanaito kettanaito merged commit aa4d55f into mswjs:main Jun 9, 2022
@95th
Copy link
Contributor Author

95th commented Jun 9, 2022

@kettanaito Thanks for the fix up and merging. Any ETA on when it will become published and propagate to msw ?
I am currently blocked at my org from using msw because of this.

@kettanaito
Copy link
Member

Everything in the MSW ecosystem is released nightly. So, tonight.

I am currently blocked at my org from using msw because of this.

You can always resolve a dependency at a specific PR state. Use Yarn resolutions or NPM overrides to do that.

@kettanaito
Copy link
Member

Released: v0.16.5 🎉

This has been released in v0.16.5!

Make sure to always update to the latest version (npm i @mswjs/interceptors@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request hangs forever with ClientRequest
2 participants