import { Eltro as t, assert, stub, spy } from 'eltro' import { Flaska, HttpError } from '../flaska.mjs' import { createCtx, fakeHttp } from './helper.mjs' const faker = fakeHttp() t.test('should be able to override the http', function() { let flaska = new Flaska({}, faker) assert.strictEqual(flaska.http, faker) }) t.test('it should have all the common verbs', function() { let flaska = new Flaska({}, faker) assert.ok(flaska.get) assert.strictEqual(typeof(flaska.get), 'function') assert.ok(flaska.post) assert.strictEqual(typeof(flaska.post), 'function') assert.ok(flaska.put) assert.strictEqual(typeof(flaska.put), 'function') assert.ok(flaska.delete) assert.strictEqual(typeof(flaska.delete), 'function') assert.ok(flaska.options) assert.strictEqual(typeof(flaska.options), 'function') assert.ok(flaska.patch) assert.strictEqual(typeof(flaska.patch), 'function') }) t.test('the verbs GET and HEAD should be identical', function() { let flaska = new Flaska({}, faker) assert.ok(flaska.get) assert.strictEqual(typeof(flaska.get), 'function') assert.notOk(flaska.head) assert.ok(flaska.routers['HEAD']) assert.strictEqual(flaska.routers['GET'], flaska.routers['HEAD']) }) t.describe('#log', function() { t.test('default have a logger valid', function() { let flaska = new Flaska({}, faker) assert.strictEqual(typeof(flaska.log.error), 'function') assert.strictEqual(typeof(flaska.log.info), 'function') assert.strictEqual(typeof(flaska.log.warn), 'function') }) t.test('allow overwriting in options', function() { const assertFunction = function() { return 1 } let flaska = new Flaska({ log: { error: assertFunction, info: assertFunction, warn: assertFunction, debug: assertFunction, } }, faker) assert.strictEqual(flaska.log.error, assertFunction) assert.strictEqual(flaska.log.info, assertFunction) assert.strictEqual(flaska.log.warn, assertFunction) assert.strictEqual(flaska.log.debug, assertFunction) }) }) let specialHandlers = ['on404', 'onerror', 'onreqerror', 'onreserror'] specialHandlers.forEach(function(type) { t.describe(`#${type}()`, function() { t.test('exist', function() { let flaska = new Flaska({}, faker) assert.strictEqual(typeof(flaska[type]), 'function') }) t.test('validate handler', function() { let flaska = new Flaska({}, faker) assert.throws(function() { flaska[type]() }, /[Ff]unction/) assert.throws(function() { flaska[type]('asdf') }, /[Ff]unction/) assert.throws(function() { flaska[type]('123') }, /[Ff]unction/) assert.throws(function() { flaska[type]([]) }, /[Ff]unction/) assert.throws(function() { flaska[type]({}) }, /[Ff]unction/) assert.throws(function() { flaska[type](1234) }, /[Ff]unction/) }) if (type !== 'on404') { t.test('should call ctx.log.error correctly', function() { const assertError = new Error('Samuraism') let flaska = new Flaska({}, faker) let ctx = createCtx() flaska['_' + type](assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) }) } if (type === 'onreqerror') { t.test('default sends 400 immediately', function() { let flaska = new Flaska({}, faker) let ctx = createCtx() flaska['_' + type](new Error(), ctx) assert.strictEqual(ctx.res.statusCode, 400) assert.strictEqual(ctx.res.end.callCount, 1) assert.strictEqual(ctx.res.end.firstCall.length, 0) }) } t.test('register it into flaska', function() { const assertFunction = function() { return true } let flaska = new Flaska({}, faker) flaska[type](assertFunction) assert.strictEqual(flaska['_' + type], assertFunction) }) }) }) t.describe('_on404', function() { t.test('a valid function', function() { let flaska = new Flaska({}, faker) assert.strictEqual(typeof(flaska._on404), 'function') }) t.test('default valid handling of context', function() { let ctx = createCtx() let flaska = new Flaska({}, faker) flaska._on404(ctx) assert.strictEqual(ctx.status, 404) assert.deepStrictEqual(ctx.body, { status: 404, message: 'Not Found', }) }) }) t.describe('_onerror', function() { t.test('a valid function', function() { let flaska = new Flaska({}, faker) assert.strictEqual(typeof(flaska._onerror), 'function') }) t.test('default valid handling of context', function() { const assertError = new Error('should not be seen') let ctx = createCtx() let flaska = new Flaska({}, faker) flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, 500) assert.deepStrictEqual(ctx.body, { status: 500, message: 'Internal Server Error', }) }) t.test('default valid handling of HttpError', function() { const assertStatus = 431 const assertBody = { a: 1 } const assertError = new HttpError(assertStatus, 'should not be seen', assertBody) let ctx = createCtx() let flaska = new Flaska({}, faker) flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.body, assertBody) }) t.test('default valid handling of HttpError with no body', function() { const assertStatus = 413 const assertError = new HttpError(assertStatus, 'should not be seen') let ctx = createCtx() let flaska = new Flaska({}, faker) flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, assertStatus) assert.deepStrictEqual(ctx.body, { status: assertStatus, message: 'Payload Too Large', }) }) t.test('default valid handling of HttpError with missing status message', function() { const assertStatus = 432 const assertError = new HttpError(assertStatus, 'should not be seen') let ctx = createCtx() let flaska = new Flaska({}, faker) flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, assertStatus) assert.deepStrictEqual(ctx.body, { status: assertStatus, message: 'Internal Server Error', }) }) }) t.describe('_onreqerror', function() { t.test('a valid function', function() { let flaska = new Flaska({}, faker) assert.strictEqual(typeof(flaska._onreqerror), 'function') }) t.test('default valid handling of aborted', function() { const assertError = new Error('aborted') let flaska = new Flaska({}, faker) let ctx = createCtx() flaska._onreqerror(assertError, ctx) assert.strictEqual(ctx.log.info.callCount, 1) assert.strictEqual(ctx.log.info.firstCall[0], assertError) }) }) t.describe('#devMode()', function() { t.test('turns on debug mode', function() { const assertErrorMessage = 'Fascination' const assertError = new Error(assertErrorMessage) let ctx = createCtx() let flaska = new Flaska({}, faker) flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, 500) assert.deepStrictEqual(ctx.body, { status: 500, message: 'Internal Server Error', }) ctx = createCtx() flaska._backuperror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, 500) assert.deepStrictEqual(ctx.body, { status: 500, message: 'Internal Server Error', }) flaska.devMode() ctx = createCtx() flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, 500) assert.strictEqual(ctx.body.status, 500) assert.match(ctx.body.message, /Internal Server Error/) assert.match(ctx.body.message, new RegExp(assertErrorMessage)) assert.ok(ctx.body.stack) ctx = createCtx() flaska._backuperror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, 500) assert.strictEqual(ctx.body.status, 500) assert.match(ctx.body.message, /Internal Server Error/) assert.match(ctx.body.message, new RegExp(assertErrorMessage)) assert.ok(ctx.body.stack) }) t.test('default valid handling of HttpError', function() { const assertStatus = 431 const assertBody = { a: 1 } const assertError = new HttpError(assertStatus, 'should not be seen', assertBody) let ctx = createCtx() let flaska = new Flaska({}, faker) flaska.devMode() flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.body, assertBody) }) t.test('default valid handling of HttpError with no body', function() { const assertStatus = 413 const assertErrorMessage = 'A day' const assertError = new HttpError(assertStatus, assertErrorMessage) let ctx = createCtx() let flaska = new Flaska({}, faker) flaska.devMode() flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.body.status, assertStatus) assert.match(ctx.body.message, /Payload Too Large/) assert.match(ctx.body.message, new RegExp(assertErrorMessage)) }) t.test('default valid handling of HttpError with missing status message', function() { const assertStatus = 432 const assertErrorMessage = 'Jet Coaster Ride' const assertError = new HttpError(assertStatus, assertErrorMessage) let ctx = createCtx() let flaska = new Flaska({}, faker) flaska.devMode() flaska._onerror(assertError, ctx) assert.strictEqual(ctx.log.error.callCount, 1) assert.strictEqual(ctx.log.error.firstCall[0], assertError) assert.strictEqual(ctx.status, assertStatus) assert.strictEqual(ctx.body.status, assertStatus) assert.match(ctx.body.message, /Internal Server Error/) }) }) t.describe('#before()', function() { t.test('should throw if not a function', function() { let flaska = new Flaska({}, faker) assert.throws(function() { flaska.before() }, /[Ff]unction/) assert.throws(function() { flaska.before('asdf') }, /[Ff]unction/) assert.throws(function() { flaska.before('123') }, /[Ff]unction/) assert.throws(function() { flaska.before([]) }, /[Ff]unction/) assert.throws(function() { flaska.before({}) }, /[Ff]unction/) assert.throws(function() { flaska.before(1234) }, /[Ff]unction/) }) t.test('add handler to preflight list', function() { const assertFunction = function() {} let flaska = new Flaska({}, faker) assert.ok(flaska._before) flaska.before(assertFunction) assert.strictEqual(flaska._before.length, 1) assert.strictEqual(flaska._before[0], assertFunction) }) }) t.describe('#beforeAsync()', function() { t.test('should throw if not a function', function() { let flaska = new Flaska({}, faker) assert.throws(function() { flaska.beforeAsync() }, /[Ff]unction/) assert.throws(function() { flaska.beforeAsync('asdf') }, /[Ff]unction/) assert.throws(function() { flaska.beforeAsync('123') }, /[Ff]unction/) assert.throws(function() { flaska.beforeAsync([]) }, /[Ff]unction/) assert.throws(function() { flaska.beforeAsync({}) }, /[Ff]unction/) assert.throws(function() { flaska.beforeAsync(1234) }, /[Ff]unction/) }) t.test('add handler to preflight list', function() { const assertFunction = function() {} let flaska = new Flaska({}, faker) assert.ok(flaska._beforeAsync) flaska.beforeAsync(assertFunction) assert.strictEqual(flaska._beforeAsync.length, 1) assert.strictEqual(flaska._beforeAsync[0], assertFunction) }) }) t.describe('#after()', function() { t.test('should throw if not a function', function() { let flaska = new Flaska({}, faker) assert.throws(function() { flaska.after() }, /[Ff]unction/) assert.throws(function() { flaska.after('asdf') }, /[Ff]unction/) assert.throws(function() { flaska.after('123') }, /[Ff]unction/) assert.throws(function() { flaska.after([]) }, /[Ff]unction/) assert.throws(function() { flaska.after({}) }, /[Ff]unction/) assert.throws(function() { flaska.after(1234) }, /[Ff]unction/) }) t.test('add handler to preflight list', function() { const assertFunction = function() {} let flaska = new Flaska({}, faker) assert.ok(flaska._after) flaska.after(assertFunction) assert.strictEqual(flaska._after.length, 1) assert.strictEqual(flaska._after[0], assertFunction) }) }) t.describe('#afterAsync()', function() { t.test('should throw if not a function', function() { let flaska = new Flaska({}, faker) assert.throws(function() { flaska.afterAsync() }, /[Ff]unction/) assert.throws(function() { flaska.afterAsync('asdf') }, /[Ff]unction/) assert.throws(function() { flaska.afterAsync('123') }, /[Ff]unction/) assert.throws(function() { flaska.afterAsync([]) }, /[Ff]unction/) assert.throws(function() { flaska.afterAsync({}) }, /[Ff]unction/) assert.throws(function() { flaska.afterAsync(1234) }, /[Ff]unction/) }) t.test('add handler to preflight list', function() { const assertFunction = function() {} let flaska = new Flaska({}, faker) assert.ok(flaska._afterAsync) flaska.afterAsync(assertFunction) assert.strictEqual(flaska._afterAsync.length, 1) assert.strictEqual(flaska._afterAsync[0], assertFunction) }) }) t.describe('#compile()', function() { t.test('join all before together in one function', function() { let flaska = new Flaska({}, faker) flaska.before(function(ctx) { ctx.a = 1 }) flaska.before(function(ctx) { ctx.b = 2 }) flaska.before(function(ctx) { ctx.c = 3 }) flaska.before(function(ctx) { ctx.d = 4 }) assert.notOk(flaska._beforeCompiled) flaska.compile() assert.ok(flaska._beforeCompiled) assert.notOk(flaska._beforeAsyncCompiled) assert.strictEqual(typeof(flaska._beforeCompiled), 'function') let ctx = createCtx() flaska._beforeCompiled(ctx) assert.strictEqual(ctx.a, 1) assert.strictEqual(ctx.b, 2) assert.strictEqual(ctx.c, 3) assert.strictEqual(ctx.d, 4) }) t.test('join all beforeAsync together in one function', function() { let flaska = new Flaska({}, faker) flaska.beforeAsync(function(ctx) { return Promise.resolve().then(function() { ctx.a = 1 }) }) flaska.beforeAsync(function(ctx) { ctx.b = 2 }) flaska.beforeAsync(function(ctx) { return new Promise(function(res) { ctx.c = 3; res() }) }) flaska.beforeAsync(function(ctx) { ctx.d = 4 }) assert.notOk(flaska._beforeAsyncCompiled) flaska.compile() assert.ok(flaska._beforeAsyncCompiled) assert.strictEqual(typeof(flaska._beforeAsyncCompiled), 'function') let ctx = createCtx() return flaska._beforeAsyncCompiled(ctx).then(function() { assert.strictEqual(ctx.a, 1) assert.strictEqual(ctx.b, 2) assert.strictEqual(ctx.c, 3) assert.strictEqual(ctx.d, 4) }) }) t.test('join all after together in one function', function() { let flaska = new Flaska({}, faker) flaska.after(function(ctx) { ctx.a = 1 }) flaska.after(function(ctx) { ctx.b = 2 }) flaska.after(function(ctx) { ctx.c = 3 }) flaska.after(function(ctx) { ctx.d = 4 }) assert.notOk(flaska._afterCompiled) flaska.compile() assert.ok(flaska._afterCompiled) assert.notOk(flaska._afterAsyncCompiled) assert.strictEqual(typeof(flaska._afterCompiled), 'function') let ctx = createCtx() flaska._afterCompiled(ctx) assert.strictEqual(ctx.a, 1) assert.strictEqual(ctx.b, 2) assert.strictEqual(ctx.c, 3) assert.strictEqual(ctx.d, 4) }) t.test('join all afterAsync together in one function', function() { let flaska = new Flaska({}, faker) flaska.afterAsync(function(ctx) { return Promise.resolve().then(function() { ctx.a = 1 }) }) flaska.afterAsync(function(ctx) { ctx.b = 2 }) flaska.afterAsync(function(ctx) { return new Promise(function(res) { ctx.c = 3; res() }) }) flaska.afterAsync(function(ctx) { ctx.d = 4 }) assert.notOk(flaska._afterAsyncCompiled) flaska.compile() assert.ok(flaska._afterAsyncCompiled) assert.strictEqual(typeof(flaska._afterAsyncCompiled), 'function') let ctx = createCtx() return flaska._afterAsyncCompiled(ctx).then(function() { assert.strictEqual(ctx.a, 1) assert.strictEqual(ctx.b, 2) assert.strictEqual(ctx.c, 3) assert.strictEqual(ctx.d, 4) }) }) }) t.describe('#handleMiddleware()', function() { t.test('should work with empty array', function() { let flaska = new Flaska({}, faker) flaska.handleMiddleware({}, [], 0) }) t.test('should work with correct index', function() { let checkIsTrue = false let flaska = new Flaska({}, faker) flaska.handleMiddleware({}, [ function() { throw new Error('should not be thrown') }, function() { throw new Error('should not be thrown') }, function() { throw new Error('should not be thrown') }, function() { checkIsTrue = true }, ], 3) assert.strictEqual(checkIsTrue, true) }) t.test('should work with static functions', function() { const assertCtx = createCtx({ a: 1 }) let checkCounter = 0 let flaska = new Flaska({}, faker) flaska.handleMiddleware(assertCtx, [ function(ctx) { assert.strictEqual(ctx, assertCtx); checkCounter++ }, function(ctx) { assert.strictEqual(ctx, assertCtx); checkCounter++ }, function(ctx) { assert.strictEqual(ctx, assertCtx); checkCounter++ }, function(ctx) { assert.strictEqual(ctx, assertCtx); checkCounter++ }, function(ctx) { assert.strictEqual(ctx, assertCtx); checkCounter++ }, ], 0) assert.strictEqual(checkCounter, 5) }) t.test('should work with random promises inbetween', function() { const assertCtx = createCtx({ a: 1 }) let checkCounter = 0 let flaska = new Flaska({}, faker) let result = flaska.handleMiddleware(assertCtx, [ function(ctx) { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 0); checkCounter++ }, function(ctx) { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 1); checkCounter++ }, function(ctx) { return new Promise(function(res) { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 2); checkCounter++; res() }) }, function(ctx) { return Promise.resolve().then(function() { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 3); checkCounter++ }) }, function(ctx) { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 4); checkCounter++ }, function(ctx) { return Promise.resolve().then(function() { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 5); checkCounter++ }) }, function(ctx) { return Promise.resolve().then(function() { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 6); checkCounter++ }) }, function(ctx) { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 7); checkCounter++ }, function(ctx) { assert.strictEqual(ctx, assertCtx); assert.strictEqual(checkCounter, 8); checkCounter++ }, ], 0) assert.ok(result) assert.strictEqual(typeof(result.then), 'function') return result.then(function() { assert.strictEqual(checkCounter, 9) }) }) t.test('should work with rejected promises inbetween', async function() { const assertError = { a: 1 } let checkCounter = 0 let flaska = new Flaska({}, faker) let err = await assert.isRejected(flaska.handleMiddleware({}, [ function() { }, function() { return new Promise(function(res, rej) { rej(assertError) }) }, function() { throw new Error('should not be seen') }, ], 0)) assert.strictEqual(err, assertError) err = await assert.isRejected(flaska.handleMiddleware({}, [ function() { }, function() { return Promise.reject(assertError) }, function() { throw new Error('should not be seen') }, ], 0)) assert.strictEqual(err, assertError) err = await assert.isRejected(flaska.handleMiddleware({}, [ function() { }, function() { return Promise.resolve() }, function() { throw assertError }, ], 0)) assert.strictEqual(err, assertError) }) }) t.describe('#listen()', function() { t.test('it should throw if missing port', function() { let flaska = new Flaska({}, faker) assert.throws(function() { flaska.listen() }, /[Pp]ort/) assert.throws(function() { flaska.listen('asdf') }, /[Pp]ort/) assert.throws(function() { flaska.listen('123') }, /[Pp]ort/) assert.throws(function() { flaska.listen([]) }, /[Pp]ort/) assert.throws(function() { flaska.listen({}) }, /[Pp]ort/) assert.throws(function() { flaska.listen(function() {}) }, /[Pp]ort/) }) t.test('it should automatically call compile', function() { let assertCalled = false let flaska = new Flaska({}, faker) flaska.compile = function() { assertCalled = true } flaska.listen(404) assert.strictEqual(assertCalled, true) }) t.test('call http correctly', function() { const assertPort = 325897235 const assertIp = 'asdga' const assertCb = function() { } let checkPort = null let checkIp = null let checkListenCb = null let testFaker = fakeHttp(null, function(port, ip, cb) { checkPort = port checkIp = ip checkListenCb = cb }) let flaska = new Flaska({}, testFaker) assert.ok(flaska.requestStart) flaska.requestStart = function() { checkInternalThis = this checkIsTrue = true } flaska.listen(assertPort, assertIp, assertCb) assert.strictEqual(checkPort, assertPort) assert.strictEqual(checkIp, assertIp) assert.strictEqual(checkListenCb, assertCb) }) t.test('call http correctly if only port is specified', function() { const assertPort = 325897235 const assertCb = function() { } let checkPort = null let checkIp = null let checkListenCb = null let testFaker = fakeHttp(null, function(port, ip, cb) { checkPort = port checkIp = ip checkListenCb = cb }) let flaska = new Flaska({}, testFaker) assert.ok(flaska.requestStart) flaska.requestStart = function() { checkInternalThis = this checkIsTrue = true } flaska.listen(assertPort, assertCb) assert.strictEqual(checkPort, assertPort) assert.strictEqual(checkIp, '::') assert.strictEqual(checkListenCb, assertCb) }) t.test('register requestStart if no async', function() { let checkIsTrue = false let checkInternalThis = null let checkHandler = null let testFaker = fakeHttp(function(cb) { checkHandler = cb }) let flaska = new Flaska({}, testFaker) assert.ok(flaska.requestStart) flaska.requestStart = function() { checkInternalThis = this checkIsTrue = true } flaska.listen(404) assert.strictEqual(typeof(checkHandler), 'function') assert.notStrictEqual(checkHandler, flaska.requestStart) assert.notStrictEqual(checkIsTrue, true) assert.notStrictEqual(checkInternalThis, flaska) checkHandler() assert.strictEqual(checkIsTrue, true) assert.strictEqual(checkInternalThis, flaska) }) }) t.describe('#listenAsync()', function() { t.test('it should throw if missing port', async function() { let flaska = new Flaska({}, faker) let tests = [ undefined, 'asdf', '123', [], {}, function() {}, ] let errors = await Promise.all(tests.map(x => assert.isRejected(flaska.listenAsync(x)))) assert.strictEqual(errors.length, tests.length) for (let i = 0; i < errors.length; i++) { assert.match(errors[i].message, /[Pp]ort/) } }) t.test('it should automatically call compile', async function() { let assertCalled = false let flaska = new Flaska({}, faker) flaska.compile = function() { assertCalled = true } await flaska.listenAsync(404) assert.strictEqual(assertCalled, true) }) t.test('call http correctly', async function() { const assertPort = 325897235 const assertIp = 'asdf' let checkPort = null let checkIp = null let testFaker = fakeHttp(null, function(port, ip, cb) { checkPort = port checkIp = ip cb() }) let flaska = new Flaska({}, testFaker) assert.ok(flaska.requestStart) flaska.requestStart = function() { checkInternalThis = this checkIsTrue = true } await flaska.listenAsync(assertPort, assertIp) assert.strictEqual(checkPort, assertPort) assert.strictEqual(checkIp, assertIp) }) t.test('call http correctly if only port specified', async function() { const assertPort = 325897235 let checkPort = null let checkIp = null let testFaker = fakeHttp(null, function(port, ip, cb) { checkPort = port checkIp = ip cb() }) let flaska = new Flaska({}, testFaker) assert.ok(flaska.requestStart) flaska.requestStart = function() { checkInternalThis = this checkIsTrue = true } await flaska.listenAsync(assertPort) assert.strictEqual(checkPort, assertPort) assert.strictEqual(checkIp, '::') }) t.test('register requestStart if no async', async function() { let checkIsTrue = false let checkInternalThis = null let checkHandler = null let testFaker = fakeHttp(function(cb) { checkHandler = cb }) let flaska = new Flaska({}, testFaker) assert.ok(flaska.requestStart) flaska.requestStart = function() { checkInternalThis = this checkIsTrue = true } await flaska.listenAsync(404) assert.strictEqual(typeof(checkHandler), 'function') assert.notStrictEqual(checkHandler, flaska.requestStart) assert.notStrictEqual(checkIsTrue, true) assert.notStrictEqual(checkInternalThis, flaska) checkHandler() assert.strictEqual(checkIsTrue, true) assert.strictEqual(checkInternalThis, flaska) }) }) t.describe('#closeAsync()', function() { t.test('it return if server is null', function() { let flaska = new Flaska() flaska.server = null return flaska.closeAsync() }) t.test('it should call close on server correctly', async function() { const assertError = new Error('Pirate Fight') let flaska = new Flaska() flaska.server = { close: stub() } flaska.server.close.returnWith(function(cb) { cb(assertError) }) let err = await assert.isRejected(flaska.closeAsync()) assert.strictEqual(err, assertError) }) t.test('should otherwise work', async function() { let flaska = new Flaska() flaska.server = { close: stub() } flaska.server.close.returnWith(function(cb) { cb(null, { a: 1 }) }) let res = await flaska.closeAsync() assert.notOk(res) }) })