title: Generate test session manually with express-session
body: <p>I have implemented the OAUth 2.0 flow as single authentication method for my express application. </p>
  
  <p>In real situation, once a user connected using OAuth, a session is created and accessible server-side in <code>req.session.passport</code>.</p>
  
  <p>In this session, there is the OAuth access token that is required to perform authenticated requests on my REST API. To be able to do so, I wrote a middleware that copies the token from <code>req.session.passport</code> to the <code>Authorization</code> header.</p>
  
  <pre><code>app.use(function(req, res, next) {
    if (req.session.passport) {
      req.headers['Authorization'] = req.session.passport.user.accessToken;
    }
    next();
  });
  </code></pre>
  
  <p>Now, I would like to test this middleware without going through the entire oauth flow. Ideally, I would like to create a session manually and inject test data inside <code>req.session.passport</code>, then call any protected endpoint of my API to check that it can correctly perform authenticated requests.</p>
  
  <p>I tried to manually set data inside <code>req.session</code> in the following test</p>
  
  <pre><code>it('Should be able to make an authenticated request with session data',
  function(done) {
    var req = request(app).get('/api/SomeProtectedEndpoint');
  
    req.session = {
      passport: {
        user: {
          accessToken : 'eade123d3ffwhatever'
        }
      }
    }
  
    req.expect(200, function(err, res){
        if (err) return done(err);
        done();
      });
  });
  </code></pre>
  
  <p>But if I display the contents of <code>req.session</code> inside my middleware, the manual data is gone. I believe it is removed by <code>express-session</code>.</p>
  
  <p>How can I manually inject session data in a request for testing purposes ?</p>