Skip to content

Commit

Permalink
return Promise for followRedirect, not req.init wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Nov 1, 2019
1 parent 4ce8f22 commit 7c0304f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
42 changes: 14 additions & 28 deletions packages/server/lib/request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ module.exports = (options = {}) ->
cookies = [cookies]

parsedUrl = url.parse(resUrl)
debug('setting cookies on browser %o', { url: parsedUrl, cookies })
debug('setting cookies on browser %o', { url: parsedUrl.href, cookies })

Promise.map cookies, (cookie) ->
cookie = tough.Cookie.parse(cookie, { loose: true })
Expand Down Expand Up @@ -478,24 +478,18 @@ module.exports = (options = {}) ->
currentUrl = options.url

options.followRedirect = (incomingRes) ->
req = @

newUrl = url.resolve(currentUrl, incomingRes.headers.location)

## and when we know we should follow the redirect
## we need to override the init method and
## first set the received cookies on the browser
## and then grab the cookies for the new url
req.init = _.wrap req.init, (orig, opts) =>
options.onBeforeReqInit ->
self.setCookiesOnBrowser(incomingRes, currentUrl, automationFn)
.then (cookies) ->
self.setRequestCookieHeader(req, newUrl, automationFn)
.then (cookieHeader) ->
currentUrl = newUrl
orig.call(req, opts)

followRedirect.call(req, incomingRes)
self.setCookiesOnBrowser(incomingRes, currentUrl, automationFn)
.then (cookies) =>
self.setRequestCookieHeader(@, newUrl, automationFn)
.then =>
currentUrl = newUrl
true

@setRequestCookieHeader(options, options.url, automationFn)
.then =>
Expand Down Expand Up @@ -565,24 +559,16 @@ module.exports = (options = {}) ->

push(incomingRes)

req = @

## and when we know we should follow the redirect
## we need to override the init method and
## first set the new cookies on the browser
## and then grab the cookies for the new url
req.init = _.wrap req.init, (orig, opts) =>
self.setCookiesOnBrowser(incomingRes, currentUrl, automationFn)
.then ->
self.setRequestCookieHeader(req, newUrl, automationFn)
.then ->
currentUrl = newUrl
orig.call(req, opts)

## cause the redirect to happen
## but swallow up the incomingRes
## so we can build an array of responses
return true
self.setCookiesOnBrowser(incomingRes, currentUrl, automationFn)
.then =>
self.setRequestCookieHeader(@, newUrl, automationFn)
.then =>
currentUrl = newUrl
true

@create(options, true)
.then(@normalizeResponse.bind(@, push))
Expand All @@ -602,7 +588,7 @@ module.exports = (options = {}) ->
## the current url
resp.redirectedToUrl = url.resolve(options.url, loc)

@setCookiesOnBrowser(resp, options.url, automationFn)
@setCookiesOnBrowser(resp, currentUrl, automationFn)
.return(resp)

if c = options.cookies
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"pumpify": "1.5.1",
"ramda": "0.24.1",
"randomstring": "1.1.5",
"request": "2.88.0",
"request": "cypress-io/request#47cdc67085c9fddc8d39d3172538f3f86c96bb8b",
"request-promise": "4.2.4",
"return-deep-diff": "0.3.0",
"sanitize-filename": "1.6.3",
Expand Down

0 comments on commit 7c0304f

Please sign in to comment.