Skip to content

Instantly share code, notes, and snippets.

@stockholmux
Created January 3, 2014 13:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stockholmux/8237643 to your computer and use it in GitHub Desktop.
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.
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 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