Cypress: cy.route() does fails when using basic auth in baseUrl

Created on 21 Dec 2017  路  2Comments  路  Source: cypress-io/cypress

Current behavior:

When invoking cy.route() with baseUrl having basic auth credentials in the URL the route is never called.

$ CYPRESS_baseUrl=https://username:[email protected] yarn cypress:run
cy.route('POST', '/api/endpoint').as('someJsonData');
cy.wait('@someJsonData') // fails, since Cypress is waiting for the XHR call to include the basic auth credentials

Desired behavior:

The route should be called as all the subsequent XHR calls add the Authentication headers automatically. However the cy.route() expects the route to have the credentials in the URL.

How to reproduce:

Start a server that required basic auth. Add basic auth credentials to the baseUrl e.g. by passing in an environment variable. CYPRESS_baseUrl=https://username:[email protected] yarn cypress:run

  • Operating System: OS X
  • Cypress Version: 1.4
  • Browser Version: 63
needs investigating

Most helpful comment

I was having the same issue. I fixed it by overwriting the visit command:

Cypress.Commands.overwrite('visit', (orig, url, options) => {
    options = options || {}
    options.auth = {
        username: 'username',
        password: 'pass',
    }

    return orig(url, options)
})

All 2 comments

Providing username:password as part of my baseUrl broke everything; it caused an immediate 401 as soon as the child Chrome process started in interactive mode, and caused timeouts in cypress run cli.

I was able to work around this by stripping it out of baseUrl and adding a dummy cy.visit() first thing in my tests, like this:

describe('Set up environment', function() {
  context("Set up environment", function() {
    it("starts", function() {
      cy.visit('/', {
        auth: {
          username: 'username',
          password: 'pass'
        } } )
    })
  })
})

Not sure if this is the exact thing you're seeing...

I was having the same issue. I fixed it by overwriting the visit command:

Cypress.Commands.overwrite('visit', (orig, url, options) => {
    options = options || {}
    options.auth = {
        username: 'username',
        password: 'pass',
    }

    return orig(url, options)
})
Was this page helpful?
0 / 5 - 0 ratings