-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Automatically include CORS headers for network stubbing #9264
Comments
I ran into this issue trying to convert cy.route() to cy.intercept() in the docs. Was opening a new issue when I saw this. Current behaviorTrying to stub a request converting Test code to reproduce
it('Displays Algolia dropdown on focus after close all banners', function () {
cy.visit('https://docs.cypress.io/guides/overview/why-cypress.html')
cy.server()
cy.route({
method: 'POST',
url: /algolia/,
response: {
'results': [
{
'hits': [
{
'hierarchy': { 'lvl2': null, 'lvl3': null, 'lvl0': 'Known Issues', 'lvl1': null, 'lvl6': null, 'lvl4': null, 'lvl5': null },
'url': 'https://docs-staging.cypress.io/guides/appendices/known-issues.html#content-inner', 'content': 'cy.get', 'anchor': 'content-inner', 'objectID': '15872310', '_snippetResult': { 'content': { 'value': 'to implement in Cypress. Right <span class="algolia-docsearch-suggestion--highlight">click Issue #53</span> Workaround Oftentimes', 'matchLevel': 'full' } }, '_highlightResult': { 'hierarchy': { 'lvl0': { 'value': 'Known Issues', 'matchLevel': 'none', 'matchedWords': [] } }, 'content': { 'value': 'cy.get', 'matchLevel': 'full', 'fullyHighlighted': false, 'matchedWords': ['click', 'issue', '53'] }, 'hierarchy_camel': [{ 'lvl0': { 'value': 'Known Issues', 'matchLevel': 'none', 'matchedWords': [] } }] },
},
], 'nbHits': 1, 'page': 0, 'nbPages': 1, 'hitsPerPage': 5, 'processingTimeMS': 1, 'exhaustiveNbHits': true, 'query': '"click Issue #53" ', 'params': 'query=%22click%20Issue%20%2353%22%20&hitsPerPage=5', 'index': 'cypress',
},
],
},
}).as('postAlgolia')
cy.get('.ds-dropdown-menu').should('not.be.visible')
cy.get('#search-input').type('g')
cy.wait('@postAlgolia')
cy.get('.ds-dropdown-menu').should('be.visible')
})
So then I see this in the console:
it('Displays Algolia dropdown on focus after close all banners', function () {
cy.visit('https://docs.cypress.io/guides/overview/why-cypress.html#In-a-nutshell')
cy.intercept(/algolia/, {
'results': [
{
'hits': [
{
'hierarchy': { 'lvl2': null, 'lvl3': null, 'lvl0': 'Known Issues', 'lvl1': null, 'lvl6': null, 'lvl4': null, 'lvl5': null },
'url': 'https://docs-staging.cypress.io/guides/appendices/known-issues.html#content-inner', 'content': 'cy.get', 'anchor': 'content-inner', 'objectID': '15872310', '_snippetResult': { 'content': { 'value': 'to implement in Cypress. Right <span class="algolia-docsearch-suggestion--highlight">click Issue #53</span> Workaround Oftentimes', 'matchLevel': 'full' } }, '_highlightResult': { 'hierarchy': { 'lvl0': { 'value': 'Known Issues', 'matchLevel': 'none', 'matchedWords': [] } }, 'content': { 'value': 'cy.get', 'matchLevel': 'full', 'fullyHighlighted': false, 'matchedWords': ['click', 'issue', '53'] }, 'hierarchy_camel': [{ 'lvl0': { 'value': 'Known Issues', 'matchLevel': 'none', 'matchedWords': [] } }] },
},
], 'nbHits': 1, 'page': 0, 'nbPages': 1, 'hitsPerPage': 5, 'processingTimeMS': 1, 'exhaustiveNbHits': true, 'query': '"click Issue #53" ', 'params': 'query=%22click%20Issue%20%2353%22%20&hitsPerPage=5', 'index': 'cypress',
},
],
}).as('postAlgolia')
cy.get('.ds-dropdown-menu').should('not.be.visible')
cy.get('#search-input').type('g')
cy.wait('@postAlgolia')
cy.get('.ds-dropdown-menu').should('be.visible')
}) Workarounds
it('Displays Algolia dropdown on focus after close all banners', function () {
cy.visit('https://docs.cypress.io/guides/overview/why-cypress.html#In-a-nutshell')
cy.intercept(/algolia/, {
body:{
'results': [
{
'hits': [
{
'hierarchy': { 'lvl2': null, 'lvl3': null, 'lvl0': 'Known Issues', 'lvl1': null, 'lvl6': null, 'lvl4': null, 'lvl5': null },
'url': 'https://docs-staging.cypress.io/guides/appendices/known-issues.html#content-inner', 'content': 'cy.get', 'anchor': 'content-inner', 'objectID': '15872310', '_snippetResult': { 'content': { 'value': 'to implement in Cypress. Right <span class="algolia-docsearch-suggestion--highlight">click Issue #53</span> Workaround Oftentimes', 'matchLevel': 'full' } }, '_highlightResult': { 'hierarchy': { 'lvl0': { 'value': 'Known Issues', 'matchLevel': 'none', 'matchedWords': [] } }, 'content': { 'value': 'cy.get', 'matchLevel': 'full', 'fullyHighlighted': false, 'matchedWords': ['click', 'issue', '53'] }, 'hierarchy_camel': [{ 'lvl0': { 'value': 'Known Issues', 'matchLevel': 'none', 'matchedWords': [] } }] },
},
], 'nbHits': 1, 'page': 0, 'nbPages': 1, 'hitsPerPage': 5, 'processingTimeMS': 1, 'exhaustiveNbHits': true, 'query': '"click Issue #53" ', 'params': 'query=%22click%20Issue%20%2353%22%20&hitsPerPage=5', 'index': 'cypress',
},
],
},
headers: {
"access-control-allow-origin": window.location.origin,
"Access-Control-Allow-Credentials": "true",
},}).as('postAlgolia')
cy.get('.ds-dropdown-menu').should('not.be.visible')
cy.get('#search-input').type('g')
cy.wait('@postAlgolia')
cy.get('.ds-dropdown-menu').should('be.visible')
}) |
@jennifer-shehane, I've reached the same solution, but it doesn't work for me when I try to use the version with a handler function (I need custom responses). Another difference is that my request has a custom header + I'll revert my tests to the |
The code for this is done in cypress-io/cypress#9384, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
What would you like?
Currently, users must specify CORS headers manually, if needed:
cypress-io/cypress-realworld-app@2df6ef7
This is some manual work that could probably be avoided in 99% of cases.
We should set
Access-Control-Allow-Origin: ${request's origin header}
andAccess-Control-Allow-Credentials: true
by default on stubbed responses so users don't need to do this manually.The text was updated successfully, but these errors were encountered: