forked from adonisjs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server_process.spec.ts
More file actions
179 lines (147 loc) · 4.64 KB
/
http_server_process.spec.ts
File metadata and controls
179 lines (147 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* @adonisjs/core
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import getPort from 'get-port'
import supertest from 'supertest'
import { test } from '@japa/runner'
import { createServer } from 'node:http'
import type { ApplicationService } from '../../src/types.js'
import { IgnitorFactory } from '../../factories/core/ignitor.js'
const BASE_URL = new URL('./tmp/', import.meta.url)
test.group('Ignitor | Http server process', () => {
test('start http server using the http server process', async ({ assert, cleanup }) => {
cleanup(async () => {
delete process.env.HOST
delete process.env.PORT
await ignitor.terminate()
})
process.env.HOST = 'localhost'
process.env.PORT = String(await getPort())
const serverURL = `http://${process.env.HOST}:${process.env.PORT}`
const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [() => import('../../providers/app_provider.js')],
},
})
.withCoreConfig()
.preload(async (app) => {
const router = await app.container.make('router')
router.get('/', () => {
return 'hello world'
})
})
.create(BASE_URL)
await ignitor.httpServer().start()
const { text } = await supertest(serverURL).get('/')
assert.equal(text, 'hello world')
})
test('use port 3333 when PORT env variable is not defined', async ({ assert, cleanup }) => {
cleanup(async () => {
await ignitor.terminate()
})
const serverURL = 'http://127.0.0.1:3333'
const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [() => import('../../providers/app_provider.js')],
},
})
.withCoreConfig()
.preload(async (app) => {
const router = await app.container.make('router')
router.get('/', () => {
return 'hello world'
})
})
.create(BASE_URL)
await ignitor.httpServer().start()
const { text } = await supertest(serverURL).get('/')
assert.equal(text, 'hello world')
})
test('shutdown server when app terminates', async ({ assert, cleanup }) => {
let app: ApplicationService
cleanup(async () => {
delete process.env.HOST
delete process.env.PORT
})
process.env.HOST = 'localhost'
process.env.PORT = String(await getPort())
const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [() => import('../../providers/app_provider.js')],
},
})
.withCoreConfig()
.create(BASE_URL)
ignitor.tap((application) => {
app = application
})
await ignitor.httpServer().start()
const server = await app!.container.make('server')
assert.isTrue(server.getNodeServer()!.listening)
await ignitor.terminate()
assert.isFalse(server.getNodeServer()!.listening)
})
test('throw error if port is already in use', async ({ assert, cleanup }) => {
const nodeServer = createServer(() => {})
cleanup(async () => {
delete process.env.HOST
delete process.env.PORT
nodeServer.close()
})
process.env.HOST = 'localhost'
process.env.PORT = String(await getPort())
const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [() => import('../../providers/app_provider.js')],
},
})
.withCoreConfig()
.create(BASE_URL)
await new Promise<void>((resolve) => {
nodeServer.listen(Number(process.env.PORT), process.env.HOST, () => {
resolve()
})
})
await assert.rejects(
() => ignitor.httpServer().start(),
/listen EADDRINUSE: address already in use/
)
})
test('terminate app if server crashes after starting', async ({ cleanup, assert }, done) => {
let app: ApplicationService
cleanup(async () => {
process.exitCode = undefined
delete process.env.HOST
delete process.env.PORT
})
process.env.HOST = 'localhost'
process.env.PORT = String(await getPort())
const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [() => import('../../providers/app_provider.js')],
},
})
.withCoreConfig()
.create(BASE_URL)
ignitor.tap((application) => {
app = application
})
await ignitor.httpServer().start()
app!.terminating(() => {
done()
})
const server = await app!.container.make('server')
server.getNodeServer()!.emit('error', new Error('crash'))
assert.equal(process.exitCode, 1)
}).waitForDone()
})