Skip to content

Commit

Permalink
reproducable test case for cypress-io/cypress#1315
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Feb 15, 2018
1 parent f5fc064 commit 28fad81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 44 deletions.
38 changes: 3 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,8 @@ node server.js
the in 2nd terminal

```
cypress run
cypress open
```

Test will fail with something like this

```
1) CORS + 301 redirect works:
TypeError: Failed to fetch
at TypeError (native)
(Tests Finished)
- Tests: 1
- Passes: 0
- Failures: 1
- Pending: 0
- Duration: 13 seconds
- Screenshots: 1
- Video Recorded: false
- Cypress Version: 1.4.2
```

To see the error causing this, run

```
cypress run --headed
```

The moment you see the Cypress window, press Alt + Command + i to open web developer tools.
It will stop at the debugger statement in `spec.js` and show this error:

> Fetch API cannot load http://localhost:3000/. The request was redirected to 'http://localhost:3000/foo', which is disallowed for cross-origin requests that require preflight.
When running with `cypress open` the error does not occur
The test will fail due to caching of the 301 redirect.
The tests work when disabling cache in the web developer tools (network tab)
20 changes: 13 additions & 7 deletions cypress/integration/spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
describe('CORS + 301 redirect', () => {
it('works', () => {
it('redirects to foo', () => {
return fetch('http://localhost:3000', {
headers: {
'x-foo': 'foo'
'x-redirect-to': 'foo'
}
})
.then(response => response.text())
.then(result => {
expect(result).to.equal('ok')
expect(result).to.equal('foo')
})

.catch(error => {
debugger
throw error
})
it('redirects to bar', () => {
return fetch('http://localhost:3000', {
headers: {
'x-redirect-to': 'bar'
}
})
.then(response => response.text())
.then(result => {
expect(result).to.equal('bar')
})
})
})
8 changes: 6 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ var app = express()
app.use(cors())

app.get('/', function (req, res, next) {
return res.redirect(301, 'http://localhost:3000/foo')
return res.redirect(301, 'http://localhost:3000/' + req.headers['x-redirect-to'])
})

app.get('/foo', function (req, res, next) {
return res.send('ok')
return res.send('foo')
})

app.get('/bar', function (req, res, next) {
return res.send('bar')
})

app.listen(3000, function () {
Expand Down

0 comments on commit 28fad81

Please sign in to comment.