【问题标题】:How to send a authentication with a request with supertest如何通过超测试请求发送身份验证
【发布时间】:2016-10-21 12:38:41
【问题描述】:

我正在尝试测试经过身份验证的路由。

这是我的代码:

let request = require('supertest');
var superagent = require('superagent');
var agent = superagent.agent();
var theAccount = {
  name: '*********',
  role: 'admin',
  id: '115039452833383267752'
};
request = request('http://localhost:3000');

describe('Live-score', () => {

  before(function (done) {
    request
      .post('/api/login')
      .send(theAccount)
      .end(function (err, res) {
        if (err) {
          throw err;
        }
        agent.saveCookies(res);
        done();
      });
  });

  it('Should work', (done) => {
    agent.attachCookies(req);
    request
      .get('/api/live-score')
      .send(agent)
      .set('Accept', 'text/html')
      .expect('Content-Type', 'application/json; charset=utf-8')
      .expect(200, done);

  });

但是我得到以下错误:

TypeError: agent.saveCookies 不是函数

我正在使用 Google Passport 策略。

【问题讨论】:

    标签: node.js supertest superagent


    【解决方案1】:

    我在一个地方看到了一些与此类似的代码,代理是在 before 块中声明的。

    你可以试试:

    before(function (done) {
        agent = superagent.agent();
        request
          .post('/api/login')
          .send(theAccount)
          .end(function (err, res) {
            if (err) {
              throw err;
            }
            agent.saveCookies(res);
            done();
          });
      });
    

    参考:https://github.com/visionmedia/superagent/issues/352

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      相关资源
      最近更新 更多