2016-04-30 96 views
4

我正在使用node.js構建應用程序並使用mocha + chai進行測試。有沒有一種方法可以將自定義標題添加到我的GET和POST chai請求中?向chai請求添加自定義http標題

例如,我想是這樣的(半僞代碼):

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

而且同樣與崗位:

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

有人能幫忙嗎?

在此先感謝!

回答

17

使用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

+0

以及如何與標題一起添加後的變量? – user269867

+1

@ user269867使用'send'方法。 – alexmac

相關問題