-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.test.mjs
146 lines (120 loc) · 3.53 KB
/
http.test.mjs
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
import tap from 'tap'
import { spawn } from 'node:child_process'
import { withConfig } from './server.mjs'
import { server } from './http.mjs'
const { test } = tap
let serverProcess
const port = Number(process.env.PORT || 3000) + 1
const apiUri = `http://localhost:${port}`
tap.before(async () => {
// Start the server in a child process
serverProcess = spawn('node', ['./cli.mjs', 'server', './example/api.mjs', '--port', port], {
env: {
...process.env,
DEBUG: '*'
}
})
// Wait for the server to start listening on the port
await new Promise((resolve) => serverProcess.stderr.once('data', resolve))
// Listen for the serverProcess exit event
serverProcess.once('exit', (code) => {
code = code || 0
if (code !== 0) {
console.error(`Server process exited with code ${code}`)
process.exit(code)
}
})
})
tap.teardown(() => serverProcess.kill())
test('Returns 400 when receiving malformed JSON data', async (t) => {
// Send a POST request with malformed JSON data
const res = await fetch(apiUri, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '{invalid-json}'
})
// Verify the response status code
t.equal(res.status, 400)
t.end()
})
test('Returns 404 status', async (t) => {
try {
const res = await fetch(apiUri, {
method: 'POST',
body: JSON.stringify([]),
headers: { 'Content-Type': 'application/json' }
})
t.equal(res.status, 404, 'Status should be 404')
t.end()
} catch (error) {
t.error(error)
}
})
test('Test 200 status', async (t) => {
const requestData = ['array', ['add', 1, 2]]
const expectedResponse = [3]
// Send a POST request with valid JSON data
const res = await fetch(apiUri, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestData)
})
// Verify the response status code
t.equal(res.status, 200)
// Verify the response body
const responseBody = await res.json()
t.same(responseBody, expectedResponse)
t.end()
})
test('withConfig helper', async (t) => {
t.plan(6)
const tPort = port + 1
const apiUri = `http://localhost:${tPort}`
const api = {
add: (a, b) => a + b
}
const config = {
onRequest (request) {
t.pass('Called onRequest')
},
onError (request, response, error) {
t.pass('Called onError')
response.writeHead(500)
response.end()
},
onResponse (request, response, result) {
t.pass('Called onResponse')
response.writeHead(200, { 'Content-Type': 'application/json' })
response.write(JSON.stringify(result))
response.end()
}
}
// Create a new API with the custom configuration
const apiWithConfig = withConfig(config, api)
// Start the server with the new API
const instance = await server(apiWithConfig, tPort)
// Send a request to the server
const requestData = ['array', ['add', 1, 2]]
const expectedResponse = [3]
await fetch(apiUri, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: 'not a valid JSON string'
})
await fetch(apiUri, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: '["DoesNotExist"]'
})
const res = await fetch(apiUri, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestData)
})
// Verify the response status code and body
t.equal(res.status, 200)
const responseBody = await res.json()
t.same(responseBody, expectedResponse)
// Stop the server
instance.close()
})