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

Overriding user-agent when using cy.request or cy.visit #3920

Merged
merged 4 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,15 @@ describe "src/cy/commands/navigation", ->
})
cy.contains('"x-foo-baz":"bar-quux"')

it "can send user-agent header", ->
cy.visit({
url: "http://localhost:3500/dump-headers",
headers: {
"user-agent": "something special"
}
})
cy.contains('"user-agent":"something special"')

describe "can send a POST request", ->
it "automatically urlencoded using an object body", ->
cy.visit("http://localhost:3500/post-only", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ describe "src/cy/commands/request", ->
}).then (res) =>
expect(res.body).to.contain('M-SEARCH')

describe "headers", ->
it "can send user-agent header", ->
cy.request({
url: "http://localhost:3500/dump-headers",
headers: {
"user-agent": "something special"
}
}).then (res) ->
expect(res.body).to.contain('"user-agent":"something special"')

describe "subjects", ->
it "resolves with response obj", ->
resp = {
Expand Down
11 changes: 9 additions & 2 deletions packages/server/lib/request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ reduceCookieToArray = (c) ->
createCookieString = (c) ->
reduceCookieToArray(c).join("; ")

caseInsensitiveGet = (obj, property) ->
lowercaseProperty = property.toLowerCase()

for key in Object.keys(obj)
if key.toLowerCase() == lowercaseProperty
return obj[key]

module.exports = (options = {}) ->
defaults = {
timeout: options.timeout ? 20000
Expand Down Expand Up @@ -271,7 +278,7 @@ module.exports = (options = {}) ->
jar: true
}

if ua = headers["user-agent"]
if not caseInsensitiveGet(options.headers, "user-agent") and (ua = headers["user-agent"])
options.headers["user-agent"] = ua

## create a new jar instance
Expand Down Expand Up @@ -332,7 +339,7 @@ module.exports = (options = {}) ->
followRedirect: true
}

if ua = headers["user-agent"]
if not caseInsensitiveGet(options.headers, "user-agent") and (ua = headers["user-agent"])
options.headers["user-agent"] = ua

## normalize case sensitivity
Expand Down
65 changes: 65 additions & 0 deletions packages/server/test/unit/request_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,43 @@ describe "lib/request", ->
.then (resp) ->
expect(resp.body).to.eq("it worked")

it "lower cases headers", ->
nock("http://localhost:8080")
.matchHeader("test", "true")
.get("/foo")
.reply(200, "derp")

headers = {}
headers["user-agent"] = "foobarbaz"

request.send(headers, @fn, {
url: "http://localhost:8080/foo"
cookies: false,
headers: {
'TEST': true,
}
})
.then (resp) ->
expect(resp.body).to.eq("derp")

it "allows overriding user-agent in headers", ->
nock("http://localhost:8080")
.matchHeader("user-agent", "custom-agent")
.get("/foo")
.reply(200, "derp")

headers = {'user-agent': 'test'}

request.send(headers, @fn, {
url: "http://localhost:8080/foo"
cookies: false,
headers: {
'User-Agent': "custom-agent",
},
})
.then (resp) ->
expect(resp.body).to.eq("derp")

context "accept header", ->
it "sets to */* by default", ->
nock("http://localhost:8080")
Expand Down Expand Up @@ -584,3 +621,31 @@ describe "lib/request", ->
"x-text": "אבגד"
}
})

context '#sendStream', ->
beforeEach ->
@fn = sinon.stub()

it "allows overriding user-agent in headers", ->
nock("http://localhost:8080")
.matchHeader("user-agent", "custom-agent")
.get("/foo")
.reply(200, "derp")

sinon.spy(request, "create")
@fn.resolves({})

headers = {'user-agent': 'test'}

options = {
url: "http://localhost:8080/foo"
cookies: false,
headers: {
'user-agent': "custom-agent",
},
}

request.sendStream(headers, @fn, options)
.then ->
expect(request.create).to.be.calledOnce
expect(request.create).to.be.calledWith(options)