Node.js: share session cookie when using mocha with .socketio passport

I am working on a Node.js project where we are using passport for authentication. To roll up authentication to socket.io/engine.io, we use the passport.socketio package instead. This works great in the browser as the available cookies are sent in the original connect.io/engine.io message, which is done over HTTP.

When we try to write tests with mocha for the socket API, as soon as the first time we enter an HTTP call via an HTTP request with a module request, we cannot find a way to pass the received session cookie along with socket.io-client (which the .io- client) initial connection. Hence, we cannot find a way to write these tests in mocha.

Is there a way to set additional headers to send in the handshake or cookie exchange between the request and socket connection, or is there another solution for this?

(there is a legacy "hack" solution, but it no longer works jfromaniello / passport.socketio # 72)

+3


source to share


1 answer


I was struggling with this too, and after I figured it out, I wrote an article about it: Testing Socket.io + Passport.socketio with mocha .

Long story short, I was unable to inject cookies into the confirmation request, but the .socketio passport allows us to pass session_id

as a request parameter, for example:



var socket = io(
  socketUrl,
  {
    //-- pass session_id as a query parameter
    query: 'session_id=' + mySessionId
  }
);

      

See article and example for details.

+3


source







All Articles