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

fix: update getPath to use WHATWG URL API #28354

Merged
merged 9 commits into from
Nov 20, 2023
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _Released 11/21/2023 (PENDING)_
- Fixed an issue where dynamic intercept aliases now show with alias name instead of "no alias" in driver. Addresses [#24653](/~https://github.com/cypress-io/cypress/issues/24653)
- Fixed an issue where [aliasing individual requests](https://docs.cypress.io/api/commands/intercept#Aliasing-individual-requests) with `cy.intercept()` led to an error when retrieving all of the aliases with `cy.get(@alias.all)` . Addresses [#25448](/~https://github.com/cypress-io/cypress/issues/25448)
- The URL of the application under test and command error "Learn more" links now open externally instead of in the Cypress-launched browser. Fixes [#24572](/~https://github.com/cypress-io/cypress/issues/24572).
- Fixed issue where some URLs would timeout in pre-request correlation. Addressed in [#28354](/~https://github.com/cypress-io/cypress/pull/28354).

**Misc:**

Expand Down
6 changes: 5 additions & 1 deletion packages/network/lib/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export function addDefaultPort (urlToCheck: any) {
}

export function getPath (urlToCheck: string) {
return url.parse(urlToCheck).path
// since we are only concerned with the pathname and search properties,
// we can set the base to a fake base to handle relative urls
const url = new URL(urlToCheck, 'http://fake-base.com')

return `${url.pathname}${url.search}`
}

const localhostIPRegex = /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
Expand Down
18 changes: 18 additions & 0 deletions packages/network/test/unit/uri_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import { URL } from 'url'
import { uri } from '../../lib'

describe('lib/uri', () => {
context('.getPath', () => {
it('returns the pathname and search', () => {
expect(uri.getPath('http://localhost:9999/foo/bar?baz=quux#/index.html')).to.eq('/foo/bar?baz=quux')
})

it('supports encoded characters', () => {
expect(uri.getPath('http://localhost:9999?foo=0%3C1')).to.eq('/?foo=0%3C1')
})

it('does not encode the "|" character', () => {
expect(uri.getPath('http://localhost:9999?foo=bar|baz')).to.eq('/?foo=bar|baz')
})

it('works with relative urls', () => {
expect(uri.getPath('/foo/bar?foo=bar|baz')).to.eq('/foo/bar?foo=bar|baz')
})
})

context('.isLocalhost', () => {
it('http://localhost is localhost', () => {
expect(uri.isLocalhost(new URL('http://localhost'))).to.be.true
Expand Down
Loading