Node.js unit testing for session specific middleware

I am writing unit test for middleware that relies on persistent sessions in a connection. (namely connect-mongo).

I would like to create a fake session, but cannot figure out how to do this.

I have a connect.sid cookie in my browser that I believe correlates with _id in my session collection in some encrypted form.

Here is what I tried:

I added cookieParser and session storage to the server in the middleware, and then used the following request to send it to the server (I copied the key from the chrome dev toolbar):

var jar = request.jar(),
    cookie = request.cookie('connect.sid=<REALLYLONGKEY>');
jar.add(cookie);
request({url : 'http://localhost:8585/',jar : jar},this.callback);

which set the cookie correctly on the server side, and I checked that the sessions are working.

However, the magic conversion from cookie to session did not happen, as I had hoped - what is the right way to do this?

+3
source share
1 answer

Setting a cookie on the server will only work if there is a session with this identifier. Who created the session first?

I can tell you what I did on my server. I wanted to create tests that simulate the client side and send requests to the server. I needed a way to authenticate clients. My server has allowed authentication based on Google OAuth. However, I didn’t want to have difficulty teaching clients to sign up for a Google account.

, - . . . cookie "connect.sid" .

request.jar() cookie . , , cookie .

+1

All Articles