Skip to content

Commit 854a478

Browse files
committed
test: improve coverage
1 parent c46b254 commit 854a478

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

src/server/main.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export class Server<NamedMiddleware extends Record<string, LazyImport<Middleware
209209
/**
210210
* Set the HTTP server instance used to listen for requests.
211211
*/
212-
setServer(server: HttpServer | HttpsServer) {
212+
setNodeServer(server: HttpServer | HttpsServer) {
213213
this.#underlyingHttpServer = server
214214
}
215215

@@ -218,7 +218,13 @@ export class Server<NamedMiddleware extends Record<string, LazyImport<Middleware
218218
*/
219219
close() {
220220
return new Promise<void>((resolve, reject) => {
221-
this.#underlyingHttpServer?.close((error) => {
221+
if (!this.#underlyingHttpServer || !this.#underlyingHttpServer.listening) {
222+
resolve()
223+
return
224+
}
225+
226+
this.#underlyingHttpServer.close((error) => {
227+
/* c8 ignore next 3 */
222228
if (error) {
223229
return reject(error)
224230
}
@@ -232,7 +238,7 @@ export class Server<NamedMiddleware extends Record<string, LazyImport<Middleware
232238
* Returns reference to the underlying HTTP server
233239
* in use
234240
*/
235-
getServer() {
241+
getNodeServer() {
236242
return this.#underlyingHttpServer
237243
}
238244

test_factories/server_factory.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ import { Server } from '../src/server/main.js'
1616
import { EncryptionFactory } from './encryption.js'
1717

1818
import type { LazyImport } from '../src/types/base.js'
19-
import type { RequestConfig } from '../src/types/request.js'
20-
import type { ResponseConfig } from '../src/types/response.js'
19+
import type { ServerConfig } from '../src/types/server.js'
2120
import type { MiddlewareAsClass } from '../src/types/middleware.js'
2221

2322
type FactoryParameters = {
2423
app: Application
2524
encryption: Encryption
26-
config: Partial<RequestConfig & ResponseConfig>
25+
config: Partial<ServerConfig>
2726
}
2827

2928
/**

tests/server.spec.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,74 @@ import 'reflect-metadata'
1111
import supertest from 'supertest'
1212
import { test } from '@japa/runner'
1313
import { createServer } from 'node:http'
14+
import type { NextFn } from '@poppinss/middleware/types'
15+
16+
import { Router } from '../src/router/main.js'
1417
import { AppFactory } from '../test_factories/app.js'
1518
import { HttpContext } from '../src/http_context/main.js'
16-
import type { NextFn } from '@poppinss/middleware/types'
1719
import { ServerFactory } from '../test_factories/server_factory.js'
1820

21+
test.group('Server', () => {
22+
test('fail when booting without defining middleware', ({ assert }) => {
23+
const app = new AppFactory().create()
24+
const server = new ServerFactory().merge({ app }).create()
25+
26+
assert.rejects(
27+
() => server.boot(),
28+
'Cannot boot HTTP server. Register middleware using "server.use" first'
29+
)
30+
})
31+
32+
test('get router instance used by the server', ({ assert }) => {
33+
const app = new AppFactory().create()
34+
const server = new ServerFactory().merge({ app }).create()
35+
server.use([], [], {})
36+
37+
assert.instanceOf(server.getRouter(), Router)
38+
})
39+
40+
test('store http server instance', ({ assert }) => {
41+
const app = new AppFactory().create()
42+
const server = new ServerFactory().merge({ app }).create()
43+
server.use([], [], {})
44+
45+
const httpServer = createServer(() => {})
46+
server.setNodeServer(httpServer)
47+
48+
assert.strictEqual(server.getNodeServer(), httpServer)
49+
})
50+
51+
test('close node server ', async ({ assert }) => {
52+
assert.plan(1)
53+
54+
const app = new AppFactory().create()
55+
const server = new ServerFactory().merge({ app }).create()
56+
server.use([], [], {})
57+
58+
const httpServer = createServer(() => {}).listen(3000)
59+
server.setNodeServer(httpServer)
60+
61+
httpServer.on('close', () => {
62+
assert.isFalse(httpServer.listening)
63+
})
64+
65+
await server.close()
66+
})
67+
68+
test('noop when http server is not listening or not set', async ({ assert }) => {
69+
const app = new AppFactory().create()
70+
const server = new ServerFactory().merge({ app }).create()
71+
server.use([], [], {})
72+
73+
assert.doesNotRejects(() => server.close())
74+
75+
const httpServer = createServer(() => {})
76+
server.setNodeServer(httpServer)
77+
78+
assert.doesNotRejects(() => server.close())
79+
})
80+
})
81+
1982
test.group('Server | Response handling', () => {
2083
test('invoke router handler', async ({ assert }) => {
2184
const app = new AppFactory().create()

0 commit comments

Comments
 (0)