Node.js unit testing for session-specific middleware

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

I'd like to create a fake session, but can't seem to figure out how.

I have a connect.sid cookie in my browser that I assume correlates to the _id in my sessions collection in some encrypted manner.

Here's what I tried:

I added in the cookieParser middleware and a session store to a server, then used the following request to send it up to the server (copied the key from chrome's dev tools panel):

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

that correctly set the cookie on the server side, and I have verified that sessions are working.

However, the magic conversion from cookie to session didn't happen as I had hoped - what's the correct way to do this?

Setting the cookie on the server would only work if a session with that ID exists. Who created the session in the first place?

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 the clients. My server allowed authentication based on Google OAuth. However, I did not want to go through the trouble of teaching the clients to sign into a Google account.

My solution was to implement an alternative method for signing in to my server - using nothing but a username. This feature is only enabled during testing and disabled for production. My test clients can now sign in without a problem. They receive the cookie 'connect.sid' as a result of the sign-in and send it back to the server in subsequent requests.

I too used request.jar() to create a cookie jar for my requests. I should note, however, that this is only necessary if you are simulating more than one client at the same time and need a separate cookie jar for each client.