Created
January 3, 2014 13:10
-
-
Save stockholmux/8237643 to your computer and use it in GitHub Desktop.
When using jsdom, redirects are followed but they do not take into account any cookies that are set during the course of the redirect. Consider the short express app and related jsdom script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
jsdom = require('jsdom'); | |
jsdom.env( | |
'http://localhost:3012/', | |
["http://code.jquery.com/jquery.js"], | |
function (errors, window) { | |
if (errors) { | |
console.log('error', errors); | |
} else { | |
console.log('window', window); | |
} | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
this just sets a cookie then redirects. If the cookie is set, then it renders the correct page, otherwise it redirects again. | |
*/ | |
var express = require('express'); | |
var app = express(); | |
app.use(express.bodyParser()); | |
app.use(express.cookieParser()); | |
app.get('/', function(req, res){ | |
console.log(req.cookies); | |
if (typeof req.cookies.MyCookie === 'undefined') { | |
res.cookie('MyCookie', 'cookievalue'); | |
res.redirect(302,'/'); | |
console.log('redirecting'); | |
} else { | |
res.send('you got the cookie'); | |
console.log('cookie ok', req.cookies); | |
} | |
}); | |
app.listen(3012); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment