mardi 2 février 2016

getting key value pairs from cookie while testing in node.js with supertest-session

I'd like tests to include checking the value of session variables. I've used passport's serialization/deserialization of my logged in user as a starting point (will want to make this more general). Passport is running fine, and I've confirmed that the user._id (using mongodb) is being both serialized and deserialized correctly.

It took me a while to find the value of connect.sid for the cookie, but I've now grabbed that. But I'm not entirely sure how to get the values I'm looking for after that. Here's my test code for the route that registers the user:

        it("Tests Creating User", function(done) {
        User.remove({username:'test@test.com'}, function(err) { });
        testSession.post('/users/register')
            .send({username: 'test@test.com', password: 'hotdog'})
            .expect(302)
            .end(function(err,res) {
                expect(User.find({username: 'test@test.com'})).to.be.an('object')
                expect(err).to.equal(null);

                // grab the session id at the time it is set
                // id is first item in array, key separated by = sign
                sid = res.headers['set-cookie'].pop().split(';')[0].split('=')[1];

                var sessionCookie = _.find(testSession.cookies, function (cookie) {
                   return cookie.value === sid;
                });
                console.log("session cookie is " + sessionCookie)
                done();
                });
    })

what I'm trying to determine is how I get to discovering the actual value of the user._id that is presumably encoded in the cookie. When I put the found cookie onto the console, I see this:

session cookie is connect.sid=s%3A95nG860WBEZeYzHBM7dVhlto4H5_AJVJ.W%2FWaC9FLpGOR4ucjZxi3yl3so7fsHoamQL2osmWqRoU; path=/; httponly

which seems straightforward - but forgive my ignorance, but where is the data? Encoded in the value of connect.sid? Or should I see a key/value pair here. I'm assuming the former, so how do I deserialize the value in the context of a test?

I think the answer is going to be obvious, but for some reason I haven't found a path to it myself - so thanks for your help.

Aucun commentaire:

Enregistrer un commentaire