Open History to see the last commit
1import http from 'node:http'
2import { posts } from './posts'
3import { users } from './users'
4β
5// some tests redirect to an external host
6// however, in CI this is unstable due to network conditions
7// so here we spawn a local server to simulate the external host
8β
9export async function localDummyServer(port: number) {
10 const server = http.createServer((req, res) => {
11 // Set CORS headers
12 res.setHeader('Access-Control-Allow-Origin', '*')
13 res.setHeader('Access-Control-Request-Method', '*')
14 res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET, POST')
15 res.setHeader('Access-Control-Allow-Headers', '*')
16β
17 if (req.method === 'OPTIONS') {
18 res.writeHead(200)
19 res.end()
20 return
21 }
22 })
23β
24 server.on('request', (req, res) => {
25 const url = new URL(req.url!, 'http://localhost')
26β
27 if (res.req.method === 'GET' && url.pathname === '/') {
28 res.writeHead(200, { 'Content-Type': 'text/html' })
29 res.end('Hello World')
30 }
31 })
32β
33 server.on('request', (req, res) => {
34 const url = new URL(req.url!, `http://localhost`)
35β
36 if (res.req.method === 'POST' && url.pathname === '/stop') {
37 server.close()
38 console.info(
39 `stopped local dummy server running at http://localhost:${port}`,
40 )
41β
42 res.writeHead(200)
43 res.end()
44 }
45 })
46β
47 server.on('request', (req, res) => {
48 const url = new URL(req.url ?? '/', `http://localhost`)
49β
50 if (req.method === 'GET' && url.pathname.startsWith('/posts')) {
51 const parts = url.pathname.split('/')
52β
53 if (parts.length === 2) {
54 res.writeHead(200, { 'content-type': 'application/json' })
55 res.end(JSON.stringify(posts))
56 return
57 }
58β
59 if (parts[2]) {
60 const postId = parseInt(parts[2])
61β
62 if (isNaN(postId)) {
63 res.writeHead(404)
64 res.end(JSON.stringify({ error: 'invalid post id' }))
65 return
66 }
67β
68 const post = posts.find((post) => post.id === postId)
69β
70 res.writeHead(200, { 'content-type': 'application/json' })
71 res.end(JSON.stringify(post))
72 return
73 }
74β
75 res.writeHead(404)
76 res.end(JSON.stringify({ error: 'invalid posts path' }))
77 return
78 }
79 })
80β
81 server.on('request', (req, res) => {
82 const url = new URL(req.url ?? '/', `http://localhost`)
83β
84 if (req.method === 'GET' && url.pathname.startsWith('/users')) {
85 const parts = url.pathname.split('/')
86β
87 if (parts.length === 2) {
88 res.writeHead(200, { 'content-type': 'application/json' })
89 res.end(JSON.stringify(users))
90 return
91 }
92β
93 if (parts[2]) {
94 const userId = parseInt(parts[2])
95β
96 if (isNaN(userId)) {
97 res.writeHead(404)
98 res.end(JSON.stringify({ error: 'invalid user id' }))
99 return
100 }
101β
102 const user = users.find((user) => user.id === userId)
103β
104 res.writeHead(200, { 'content-type': 'application/json' })
105 res.end(JSON.stringify(user))
106 return
107 }
108β
109 res.writeHead(404)
110 res.end(JSON.stringify({ error: 'invalid users path' }))
111 return
112 }
113 })
114β
115 const startServer = new Promise<void>((resolve) => {
116 server.listen(port, 'localhost', () => {
117 console.info(
118 `started local dummy server running at http://localhost:${port}`,
119 )
120 resolve()
121 })
122 })
123β
124 await startServer
125β
126 await new Promise((r) => setTimeout(r, 500))
127 return server
128}