How to authenticate Supertest requests with Passport /Facebook strategy/?

前端 未结 3 1629
南方客
南方客 2021-02-09 20:32

I\'m using Passport.js for authentication (Facebook strategy) and testing with Mocha and Supertest. How can I create a session and make authenticated requests with Supertest for

3条回答
  •  臣服心动
    2021-02-09 20:47

    The general solution is to create a cookie jar that will be re-used between requests.

    The following example isn't passport specific, but should work:

    var request = require('request');
    
    describe('POST /api/posts', function () {
        // Create a new cookie jar
        var j = request.jar();
        var requestWithCookie = request.defaults({jar: j}),
    
        // Authenticate, thus setting the cookie in the cookie jar
        before(function(done) {
            requestWithCookie.post('http://localhost/user', {user: 'foo', password: 'bar'}, done);
        });
    
        it('should get the user profile', function (done) {
            requestWithCookie.get('http://localhost/user', function (err, res, user) {
                assert.equal(user.login, 'foo');
                done();
            });
        });
    });
    

提交回复
热议问题