{"id":7957,"date":"2015-11-12T03:18:38","date_gmt":"2015-11-12T03:18:38","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/node-js-superagent-module-not-saving-cookies-with-passport-open-source-projects-visionmedia-superagent\/"},"modified":"2015-11-12T03:18:38","modified_gmt":"2015-11-12T03:18:38","slug":"node-js-superagent-module-not-saving-cookies-with-passport-open-source-projects-visionmedia-superagent","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/node-js-superagent-module-not-saving-cookies-with-passport-open-source-projects-visionmedia-superagent\/","title":{"rendered":"Node.js superagent module not saving cookies with passport-open source projects visionmedia\/superagent"},"content":{"rendered":"<p>So I&#8217;m trying to test a website backend I&#8217;m coding in Node.js. I need a testing method to save session cookies being managed by the Passport module. I was originally using the Unit.js httpAgent but I tried using superagent based on advice from this question, but when I threw in an assertion to test if it was storing Passport&#8217;s req.user field, it failed:<\/p>\n<pre><code>assert(req.user, \"Error! User is not defined\"); \/\/this should not fail\n<\/code><\/pre>\n<p>output:<\/p>\n<pre><code>app.js\n127.0.0.1 - - [Tue, 06 Jan 2015 23:55:45 GMT] \"POST \/login HTTP\/1.1\" 302 40 \"-\" \"node-superagent\/0.21.0\"\n127.0.0.1 - - [Tue, 06 Jan 2015 23:55:45 GMT] \"GET \/login HTTP\/1.1\" 200 2188 \"-\" \"node-superagent\/0.21.0\"\n\u2713 should use cookies at login (43ms)\n127.0.0.1 - - [Tue, 06 Jan 2015 23:55:45 GMT] \"GET \/ HTTP\/1.1\" 200 1598 \"-\" \"node-superagent\/0.21.0\"\n\u2713 should GET \/ \nAssertionError: Error! User is not defined\nat app.post.submission (\/home\/matthew\/Documents\/git\/libr\/web\/app.js:201:3)\nat callbacks (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/lib\/router\/index.js:164:37)\nat param (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/lib\/router\/index.js:138:11)\nat pass (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/lib\/router\/index.js:145:5)\nat Router._dispatch (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/lib\/router\/index.js:173:5)\nat Object.router (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/lib\/router\/index.js:33:10)\nat next (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/node_modules\/connect\/lib\/proto.js:174:15)\nat SessionStrategy.strategy.pass (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/passport\/lib\/middleware\/authenticate.js:318:9)\nat SessionStrategy.authenticate (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/passport\/lib\/strategies\/session.js:67:10)\nat attempt (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/passport\/lib\/middleware\/authenticate.js:341:16)\nat Object.authenticate [as handle] (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/passport\/lib\/middleware\/authenticate.js:342:7)\nat next (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/node_modules\/connect\/lib\/proto.js:174:15)\nat Object.initialize [as handle] (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/passport\/lib\/middleware\/initialize.js:62:5)\nat next (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/node_modules\/connect\/lib\/proto.js:174:15)\nat Object.handle (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/connect-flash\/lib\/flash.js:21:5)\nat next (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/node_modules\/connect\/lib\/proto.js:174:15)\nat Object. (\/home\/matthew\/Documents\/git\/libr\/web\/node_modules\/express\/node_modules\/connect\/node_modules\/express-session\/index.js:385:7)\nat Object.immediate._onImmediate (timers.js:372:16)\nat processImmediate [as _immediateCallback] (timers.js:354:15)\n<\/code><\/pre>\n<p>Is there anything I&#8217;m doing wrong here? I&#8217;m using done callbacks in my test program (with mocha), so I&#8217;m pretty sure things are happening in series.<\/p>\n<p>tests:<\/p>\n<pre><code>describe('app.js', function(){\n    it('should use cookies at login', function(done){\n        user1\n        .post('http:\/\/localhost:5000\/login')\n        .send({ user: 'bob', password: 'secret' })\n        .end(function(err, res) {\n            if(err){\n                done(err.message);\n            }\n            should.exist(res.headers['set-cookie']);\n            done();\n        \/\/ user1 will manage its own cookies\n        \/\/ res.redirects contains an Array of redirects\n      });\n    });\n\n    it('should GET \/', function(done){\n        user1\n        .get('http:\/\/localhost:5000\/')\n        .end(function(err, res){\n            if(err){\n                done(err);\n                return;\n            }\n            done();\n        });\n    });\n\n    it('POST event data works', function(done){\n            user1\n            .post('http:\/\/localhost:5000\/dataEntry')\n            .send({\n                start_date_time: '2014-08-24T11:11',\n                end_date_time: '2014-08-24T12:00',\n                state_intensity: 1,\n                activity: 'Sample',\n                event: 'Sample',\n                submitButton: 'Submit'\n            })\n            .end(function(err, res){\n                if (err) {\n                    done(err);\n                    return;\n                }\n                done();\n            });\n        });\n});\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So I&#8217;m trying to test a website backend I&#8217;m coding in Node.js. I need a testing method to save session cookies being managed by the Passport module. I was originally using the Unit.js httpAgent but I tried using superagent based on advice from this question, but when I threw in an assertion to test if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7957","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7957"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7957\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}