Skip to content

Commit

Permalink
Fix not keeping track of correct URL when setting cookies durin… (#5455)
Browse files Browse the repository at this point in the history
* Keep track of correct URL during request redirects

* Add snapshots for URLs during redirects
  • Loading branch information
flotwig committed Oct 25, 2019
1 parent fcc252e commit 4b278f8
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 5 deletions.
65 changes: 65 additions & 0 deletions packages/server/__snapshots__/request_spec.coffee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
exports['lib/request #sendPromise followRedirect gets + attaches the cookies at each redirect 1'] = {
"setCalls": [
{
"currentUrl": "http://localhost:1234/",
"setCookie": "foo=bar"
},
{
"currentUrl": "http://localhost:1234/B",
"setCookie": "bar=baz"
},
{
"currentUrl": "http://localhost:1234/B",
"setCookie": "foo=bar"
},
{
"currentUrl": "http://localhost:1234/",
"setCookie": "quuz=quux"
}
],
"getCalls": [
{
"newUrl": "http://localhost:1234/"
},
{
"newUrl": "http://localhost:1234/B"
},
{
"newUrl": "http://localhost:1234/B"
},
{
"newUrl": "http://localhost:1234/B"
}
]
}

exports['lib/request #sendStream gets + attaches the cookies at each redirect 1'] = {
"setCalls": [
{
"currentUrl": "http://localhost:1234/",
"setCookie": "foo=bar"
},
{
"currentUrl": "http://localhost:1234/B",
"setCookie": "bar=baz"
},
{
"currentUrl": "http://localhost:1234/B",
"setCookie": "foo=bar"
}
],
"getCalls": [
{
"newUrl": "http://localhost:1234/"
},
{
"newUrl": "http://localhost:1234/B"
},
{
"newUrl": "http://localhost:1234/B"
},
{
"newUrl": "http://localhost:1234/B"
}
]
}
16 changes: 11 additions & 5 deletions packages/server/lib/request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ module.exports = (options = {}) ->
setRequestCookieHeader: (req, reqUrl, automationFn) ->
automationFn('get:cookies', { url: reqUrl })
.then (cookies) ->
debug('getting cookies from browser %o', { reqUrl, cookies })
debug('got cookies from browser %o', { reqUrl, cookies })
header = cookies.map (cookie) ->
"#{cookie.name}=#{cookie.value}"
.join("; ") || undefined
Expand Down Expand Up @@ -475,21 +475,24 @@ module.exports = (options = {}) ->

followRedirect = options.followRedirect

currentUrl = options.url

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

newUrl = url.resolve(options.url, incomingRes.headers.location)
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, options.url, automationFn)
self.setCookiesOnBrowser(incomingRes, currentUrl, automationFn)
.then (cookies) ->
self.setRequestCookieHeader(req, newUrl, automationFn)
.then (cookieHeader) ->
currentUrl = newUrl
orig.call(req, opts)

followRedirect.call(req, incomingRes)
Expand Down Expand Up @@ -552,8 +555,10 @@ module.exports = (options = {}) ->
requestResponses.push(pick(response))

if options.followRedirect
currentUrl = options.url

options.followRedirect = (incomingRes) ->
newUrl = url.resolve(options.url, incomingRes.headers.location)
newUrl = url.resolve(currentUrl, incomingRes.headers.location)

## normalize the url
redirects.push([incomingRes.statusCode, newUrl].join(": "))
Expand All @@ -567,10 +572,11 @@ module.exports = (options = {}) ->
## 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, options.url, automationFn)
self.setCookiesOnBrowser(incomingRes, currentUrl, automationFn)
.then ->
self.setRequestCookieHeader(req, newUrl, automationFn)
.then ->
currentUrl = newUrl
orig.call(req, opts)

## cause the redirect to happen
Expand Down
54 changes: 54 additions & 0 deletions packages/server/test/unit/request_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,44 @@ require("../spec_helper")
_ = require("lodash")
http = require("http")
Request = require("#{root}lib/request")
snapshot = require("snap-shot-it")

request = Request({timeout: 100})

testAttachingCookiesWith = (fn) ->
set = sinon.spy(request, 'setCookiesOnBrowser')
get = sinon.spy(request, 'setRequestCookieHeader')

nock("http://localhost:1234")
.get("/")
.reply(302, "", {
'set-cookie': 'foo=bar'
location: "/B"
})
.get("/B")
.reply(302, "", {
'set-cookie': 'bar=baz'
location: "/B"
})
.get("/B")
.reply(200, "", {
'set-cookie': 'quuz=quux'
})

fn()
.then ->
snapshot({
setCalls: set.getCalls().map (call) ->
{
currentUrl: call.args[1],
setCookie: call.args[0].headers['set-cookie']
}
getCalls: get.getCalls().map (call) ->
{
newUrl: _.get(call, 'args.1')
}
})

describe "lib/request", ->
beforeEach ->
@fn = sinon.stub()
Expand Down Expand Up @@ -722,6 +757,12 @@ describe "lib/request", ->
expect(resp.status).to.eq(200)
expect(resp).not.to.have.property("redirectedToUrl")

it "gets + attaches the cookies at each redirect", ->
testAttachingCookiesWith =>
request.sendPromise({}, @fn, {
url: "http://localhost:1234/"
})

context "form=true", ->
beforeEach ->
nock("http://localhost:8080")
Expand Down Expand Up @@ -843,3 +884,16 @@ describe "lib/request", ->
beginFn()
expect(request.create).to.be.calledOnce
expect(request.create).to.be.calledWith(options)

it "gets + attaches the cookies at each redirect", ->
testAttachingCookiesWith =>
request.sendStream({}, @fn, {
url: "http://localhost:1234/"
followRedirect: _.stubTrue
})
.then (fn) =>
req = fn()

new Promise (resolve, reject) =>
req.on('response', resolve)
req.on('error', reject)

4 comments on commit 4b278f8

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4b278f8 Oct 25, 2019

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/linux-x64/circle-develop-4b278f89de2752e33c047620a86ee76da9ea5011-174344/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.1/circle-develop-4b278f89de2752e33c047620a86ee76da9ea5011-174335/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4b278f8 Oct 25, 2019

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/win32-ia32/appveyor-develop-4b278f89de2752e33c047620a86ee76da9ea5011-28386135/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.1/win32-ia32/appveyor-develop-4b278f89de2752e33c047620a86ee76da9ea5011-28386135/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4b278f8 Oct 25, 2019

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/win32-x64/appveyor-develop-4b278f89de2752e33c047620a86ee76da9ea5011-28386135/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.5.1/win32-x64/appveyor-develop-4b278f89de2752e33c047620a86ee76da9ea5011-28386135/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4b278f8 Oct 25, 2019

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/3.5.1/darwin-x64/circle-develop-4b278f89de2752e33c047620a86ee76da9ea5011-174349/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.5.1/circle-develop-4b278f89de2752e33c047620a86ee76da9ea5011-174347/cypress.tgz

Please sign in to comment.