Skip to content

Commit

Permalink
Overriding user-agent when using cy.request
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdullah Uzun committed Apr 16, 2019
1 parent db752f5 commit e72ae35
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
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
60 changes: 60 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,26 @@ describe "lib/request", ->
"x-text": "אבגד"
}
})

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

it "allows overriding user-agent in headers", ->
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)

0 comments on commit e72ae35

Please sign in to comment.