Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

localDummyServer.ts

128 lines3.4 KB

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