【问题标题】:Can't made authenticated requests with Mocha and Supertest无法使用 Mocha 和 Supertest 发出经过身份验证的请求
【发布时间】:2016-03-20 04:29:15
【问题描述】:

我正在尝试使用 Mocha 和 Supertest 测试 API,但没有成功。 我有以下代码:

var supertest = require('supertest');  
describe('Routing', function() {
    var url = 'http://example.com';
    var server = supertest.agent(url);

    var credentials = {
        user: 'username',
        pass: 'password'
    };


    describe('Login', function() {

        it('should login ok given valid credentials', function(done) {
            server
                .post('/login.php')
                .send(credentials)
                .end(function(err, res) {
                    if (err) {
                        throw err;
                    }                   
                    server.saveCookies(res);
                    done();
                });
        });


        it('should correctly make an authenticated request', function(done){
            server
                .get('/api/me/accounts?_=1449865354112')
                .end(function(err,res) {
                    if (err) {
                        throw err;
                    }
                    res.status.should.be.equal(200);
                    done();
                });
        }); 
    });
});

登录请求工作正常,我通过了身份验证。第二个调用抛出 401 状态。 我阅读了文档,但无法使其正常工作。

怎么了? 谢谢!

更新: 我终于通过使用.field('user', 'myUsername').field('pass', 'myPassword') 发送参数来获得身份验证。

我还必须在调用之间保留 cookie: cookie = res.headers['set-cookie']; 当我通过身份验证时,.set('cookie', cookie) 在下一个请求中。

【问题讨论】:

    标签: node.js mocha.js supertest


    【解决方案1】:

    那是因为会话(cookie)在你的两个测试之间没有持久化。

    • 首先,您应该在同一个测试中进行两次调用。
    • 其次,我记得我曾使用 superagent 来保持对同一服务器的两次调用之间的会话。但似乎 supertest 现在公开代理以保持会话。

      var supertest = require('supertest');
      var app = express();
      var agent = supertest.agent(app);
      // then you can persist cookie
      agent
        .post('/login.php')
        .auth(credentials)
        ...
      

    编辑: 这是我如何使用 superagent 进行测试的示例:

        var request = require('superagent');
        var postData= {
          email: 'john@test.com',
          password: 'test'
        };
    
        var user1 = request.agent();
          user1.post('http://localhost:3000/user/login')
            .send(postData)
            .end(function (err, res) {
              expect(err).to.not.exist;
              expect(res.status).to.equal(200);
              var result = res.body;
              expect(result.data.message).to.equal('Login successful');
    
              user1.get('http://localhost:3000/user')
                .end(function (err, res) {
                  expect(err).to.not.exist;
                  expect(res.status).to.equal(200);
                  var result = res.body;
                  expect(result.data.email).to.equal('john@test.com');
                  done();
                });
            });
    

    【讨论】:

    • 谢谢,但我必须将域放在该代码中的哪个位置?我仍然无法使它工作:(
    • 我添加了一个示例,说明我如何使用 superagent 进行单元测试。
    • 你的问题解决了吗?
    • 嗨@Sachacr,我已经用FIX更新了这个问题。
    【解决方案2】:

    .send() 用于您的数据。 .auth() 用于您的凭据。试试:

    it('should login ok given valid credentials', function(done) {
        server
            .post('/login.php')
            .auth(credentials)
            .send({"some": "value"})
            .expect(200)
            .end(function(err, res) {
                if (err) {
                    done(error);
                }                   
                server.saveCookies(res);
                done();
            });
    });
    

    有关超测的更多信息,请参阅http://visionmedia.github.io/superagent/docs/test.html

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2017-01-29
      相关资源
      最近更新 更多