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

cy.intercept should provide req.query since the url has the query object #16327

Closed
eliw00d opened this issue May 4, 2021 · 6 comments · Fixed by #16916
Closed

cy.intercept should provide req.query since the url has the query object #16327

eliw00d opened this issue May 4, 2021 · 6 comments · Fixed by #16916
Assignees
Labels
topic: cy.intercept() type: enhancement Requested enhancement of existing feature

Comments

@eliw00d
Copy link

eliw00d commented May 4, 2021

Current behavior

req.url looks like http://hostname:port/?someKey=someValue but req.query is undefined.

Desired behavior

req.url looks like http://hostname:port/?someKey=someValue and req.query is { someKey: 'someValue' }.

Test code to reproduce

cy.intercept({ hostname }, (req) => {
  console.log(req.query) // undefined
  console.log(req.url) // http://hostname:port/?someKey=someValue
})

Versions

Cypress: 7.2.0

@jennifer-shehane
Copy link
Member

I can recreate this. Although I don't see query as a documented property that is meant to be available on the req object: https://on.cypress.io/intercept#Request-object-properties so I don't think this is a bug per se.

But I can see the usefulness of wanting the req.query here.

Example

it('test cy.intercept()', () => {
  cy.intercept({ url: '/users' }, (req) => {
    console.log(req)
    console.log(req.query) // undefined
    console.log(req.url)   // https://example.com/users?someKey=someValue
  }).as('getUrl')
  cy.visit('https://example.com')
  cy.window().then((win) => {
    const xhr = new win.XMLHttpRequest()
    xhr.open('GET', '/users?someKey=someValue')
    xhr.send()
  })
  cy.wait('@getUrl')
})

@jennifer-shehane jennifer-shehane added type: enhancement Requested enhancement of existing feature topic: cy.intercept() labels May 5, 2021
@jennifer-shehane jennifer-shehane changed the title cy.intercept provides req without query even though the url has them cy.intercept should provide req.query since the url has the query object May 5, 2021
@jennifer-shehane jennifer-shehane added the stage: ready for work The issue is reproducible and in scope label May 5, 2021
@eliw00d
Copy link
Author

eliw00d commented May 5, 2021

You are right, I think based on there being a query matcher I thought there would also be a query in the response, so this is more of a feature request. I was trying to do something like this for GraphQL:

cy.intercept({ hostname }, (req) => {
  if (req.query?.operationName) {
    req.alias = req.query.operationName
  } else if (req.body?.query && req.body?.operationName) {
    req.alias = req.body.operationName
  }

but ended up doing this for now:

cy.intercept({ hostname }, (req) => {
    if (req.url.includes('operationName')) {
      const { search } = url.parse(req.url)
      const searchParams = new URLSearchParams(search)
      req.alias = searchParams.get('operationName')
    } else if (req.body?.query && req.body?.operationName) {
      req.alias = req.body.operationName
    }

since we use persisted queries and this aliases the request based on GET vs POST and query vs mutation (which are not persisted).

@flotwig
Copy link
Contributor

flotwig commented May 5, 2021

Yeah, I agree that it's weird to not have query. The only problem with this proposal is that req.url contains the querystring and can be modified, so it stands to reason that req.query can be modified as well. So, if a query property was added, any changes the user makes to it would have to be reconciled with changes to req.url somehow.

IMO the best way to implement this would be to add req.query, but make it just a fancy getter/setter for req.url, so the two can never be out of sync. What do you two think?

@eliw00d
Copy link
Author

eliw00d commented May 5, 2021

That sounds good to me!

@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: ready for work The issue is reproducible and in scope stage: work in progress stage: needs review The PR code is done & tested, needs review labels Jun 14, 2021
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: waiting labels Jun 21, 2021
@cypress-bot cypress-bot bot added stage: waiting stage: needs review The PR code is done & tested, needs review and removed stage: needs review The PR code is done & tested, needs review stage: waiting labels Jun 21, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 23, 2021

The code for this is done in cypress-io/cypress#16916, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Jun 23, 2021
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 23, 2021

Released in 7.6.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v7.6.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jun 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
topic: cy.intercept() type: enhancement Requested enhancement of existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants