Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom http headers to chai requests

Tags:

I am building an app using node.js and testing with mocha + chai. Is there a way I can add custom headers to my GET and POST chai requests?

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!

like image 490
Trung Tran Avatar asked Apr 30 '16 23:04

Trung Tran


People also ask

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

How do I set up HTTP headers?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

What is chai HTTP in node JS?

Chai HTTP provides an interface for live integration testing via superagent. To do this, you must first construct a request to an application or url. Upon construction you are provided a chainable api that allows you to specify the http VERB request (get, post, etc) that you wish to invoke.


1 Answers

Use set function to set http headers:

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

setting-up-requests

like image 171
alexmac Avatar answered Mar 14 '23 00:03

alexmac