Blame SOURCES/test.js

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