How to authenticate Supertest requests with Passport?

后端 未结 8 1443
囚心锁ツ
囚心锁ツ 2020-11-28 21:22

I\'m using Passport.js for authentication (local strategy) and testing with Mocha and Supertest.

How can I create a session and make authenticated requests with Supe

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 22:17

    Try this,

      var request=require('supertest');
      var cookie;
      request(app)
      .post('/login')
      .send({ email: "user@gluck.com", password:'password' })
      .end(function(err,res){
        res.should.have.status(200);
        cookie = res.headers['set-cookie'];
        done();        
      });
    
      //
      // and use the cookie on the next request
      request(app)
      .get('/v1/your/path')
      .set('cookie', cookie)
      .end(function(err,res){  
        res.should.have.status(200);
        done();        
      });
    

提交回复
热议问题