question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding Authorization header to cy.visit()

See original GitHub issue

Hi, I’d love to use Cypress for testing sites that use Bearer token authorization - every .visit() should automatically have a header added. I couldn’t find a place where these additional headers could be added, except maybe in socket.coffee - would that be the right place?

Thanks for any pointers!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:45 (16 by maintainers)

github_iconTop GitHub Comments

9reactions
jblaketccommented, Feb 26, 2019

I have another use case.

We have a site in Azure with SSO enabled, so that as soon as you go to the url, it redirects you to a sign-on page. Even if you’ve already auth’d, it redirects and then redirects back so you can view the site.

I’ve made a command to login via oauth2 to get the access_token, and setting that in the header of a cy.request allows me to directly hit the api without the redirect. But when I do cy.visit, it’s doing the redirect and messing up cypress. It looks like if I add a Authorization Bearer header to a GET request to site root via postman, it doesn’t do that redirect.

I tried the SSO recipe and setting the access token in localSession, but visit doesn’t seem to read it out: Tried both id_token, bearer, token, and access_token as the storage key.

image

However, it’s still redirecting to login.microsoftonline.com expecting you to login and cypress doesn’t know what to do with that. So I think the only option I have is to set an Authorization header in visit as well, unless I’m doing this incorrectly.

Here’s what it ends up as because it’s not reading the localStorage item properly. It goes from localhost:port to login.microsoftonline.com

redirect

7reactions
dwellecommented, Nov 15, 2017

Probably different use case from the OP, but having the ability to set custom headers (via cy.visit(url, options.headers) would be cool for auth mocking – currently I have to set custom cookie before issuing cy.visit to handle this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

visit - Cypress Documentation
Add basic auth headers. Cypress will automatically apply the right authorization headers if you're attempting to visit an application that requires Basic ...
Read more >
How to handle basic auth in Cypress - TestersDock
Step 1: Open Postman. Add the URL. Select the relevant Request type, for our use case it will be 'GET'. Under the authorization...
Read more >
Cypress adding token to all requests headers - Stack Overflow
For API tests, you would need to set the token in cy.request() ... //or req.headers['authorization'] = Cypress.env('token') // check you ...
Read more >
Send Network Requests with Authorization Headers in an ...
Usually, a user is identified when an authorization header is sent with the request. With .intercept(), we can dynamically add a header to...
Read more >
cypress-io/cypress - Gitter
return cy.request({ ... the token should be get from env, which is set in the before() ... headers: { authorization }, body: roomAvailabilityRequest ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found