import { Eltro as t, assert, stub } from 'eltro' import http from 'http' import https from 'https' import { request } from '../core/client.mjs' import HttpServer from '../core/http.mjs' const port = 61413 let prefix = `http://localhost:${port}/` t.describe('config', function() { t.test('should use https if https is true', function() { let server = new HttpServer() assert.strictEqual(server.creator, http) assert.strictEqual(server.ishttps, false) server = new HttpServer({ https: true }) assert.strictEqual(server.creator, https) assert.strictEqual(server.ishttps, true) }) }) t.describe('Sockets', function() { let http = new HttpServer() t.after(function() { http.closeServer().then(function() { }, function(err) { console.error(err) }) }) t.test('should keep track of sockets through its lifetime', function(cb) { let actives = [] let server = http.createServer(function(req, res) { req.on('error', function(err) { cb(err) }) res.on('error', function(err) { cb(err) }) res.on('finish', function() { }) actives.push(res) }) Promise.resolve() .then(async function() { await new Promise(function(res, rej) { server.listen(port, function(err) { if (err) rej(err); res()}) }) assert.strictEqual(actives.length, 0) assert.strictEqual(http.sockets.size, 0) request({}, prefix).then(function() {}, cb) request({}, prefix).then(async function() { while (http.sockets.size > 0) { await new Promise(function(res) { setTimeout(res, 10) }) } assert.strictEqual(http.sockets.size, 0) cb() }, cb) while (actives.length < 2) { await new Promise(function(res) { setTimeout(res, 10) }) } assert.strictEqual(http.sockets.size, 2) actives[0].statusCode = 200 actives[0].end('{}') actives[1].statusCode = 200 actives[1].end('{}') }).catch(cb) }) }) t.describe('Close', function() { let http = new HttpServer() t.after(function() { http.closeServer().then(function() { }, function(err) { console.error(err) }) }) t.test('should support forcefully closing them on server close', function(cb) { let requestErrors = [] let serverErrors = [] let server = http.createServer(function(req, res) { req.on('error', function(err) { serverErrors.push(err) }) res.on('error', function(err) { serverErrors.push(err) }) res.on('finish', function() { }) }) Promise.resolve() .then(async function() { await new Promise(function(res, rej) { server.listen(port, function(err) { if (err) rej(err); res()}) }) assert.strictEqual(http.sockets.size, 0) request({}, prefix).then( function() { cb(new Error('first succeeded')) }, function(err) { requestErrors.push(err) } ) request({}, prefix).then( function() { cb(new Error('first succeeded')) }, function(err) { requestErrors.push(err) } ) while (http.sockets.size < 2) { await new Promise(function(res) { setTimeout(res, 10) }) } http.closeServer().then(function() { }, cb) while (requestErrors.length < 2) { await new Promise(function(res) { setTimeout(res, 10) }) } assert.strictEqual(http.sockets.size, 0) assert.strictEqual(requestErrors.length, 2) assert.strictEqual(serverErrors.length, 2) assert.strictEqual(serverErrors[0].code, 'ECONNRESET') assert.strictEqual(serverErrors[1].code, 'ECONNRESET') assert.strictEqual(requestErrors[0].code, 'ECONNRESET') assert.strictEqual(requestErrors[1].code, 'ECONNRESET') while (requestErrors.length < 2) { await new Promise(function(res) { setTimeout(res, 10) }) } while (http.active) { await new Promise(function(res) { setTimeout(res, 10) }) } }) .then(function() { cb()}, cb) }) })