Describe the bug
Node.js version: 16.16.0
OS version: macOS monterey
Description: When redirects are followed (via .redirects()) cookies set by the redirecting server are not persisted correctly.
This bug was introduced with #1757, which assumes that there will always be a request object set on the response received by Agent.prototype._saveCookies. However, _saveCookies is invoked by two events: response and redirect, and that assumption is only true of the response event.
Actual behavior
If a cookie without an explicit domain has already been set in Superagent's cookie jar, that cookie cannot be overwritten using the same parameters during a followed redirect.
Expected behavior
Setting a cookie with the same parameters in a followed redirect should overwrite the previous cookie.
Code to reproduce
https://gist.github.com/wilsonjackson/746aee8f563e8486fa2f99c32baba235
Checklist
Describe the bug
Node.js version: 16.16.0
OS version: macOS monterey
Description: When redirects are followed (via
.redirects()) cookies set by the redirecting server are not persisted correctly.This bug was introduced with #1757, which assumes that there will always be a
requestobject set on the response received byAgent.prototype._saveCookies. However,_saveCookiesis invoked by two events:responseandredirect, and that assumption is only true of theresponseevent.Actual behavior
If a cookie without an explicit domain has already been set in Superagent's cookie jar, that cookie cannot be overwritten using the same parameters during a followed redirect.
Expected behavior
Setting a cookie with the same parameters in a followed redirect should overwrite the previous cookie.
Code to reproduce
https://gist.github.com/wilsonjackson/746aee8f563e8486fa2f99c32baba235
Checklist