简体   繁体   English

将自定义http标头添加到chai请求

[英]Add custom http headers to chai requests

I am building an app using node.js and testing with mocha + chai. 我正在使用node.js构建应用程序,并使用mocha + chai进行测试。 Is there a way I can add custom headers to my GET and POST chai requests? 有没有一种方法可以向GET和POST柴请求添加自定义标头?

For example, I want something like (semi-pseudocode): 例如,我想要类似(半伪代码)的内容:

chai.request(server)
  .get('/api/car/' + data.car_id)
  .headers({'some_custom_attribute':'some_value'})
  .end(function(err, res) {
    //do something
  });

And likewise with post: 并与邮政:

chai.request(server)
  .post('/api/car/')
  .headers({'some_custom_attribute':'some_value'})
  .send({car_id: 'some_car_id'})
  .end(function(err, res) {
    //do something
  });

Can someone help? 有人可以帮忙吗?

Thanks in advance! 提前致谢!

Use set function to set http headers: 使用set函数设置http标头:

chai.request(server)
  .get('/api/car/' + data.car_id)
  .set('some_custom_attribute', 'some_value')
  .end(function(err, res) {
    //do something
  });

setting-up-requests 设置请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM