【问题标题】:Setting Basic Auth in Mocha and SuperTest在 Mocha 和 SuperTest 中设置基本身份验证
【发布时间】:2015-04-29 15:52:57
【问题描述】:

我正在尝试为我们设置一个测试,以验证被用户名和密码的基本身份验证阻止的路径的用户名和密码。

it('should receive a status code of 200 with login', function(done) {
    request(url)
        .get("/staging")
        .expect(200)
        .set('Authorization', 'Basic username:password')
        .end(function(err, res) {
            if (err) {
                throw err;
            }

            done();
        });
});

【问题讨论】:

    标签: javascript node.js mocha.js basic-authentication supertest


    【解决方案1】:

    username:password 部分必须是 base64 编码

    你可以使用类似的东西

    .set("Authorization", "basic " + new Buffer("username:password").toString("base64"))
    

    【讨论】:

      【解决方案2】:

      使用认证方法

      SuperTest 基于SuperAgent,它提供了auth 方法来方便Basic Authentication

      it('should receive a status code of 200 with login', function(done) {
          request(url)
              .get('/staging')
              .auth('the-username', 'the-password')
              .expect(200, done);
      });
      

      来源:http://visionmedia.github.io/superagent/#basic-authentication


      PS:您可以将done 直接传递给任何.expect() 调用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-28
        • 1970-01-01
        • 2016-03-20
        相关资源
        最近更新 更多