UNPKG

hmetrics

Version:

The metrics of the HTTP/HTTPS request process

123 lines (115 loc) 3.6 kB
'use strict'; const nock = require('nock'); const request = require('../index').request; const should = require('should'); nock.restore(); describe('index.js', function() { context('request', function() { it('should return result when having a HTTP request', function(done) { this.timeout(10000); request({ hostname: 'www.sina.com.cn', port: 80, 'path': '/', method: 'GET' }, function(err, data) { should.ifError(err); should.exist(data); should.equal(data.time_appconnect, 0); should.notEqual(data.time_namelookup, 0); should.notEqual(data.time_starttransfer, 0); should.notEqual(data.time_total, 0); should.notEqual(data.time_connect, 0); return done(); }); }); it('should return result when having a HTTPS request', function(done) { this.timeout(10000); request('https://www.baidu.com', function(err, data) { should.ifError(err); should.exist(data); should.notEqual(data.time_appconnect, 0); should.notEqual(data.time_namelookup, 0); should.notEqual(data.time_starttransfer, 0); should.notEqual(data.time_total, 0); should.notEqual(data.time_connect, 0); return done(); }); }); it('should return result when having a HTTPS request and host is IP without certificates', function(done) { this.timeout(10000); request('https://115.239.210.27', function(err) { should.exist(err); return done(); }); }); it('should return result when having a HTTP request and host is IP', function(done) { this.timeout(10000); request('http://61.172.201.194', function(err, data) { should.ifError(err); should.exist(data); should.equal(data.time_appconnect, 0); should.equal(data.time_namelookup, 0); should.notEqual(data.time_starttransfer, 0); should.notEqual(data.time_total, 0); should.notEqual(data.time_connect, 0); return done(); }); }); it('should return error when unable to determine the domain name', function(done) { this.timeout(10000); request('www.baidu.com', function(err) { should.exist(err); return done(); }); }); it('should return error when request connect timeout', function(done) { this.timeout(10000); request({ hostname: 'www.sina.com.cn', port: 80, 'path': '/', method: 'GET', connect_timeout: 0.0001 }, function(err, data) { should.exist(err); return done(); }); }); it('should return error when request connect timeout', function(done) { this.timeout(10000); request({ hostname: 'www.sina.com.cn', port: 80, 'path': '/', method: 'GET', timeout: 0.0001 }, function(err, data) { should.exist(err); return done(); }); }); context('when socket is delayed', function() { before(function() { nock('http://my.server.com:8081') .get('/') .socketDelay(1000) .times(1) .reply(200, '<html></html>'); }); it('should return error', function(done) { this.timeout(10000); request({ hostname: 'my.server.com', port: 8081, 'path': '/', method: 'GET', connect_timeout: 0.5 }, function(err, data) { should.exist(err); return done(); }); }); }); }); });