From a6364a036242267c726fde9bec44278bb41066a2 Mon Sep 17 00:00:00 2001 From: Abdullah Uzun Date: Tue, 30 Apr 2019 16:58:56 +0200 Subject: [PATCH 1/2] Overriding user-agent when using cy.request --- packages/server/lib/request.coffee | 11 +++- packages/server/test/unit/request_spec.coffee | 65 +++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/packages/server/lib/request.coffee b/packages/server/lib/request.coffee index 92ee6e476069..78fca11cd9e5 100644 --- a/packages/server/lib/request.coffee +++ b/packages/server/lib/request.coffee @@ -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 @@ -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 @@ -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 diff --git a/packages/server/test/unit/request_spec.coffee b/packages/server/test/unit/request_spec.coffee index c7f88bba5a74..934ba677085e 100644 --- a/packages/server/test/unit/request_spec.coffee +++ b/packages/server/test/unit/request_spec.coffee @@ -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") @@ -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) From 3d1c8320446114a487387bd690cd16acc9840661 Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Wed, 1 May 2019 15:41:42 -0400 Subject: [PATCH 2/2] add driver tests for visit, request user-agent --- .../integration/commands/navigation_spec.coffee | 9 +++++++++ .../cypress/integration/commands/request_spec.coffee | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/packages/driver/test/cypress/integration/commands/navigation_spec.coffee b/packages/driver/test/cypress/integration/commands/navigation_spec.coffee index 4a5fa333708d..ce62dbd7f452 100644 --- a/packages/driver/test/cypress/integration/commands/navigation_spec.coffee +++ b/packages/driver/test/cypress/integration/commands/navigation_spec.coffee @@ -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", { diff --git a/packages/driver/test/cypress/integration/commands/request_spec.coffee b/packages/driver/test/cypress/integration/commands/request_spec.coffee index 5d7f70713775..dc7102fdb618 100644 --- a/packages/driver/test/cypress/integration/commands/request_spec.coffee +++ b/packages/driver/test/cypress/integration/commands/request_spec.coffee @@ -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 = {