Skip to content

Commit

Permalink
Have cy.request support urls with specials Unicodes chars (#5813)
Browse files Browse the repository at this point in the history
* test(pkg/driver): url with unicode characters into .request command

* fix(pkg/driver): unicode character escaping in url for .request command

Use encodeURI inside `cy.request` to make sure the url does not contain
any unescaped Unicode character.

Fixes #5274

* fix(pkg/driver): unicode character escaping in url for .request command

Use `URL constructor` inside `cy.request` to make sure the url is well
encoded when for both domain and pathname.

Fixes #5274

* fix(tests): add some more tests for the case of a % character in url

* refactor(tests): Move the tests to request_spec test file

- Move the tests inside the request_spec test file instead of his own
test file to be sure it will be maintain.
- Rename some "it" test titles to semantically fit in the actual tests
naming convention.
- Remove the no more used 5274_spec.coffee test file.
  • Loading branch information
avallete authored and flotwig committed Dec 11, 2019
1 parent 05ad20f commit d122be5
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/driver/src/cy/commands/request.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ module.exports = (Commands, Cypress, cy, state, config) ->
if originOrBase = config("baseUrl") or cy.getRemoteLocation("origin")
options.url = $Location.qualifyWithBaseUrl(originOrBase, options.url)

## Make sure the url unicode characters are properly escaped
## https://github.com/cypress-io/cypress/issues/5274
try
options.url = new URL(options.url).href
catch TypeError
# The URL object cannot be constructed because of URL failure
$utils.throwErrByPath("request.url_invalid", {
args: {
configFile: Cypress.config("configFile")
}
})


## if options.url isnt FQDN then we need to throw here
## if we made a request prior to a visit then it needs
## to be filled out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,55 @@ describe "src/cy/commands/request", ->
url: "http://localhost:8080/app/foobar?cat=1"
})

## https://github.com/cypress-io/cypress/issues/5274
it "encode url with ’ character in pathname", ->
cy.request({ url: 'http://localhost:1234/’' }).then ->
@expectOptionsToBe({
url: "http://localhost:1234/%E2%80%99"
})

it "dont re-encode url with ’ escaped in pathname", ->
cy.request({ url: encodeURI('http://localhost:1234/’') }).then ->
@expectOptionsToBe({
url: "http://localhost:1234/%E2%80%99"
})

it "encode url with % character in pathname", ->
cy.request({ url: 'http://localhost:1234/%' }).then ->
@expectOptionsToBe({
url: "http://localhost:1234/%"
})

it "dont re-encode url with % escaped in pathname", ->
cy.request({ url: encodeURI('http://localhost:1234/%') }).then ->
@expectOptionsToBe({
url: "http://localhost:1234/%25"
})

it "encode url with Astral Plane Unicode in pathname", ->
cy.request({ url: 'http://localhost:1234/😀' }).then ->
@expectOptionsToBe({
url: "http://localhost:1234/%F0%9F%98%80"
})

it "dont re-encode url with Astral Plane Unicode escaped character in pathname", ->
cy.request({ url: encodeURI('http://localhost:1234/😀') }).then ->
@expectOptionsToBe({
url: "http://localhost:1234/%F0%9F%98%80"
})

it "should percent escape Unicode in pathname and convert Unicode in domain name properly", ->
cy.request({ url: 'http://localhost😀:1234/😀' }).then ->
@expectOptionsToBe({
url: "http://xn--localhost-ob26h:1234/%F0%9F%98%80"
})

it "should percent escape Unicode in pathname and convert Unicode in domain name with URI encoded URL", ->
cy.request({ url: encodeURI('http://localhost😀:1234/😀') }).then ->
@expectOptionsToBe({
url: "http://xn--localhost-ob26h:1234/%F0%9F%98%80"
})

context "gzip", ->
it "can turn off gzipping", ->
cy.request({
Expand Down Expand Up @@ -906,6 +955,47 @@ describe "src/cy/commands/request", ->

cy.request("http://localhost:1234/foo")

## https://github.com/cypress-io/cypress/issues/5274
it "dont throw UNESCAPED_CHARACTERS error for url with ’ character in pathname", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.contain "cy.request() failed trying to load:"
expect(err.message).to.not.contain "ERR_UNESCAPED_CHARACTERS"
done()

cy.request("http://localhost:1234/’")

it "dont throw UNESCAPED_CHARACTERS error for url with % character in pathname", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.contain "cy.request() failed trying to load:"
expect(err.message).to.not.contain "ERR_UNESCAPED_CHARACTERS"
done()

cy.request("http://localhost:1234/%")

it "dont throw UNESCAPED_CHARACTERS error for url with ’ escaped in pathname", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.contain "cy.request() failed trying to load:"
expect(err.message).to.not.contain "ERR_UNESCAPED_CHARACTERS"
done()

cy.request(encodeURI('http://localhost:1234/’'))

it "dont throw UNESCAPED_CHARACTERS error for url with Unicode in pathname from BMP to Astral Plane", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.contain "cy.request() failed trying to load:"
expect(err.message).to.not.contain "ERR_UNESCAPED_CHARACTERS"
done()

cy.request('http://localhost:1234/😀')

it "dont throw UNESCAPED_CHARACTERS error for url with any Unicode escaped character in pathname", (done) ->
cy.on "fail", (err) ->
expect(err.message).to.contain "cy.request() failed trying to load:"
expect(err.message).to.not.contain "ERR_UNESCAPED_CHARACTERS"
done()

cy.request(encodeURI('http://localhost:1234/😀'))

context "displays error", ->
it "displays method and url in error", (done) ->
error = new Error("request failed")
Expand Down

4 comments on commit d122be5

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on d122be5 Dec 11, 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.8.0/linux-x64/circle-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-212490/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.8.0/circle-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-212476/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on d122be5 Dec 11, 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.8.0/win32-ia32/appveyor-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-29474172/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.8.0/win32-ia32/appveyor-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-29474172/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on d122be5 Dec 11, 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.8.0/win32-x64/appveyor-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-29474172/cypress.zip
npm install https://cdn.cypress.io/beta/binary/3.8.0/win32-x64/appveyor-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-29474172/cypress.zip

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on d122be5 Dec 11, 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.8.0/darwin-x64/circle-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-212594/cypress.zip
npm install https://cdn.cypress.io/beta/npm/3.8.0/circle-develop-d122be50cf647ec16184d9ab96006e0fc08aa078-212497/cypress.tgz

Please sign in to comment.