Skip to content

Commit

Permalink
fix: bind this correctly when setting response headers with cy.route()
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Nov 9, 2021
1 parent 925fc65 commit 6b851e9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
27 changes: 27 additions & 0 deletions packages/driver/cypress/integration/commands/xhr_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,33 @@ describe('src/cy/commands/xhr', () => {
})
})

// https://github.com/cypress-io/cypress/issues/18858
it('can stub headers', (done) => {
cy
.route({
url: '/foo',
response: '',
headers: {
'some-header': 'header-value',
},
}).as('getFoo')
.window().then((win) => {
win.$.ajax({
url: '/foo',
error (_a, _b, err) {
done(`Errored but should not have: ${err.stack}`)
},
})

return null
})
.wait('@getFoo')
.then((xhr) => {
expect(xhr.response.headers['some-header']).to.equal('header-value')
done()
})
})

// https://github.com/cypress-io/cypress/issues/2372
it('warns if a percent-encoded URL is used', () => {
cy.spy(Cypress.utils, 'warning')
Expand Down
3 changes: 1 addition & 2 deletions packages/driver/src/cypress/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ export class Server {
this.setHeader(xhr, 'response', route.response, responser)
this.setHeader(xhr, 'matched', `${route.url}`)
this.setHeader(xhr, 'delay', route.delay)

return this.setHeader(xhr, 'headers', route.headers, this.transformHeaders)
this.setHeader(xhr, 'headers', route.headers, this.transformHeaders.bind(this))
}

route (attrs = {}) {
Expand Down

0 comments on commit 6b851e9

Please sign in to comment.