node.js - Testing express app with mocha -


i have following request need test:

curl -h "accept: application/json" -h "content-type: application/json" -x post -d '{"content":{"value":"18.5", "date": "20120413"}}' 'http://server:port/marks' 

i'm using expressjs , mocha. did not find way add header , specify json parameters in mocha's request:

it('checks creation of new mark', function(done){    request.post('http://server:port/marks', function(err, response, body){    // headers , parameters should set in request    response.statuscode.should.equal(201);   done(); }); 

});

the test below (get request) works though:

it('checks existence of marks user dummyuser', function(done){   request.get('http://server:port/user/dummyuser/marks', function(err, response, body){     response.statuscode.should.equal(200);     done();   }); }); 

update

the following works charm: (i though request kind of variable created mocha).

 request(   { method: 'post'   , uri: 'http://server:port/marks'   , headers: { 'content-type': 'application/json' , 'accept': 'application/json' }   , json: { "content":{"value":"18,5", "date": "2012-04-13"} }   } , function(err, response, body){   response.statuscode.should.equal(201);   done(); }); 

take @ documentation. there great explaination of how post custom headers. 1 way of doing works me following.

var options = {   host: 'localhost',   port: 80,   path: '/echo/200',   method: 'post',   headers: {     "x-terminal-id" : terminalid   } }; var data = "" var req = https.request(options, function(res) {   res.on('data', function(d) {     data += d;   });   res.on('end', function(err){     //check data expected     done(null, data)   }) }); req.end();  req.on('error', function(err) {}   done(err)  }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -