Disabling console logging with http.get - node.js

I have this code:
var should = require('should'),
http = require('http'),
apis = {
baseUrl: 'http://11.0.0.20:4000',
listLiveMatches: '/api/blah/'
};
describe('GET ' + apis.baseUrl + apis.listLiveMatches, function () {
'use strict';
var liveListUrl = apis.baseUrl + apis.listLiveMatches;
it('is alive & returning 200', function (done) {
http.get(liveListUrl, function (res) {
res.should.be.ok;
res.should.have.property('statusCode');
res.statusCode.should.equal(200);
done();
});
});
it('is emitting json response', function (done) {
http.get(liveListUrl, function (res) {
res.should.be.ok;
res.should.have.property('headers');
res.headers['content-type'].should.startWith('application/json');
done();
});
});
});
My gulp task looks like this:
gulp.task('test-server', function () {
return gulp.src('test/rest-api.js')
.pipe(mocha({
reporter: 'spec'
}))
.on('error', function(){
console.log('error');
});
});
My only issue is that with every http.get, the console gets the body of the response logged which I want to prevent, google searches for disabling logging on http.get have yielded nothing.

Related

LdapJS exop Request timeout

I have an LdapJS server which implements standard operation and an extended operation to check health:
const server = ldap.createServer();
server.exop('healthcheck', (req, res, next) => {
res.end();
console.log('ended');
return next();
});
...
Then I wrote a simple client script to ping healthcheck service:
const { createClient } = require('ldapjs');
const client = createClient({
url: 'ldap://localhost:1389',
timeout: 2000,
connectTimeout: 2000
});
client.exop('healthcheck', (err, value, res) => {
if (err) {
console.log(`ERROR: ${err.message}`);
process.exit(1);
}
else {
console.log(`STATUS: ${res.status}`);
process.exit(0);
}
});
The problem is that the exop is correctly received by server (I can see the log inside its callback), but the client always logs: ERROR: request timeout (client interrupt).
Why the request is not correctly terminated?
EDIT
I wrote a mocha test for the exop and it works. Seems that the problem is related to the standalone call in healthcheck script.
describe('#healthcheck()', function () {
before(function () {
server = createServer();
server.listen(config.get('port'), config.get('host'), () => {});
});
after(function () {
server.close();
});
it('should return status 0', function (done) {
const { createClient } = require('ldapjs');
const client = createClient({
url: 'ldap://localhost:1389',
timeout: 2000,
connectTimeout: 2000
});
client.exop('healthcheck', (err, value, res) => {
should.not.exist(err);
res.status.should.be.equal(0);
client.destroy();
return done();
});
});
});

Getting error of timeout exceed in mocha

mocha error: timeout of 2000ms exceeded, for async test and hooks ensure the done() callback is being called in this test. if returning a promise, ensure it resolves.
This is what the error i am getting while trying to get response,
This is my index.js file where i export function
exports.info = function(callback) {
var https = require('https');
var options = {
host: 'api.github.com',
path: '/repos/sayanee/build-podcast',
method: 'GET',
headers: { 'User-Agent': 'sayanee' } };
var str = '';
https.request(options, function(response) {
response.on('data', function(data) {
str += data;
});
response.on('end', function() {
callback(JSON.parse(str));
})
response.on('error', function(error) {
console.log(error);
callback();
})
}) .end();
}
This is my indexfile where i describe the test cases
function asyncFunction() {
return new Promise(resolve => {
setTimeout(resolve, 5000);
});
}
describe('Github info', function() {
it.only('returns repo info from github', async function() {
//this.timeout(5000);
await asyncFunction();
word.info(function(reply) {
console.log("er")
expect(reply.language).to.equal('JavaScript');
expect(reply.watchers).to.equal(157);
console.log('RECEIVED');
});
console.log('HELLO'); })
});
Mocha support async test also by passing done callback as param to it that you need to call at test end
describe("Github info", function () {
it.only("returns repo info from github", function (done) {
// set long timeout to be sure word.info finish
this.timeout(5000);
word.info(function (reply) {
console.log("er");
expect(reply.language).to.equal("JavaScript");
expect(reply.watchers).to.equal(157);
console.log("RECEIVED");
// call done at end
done();
});
console.log("HELLO");
});
});
The response is in your question. Mocha is setted up to timeout after 2 second.
Either you makes your request to finish within 2000ms
Either you increase the Mocha timeout, example :
mocha -t 300000
EDIT :
You cannot use async/await mixed up with callbacks
// Wrap your function into a promise
wordInfoPromise() {
return new Promise((resolve, reject) => {
word.info((ret) => {
if (!ret) return reject();
return resolve(ret);
});
});
}
it('returns repo info from github', async function() {
//this.timeout(5000);
await asyncFunction();
const reply = await wordInfoPromise();
console.log("er")
expect(reply.language).to.equal('JavaScript');
expect(reply.watchers).to.equal(157);
console.log('RECEIVED');
console.log('HELLO'); })
});
EDIT 2 :
const req = https.request(options, (res) => {
res.on('data', (d) => {
str += data;
});
res.on('end', () => {
resolve(str);
});
});
req.on('error', (e) => {
reject();
});
req.end();

express middleware testing mocha chai

Is there a way to test those kind of middleware in express:
module.exports = function logMatchingUrls(pattern) {
return function (req, res, next) {
if (pattern.test(req.url)) {
console.log('request url', req.url);
req.didSomething = true;
}
next();
}
}
The only middleware testing i found was:
module.exports = function(request, response, next) {
/*
* Do something to REQUEST or RESPONSE
**/
if (!request.didSomething) {
console.log("dsdsd");
request.didSomething = true;
next();
} else {
// Something went wrong, throw and error
var error = new Error();
error.message = 'Error doing what this does'
next(error);
}
};
describe('Middleware test', function(){
context('Valid arguments are passed', function() {
beforeEach(function(done) {
/*
* before each test, reset the REQUEST and RESPONSE variables
* to be send into the middle ware
**/
requests = httpMocks.createRequest({
method: 'GET',
url: '/css/main.css',
query: {
myid: '312'
}
});
responses = httpMocks.createResponse();
done(); // call done so that the next test can run
});
it('does something', function(done) {
/*
* Middleware expects to be passed 3 arguments: request, response, and next.
* We are going to be manually passing REQUEST and RESPONSE into the middleware
* and create an function callback for next in which we run our tests
**/
middleware(responses, responses, function next(error) {
/*
* Usually, we do not pass anything into next except for errors, so because
* in this test we are passing valid data in REQUEST we should not get an
* error to be passed in.
**/
if (error) { throw new Error('Expected not to receive an error'); }
// Other Tests Against request and response
if (!responses.didSomething) { throw new Error('Expected something to be done'); }
done(); // call done so we can run the next test
}); // close middleware
}); // close it
}); // close context
}); // close describe
This work well with the simple middleware (it like testing basic function with callback) provided above but with more complex middleware i cannot get it work. Is it possible to test this kind of middleware?
Here's a simple setup that you could use, using chai and sinon:
var expect = require('chai').expect;
var sinon = require('sinon');
var middleware = function logMatchingUrls(pattern) {
return function (req, res, next) {
if (pattern.test(req.url)) {
console.log('request url', req.url);
req.didSomething = true;
}
next();
}
}
describe('my middleware', function() {
describe('request handler creation', function() {
var mw;
beforeEach(function() {
mw = middleware(/./);
});
it('should return a function()', function() {
expect(mw).to.be.a.Function;
});
it('should accept three arguments', function() {
expect(mw.length).to.equal(3);
});
});
describe('request handler calling', function() {
it('should call next() once', function() {
var mw = middleware(/./);
var nextSpy = sinon.spy();
mw({}, {}, nextSpy);
expect(nextSpy.calledOnce).to.be.true;
});
});
describe('pattern testing', function() {
...
});
});
From there, you can add more elaborate tests for the pattern matching, etc. Since you're only using req.url, you don't have to mock an entire Request object (as created by Express) and you can just use a simple object with a url property.
I used node-mocks-http to unit test my middleware. Here's my code:
function responseMiddleware(req, res, next) {
res.sendResponse = (...args) => {
//<==== Code removed from here
};
next();
}
And in my spec file I did it like this:
var expect = require('chai').expect;
var sinon = require('sinon');
var responseMiddleware = require('./response');
var httpMocks = require('node-mocks-http');
describe('request handler calling', function() {
it('should call next() once', function() {
var nextSpy = sinon.spy();
responseMiddleware({}, {}, nextSpy);
expect(nextSpy.calledOnce).to.be.true;
});
it('should add sendResponse key', function() {
var nextSpy = sinon.spy();
var req = httpMocks.createRequest();
var res = httpMocks.createResponse();
responseMiddleware(req, res, nextSpy);
expect(nextSpy.calledOnce).to.be.true;
responseMiddleware(req, res, () => {
expect(res).to.have.property('sendResponse');
})
});
});
If you are using async calls then you can use await and then call done() after that.

