diff --git a/README.md b/README.md index b585faca..7f7c9e2e 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/cypress/integration/spec.js b/cypress/integration/spec.js index 707f2a47..e10f038c 100644 --- a/cypress/integration/spec.js +++ b/cypress/integration/spec.js @@ -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') }) }) }) diff --git a/server.js b/server.js index 31eae8d4..31223a66 100644 --- a/server.js +++ b/server.js @@ -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 () {