Expand|Select|Wrap|Line Numbers
- app.post('/encrypt', encrypt);
Expand|Select|Wrap|Line Numbers
- function encrypt(req,res) {
- if(req.body.key && req.body.message) {
- var encryptedMessage = Encrypter.encrypt(req.body.key,req.body.message);
- return res.status(200).json({ message: encryptedMessage });
- }
- res.status(409).json({ message: 'the message could not be encrypted, no key found' });
- }
- }
At the same time, I'm testing my thing with mocha and I'm doing it like so:
Expand|Select|Wrap|Line Numbers
- describe('# Here is where the fun starts ', function () {
- /**
- * Start and stop the server
- */
- before(function () {
- server.listen(port);
- });
- after(function () {
- server.close();
- });
- it('Requesting an encrypted message', function(done) {
- var postData = querystring.stringify({
- key : key,
- message : message
- });
- var options = {
- hostname: hostname,
- port: port,
- path: '/encrypt',
- method: 'POST',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Content-Length': postData.length
- }
- };
- var req = http.request(options, function(res) {
- res.statusCode.should.equal(200);
- var encryptedMessage = res.message;
- encryptedMessage.should.not.equal(message);
- done();
- });
- req.on('error', function(e) {
- //I'm aware should.fail doesn't work like this
- should.fail('problem with request: ' + e.message);
- });
- req.write(postData);
- req.end();
- });
- });
None of the res.on (data, end, events is working, so I suppose the data should be available from there. First I had this:
Expand|Select|Wrap|Line Numbers
- var req = http.request(options, function(res) {
- res.statusCode.should.equal(200);
- var encryptedMessage;
- res.on('data', function (chunk) {
- console.log('BODY: ' + chunk);
- encryptedMessage = chunk.message;
- });
- encryptedMessage.should.not.equal(message);
- done();
- });
Weird enough, if I launch a test server and then I curl it
Expand|Select|Wrap|Line Numbers
- curl --data "key=secret&message=veryimportantstuffiabsolutellyneedtoprotect" localhost:2409/encrypt