Blame SOURCES/test.js

58790f
/* global describe, before, after, it */
58790f
58790f
'use strict';
58790f
58790f
var assert = require('assert');
58790f
var http = require('http');
58790f
var timeout = require('./');
58790f
58790f
it('should do HTTP request with a lot of time', function (done) {
58790f
	var req = http.get('http://google.com', function (res) {
58790f
		assert.ok(res.statusCode > 300 && res.statusCode < 399);
58790f
		done();
58790f
	});
58790f
58790f
	req.on('error', done);
58790f
58790f
	timeout(req, 1000);
58790f
});
58790f
58790f
it('should emit ETIMEDOUT when time is not enough', function (done) {
58790f
	var req = http.get('http://google.com', function () {});
58790f
58790f
	req.on('error', function (err) {
58790f
		if (err.code === 'ETIMEDOUT') {
58790f
			assert.equal(err.message, 'Connection timed out on request to google.com');
58790f
			done();
58790f
		}
58790f
	});
58790f
58790f
	timeout(req, 1);
58790f
});
58790f
58790f
describe('when only headers was sent', function () {
58790f
	var server;
58790f
58790f
	before(function (done) {
58790f
		server = http.createServer(function (request, res) {
58790f
			setTimeout(function() {
58790f
				res.writeHead(200, {'content-type':'text/plain'});
58790f
				res.write('waited');
58790f
				res.end();
58790f
			}, 200);
58790f
		});
58790f
58790f
		server.listen(8081, function (err) {
58790f
			done(err);
58790f
		});
58790f
	});
58790f
58790f
	after(function (done) {
58790f
		server.close(done);
58790f
	});
58790f
58790f
	it('should emit ESOCKETTIMEDOUT', function (done) {
58790f
		var req = http.get('http://0.0.0.0:8081', function () {});
58790f
58790f
		req.on('error', function (err) {
58790f
			if (err.code === 'ESOCKETTIMEDOUT') {
58790f
				assert.equal(err.message, 'Socket timed out on request to 0.0.0.0:8081');
58790f
				done();
58790f
			}
58790f
		});
58790f
58790f
		timeout(req, 400);
58790f
	});
58790f
});