Adding Authorization header to cy.visit()
See original GitHub issueHi,
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:
- Created 6 years ago
- Reactions:17
- Comments:45 (16 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 acy.request
allows me to directly hit the api without the redirect. But when I docy.visit
, it’s doing the redirect and messing up cypress. It looks like if I add aAuthorization Bearer
header to aGET
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 bothid_token
,bearer
,token
, andaccess_token
as the storage key.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 anAuthorization
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 fromlocalhost:port
tologin.microsoftonline.com
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 issuingcy.visit
to handle this.