Grab specific response properties from SuperTest

I want to be able to grab some response properties and throw them into a variable at times with SuperTest. How can I do this? I don't see the docs doing anything but assertions on the response.
for example I'd like to do something like this:
var statusCode = request(app).get(uri).header.statusCode;
I'd like to do something like this. Because sometimes I like to split out the asserts into seperate Mocha.js it() tests due to the fact I'm doing BDD and so the 'Thens' in this case are based on the expected response parts so each test is checking for a certain state coming back in a response.
for example I'd like to do this with supertest:
var response = request(app).get(uri);
it('status code returned is 204, function(){
response.status.should.be....you get the idea
};
it('data is a JSON object array', function(){
};
Here is an example how you can accomplish what you want:
server file app.js:
var express = require('express');
var app = express();
var port = 4040;
var items = [{name: 'iphone'}, {name: 'android'}];
app.get('/api/items', function(req, res) {
res.status(200).send({items: items});
});
app.listen(port, function() {
console.log('server up and running at %s:%s', app.hostname, port);
});
module.exports = app;
test.js:
var request = require('supertest');
var app = require('./app.js');
var assert = require('assert');
describe('Test API', function() {
it('should return 200 status code', function(done) {
request(app)
.get('/api/items')
.end(function(err, response) {
if (err) { return done(err); }
assert.equal(response.status, 200);
done();
});
});
it('should return an array object of items', function(done) {
request(app)
.get('/api/items')
.end(function(err, response) {
if (err) { return done(err); }
var items = response.body.items;
assert.equal(Array.isArray(items), true);
done();
});
});
it('should return a JSON string of items', function(done) {
request(app)
.get('/api/items')
.end(function(err, response) {
if (err) { return done(err); }
try {
JSON.parse(response.text);
done();
} catch(e) {
done(e);
}
});
});
});
You can see some examples here on the superagent github library since supertest is based on superagent library.

Express: npm test returns "connect ECONNREFUSED"

I'm trying to test my app, and it always returns an error of connect ECONNREFUSED. I made a simple example to show what's happening. Here's my controller (CompoundJS code):
load('application');
action('test', function() {
var obj = {success: true, data: 'blah'};
send(obj);
});
action(function show(data) {
var http = require('http');
var options = {
path: '/getTest',
port: process.env.PORT // without this, http fails because default port is 80
};
var req = http.get(options, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
data = JSON.parse(data);
return send(data);
});
});
req.on('error', function(e) {
return send({success: false, data: e.message}); // returns "connect ECONNREFUSED"
});
});
So when I have the app running, I can hit /test (which is the show method there) and /getTest just fine without any errors. However, when I try to run the following test code, I get the error as stated above, and the issue comes down to that http.get, as I can get into the show function just fine.
var app, compound
, request = require('supertest')
, sinon = require('sinon');
function TestStub() {
return {
};
}
describe('TestController', function() {
beforeEach(function(done) {
app = getApp();
compound = app.compound;
compound.on('ready', function() {
done();
});
});
/*
* GET /tests
* Should render tests/index.ejs
*/
it('should render "index" template on GET /tests', function(done) {
request(app)
.get('/test')
.end(function(err, res) {
console.log(res.body);
done();
});
});
});
Any ideas on how to fix this? Cross posted from the CompoundJS Google Group.

Resources