【问题标题】:Translate an API in a chai request在 chai 请求中翻译 API
【发布时间】:2017-09-29 14:24:23
【问题描述】:

我有这个 API 调用:

curl -X PATCH --header 'Content-Type: application/json' --header '授权: Bearer 863e2ddf246300f6c62ea9023d068805' -d '1' 'http://asdasd.com/api/loyalty/v1/Accounts/6064361727001553966/Cards'

我想写一个 chai 请求来测试我的 API。 我是这样写的:

describe('/PATCH Patch a card with a Status variable inactive test', () => {


it('it should GET a sample error json response ', (done) => {
  chai.request(app)
    .patch('/loyalty/v1/cards/6064361727001553966')
    .send({"cardStatus": "1" })
    .end((err, res) => {
  res.should.have.status(200);
  done();
});
});
});

但通过这种方式,我传递了“1”值,例如 cardStatus 参数的值。在 API 调用中,我只有这个

-d'1'

如何在 chai 请求中重现此内容? 有没有办法在请求体中传递这个参数而不需要参数key?

【问题讨论】:

    标签: node.js curl chai


    【解决方案1】:

    我找到了解决方案。 我将此添加到我的 .js 文件中:

    app.use(function(req, res, next){
      if (req.is('text/*')) {
        req.text = '';
        req.setEncoding('utf8');
        req.on('data', function(chunk){ req.text += chunk });
        req.on('end', next);
      } else {
        next();
      }
    });
    

    并使用req.text从请求中获取字符串。

    chai 请求现在是这样写的:

    describe('/PATCH Patch a card with a Status variable active test', () => {
      it('it should GET a sample error json response ', (done) => {
      chai.request(app)
        .post('/loyalty/v1/cards/6064361727001553966')
        .set('content-type', 'text/plain')
        .send('1')
        .end((err, res) => {
        res.should.have.status(200);
      done();
    });
    });
    });
    

    我可以像这样在命令行中调用我的 api:

    curl -v -X POST --header 'Content-Type: text/plain' -d '1' 'https://asdasd.com/api/loyalty/v1/cards/6064361727001553966'

    【讨论】:

      猜你喜欢
      • 2011-05-23
      • 2018-02-19
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      相关资源
      最近更新 更多