Friday, 12 May 2017

(Node.js/mocha/chai) Sending parameters in request object in a HTTP POST

I'm trying to test someone else's POST route (using mocha/chai/sinon) and I'm running into a wall. Here's what works so far: Most of the information grabbed in the post route is retrieved from req.body as illustrated in this snippet.

  var participants = JSON.parse(req.body.participantList);
  var scenarioId = req.body.scenarioId;

I'm able to send up these variables using the test code below because .send() places parameters into the body.

chai.request(server)
  .post('/viewer/add-exercise-participants')
  .send({"scenarioId": 9999, "user": {"id": 1234}, 'participantList': '['
      + '{"user_id": 1111, "role_id": [4444]},'
      + '{"user_id": 2222, "role_id": [1111]},'
      + '{"user_id": 4444, "role_id": [3333, 4444]}'
    + ']'
  })
  .end(function(err, res){
    console.log(res.body);
    done();
  });

So just to be clear, what I have above is working so far. Here is where I run into issue. Later down within the same route, they want a req.user.id which is throwing me off. Since req.user.id is not inside of body I don't know how to send that with my HTTP POST request.

var requserid = req.user.id;

How do I set this req.user.id variable when I call make a request to this route? The project is in node.js and I'm testing unit testing using the mocha/chai/sinon packages.



via jswerve

No comments:

Post a Comment