Before XHR URLs are whitelisted, strip query params and hashes (#7742)

This commit is contained in:
Jennifer Shehane
2020-06-19 10:35:06 +06:30
committed by GitHub
parent a2d2c8d42c
commit 869bcec55c
2 changed files with 37 additions and 1 deletions
@@ -2296,6 +2296,34 @@ describe('src/cy/commands/xhr', () => {
expect(resp).to.eq('{ \'bar\' }\n')
})
})
// https://github.com/cypress-io/cypress/issues/7280
it('ignores query params when whitelisting routes', () => {
cy.server()
cy.route(/url-with-query-param/, { foo: 'bar' }).as('getQueryParam')
cy.window().then((win) => {
win.$.get('/url-with-query-param?resource=foo.js')
return null
})
cy.wait('@getQueryParam').its('response.body')
.should('deep.equal', { foo: 'bar' })
})
// https://github.com/cypress-io/cypress/issues/7280
it('ignores hashes when whitelisting routes', () => {
cy.server()
cy.route(/url-with-hash/, { foo: 'bar' }).as('getHash')
cy.window().then((win) => {
win.$.get('/url-with-hash#foo.js')
return null
})
cy.wait('@getHash').its('response.body')
.should('deep.equal', { foo: 'bar' })
})
})
describe('route setup', () => {
+9 -1
View File
@@ -66,8 +66,16 @@ const warnOnForce404Default = (obj) => {
}
const whitelist = (xhr) => {
const url = new URL(xhr.url)
// https://github.com/cypress-io/cypress/issues/7280
// we want to strip the xhr's URL of any hash and query params before
// checking the REGEX for matching file extensions
url.search = ''
url.hash = ''
// whitelist if we're GET + looks like we're fetching regular resources
return xhr.method === 'GET' && regularResourcesRe.test(xhr.url)
return xhr.method === 'GET' && regularResourcesRe.test(url.href)
}
const serverDefaults = {