Skip to content

Commit 5b490b1

Browse files
committed
Bumped v5.0.0
1 parent 75716f2 commit 5b490b1

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fastify-http-proxy",
3-
"version": "4.4.0",
3+
"version": "5.0.0",
44
"description": "proxy http requests, for Fastify",
55
"main": "index.js",
66
"scripts": {

test/ws-prefix-rewrite-core.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const { once } = require('events')
5+
6+
const Fastify = require('fastify')
7+
const fastifyWebSocket = require('fastify-websocket')
8+
const proxy = require('..')
9+
const WebSocket = require('ws')
10+
const got = require('got')
11+
12+
const level = 'warn'
13+
14+
async function proxyServer (t, backendURL, backendPath, proxyOptions, wrapperOptions) {
15+
const frontend = Fastify({ logger: { level } })
16+
const registerProxy = async fastify => {
17+
fastify.register(proxy, {
18+
upstream: backendURL + backendPath,
19+
http: true,
20+
...proxyOptions
21+
})
22+
}
23+
24+
t.comment('starting proxy to ' + backendURL + backendPath)
25+
26+
if (wrapperOptions) {
27+
await frontend.register(registerProxy, wrapperOptions)
28+
} else {
29+
await registerProxy(frontend)
30+
}
31+
32+
return [frontend, await frontend.listen(0)]
33+
}
34+
35+
async function processRequest (t, frontendURL, path, expected) {
36+
const url = new URL(path, frontendURL)
37+
t.comment('ws connecting to ' + url.toString())
38+
const ws = new WebSocket(url)
39+
let wsResult, gotResult
40+
41+
try {
42+
await once(ws, 'open')
43+
t.pass('socket connected')
44+
45+
const [buf] = await Promise.race([once(ws, 'message'), once(ws, 'close')])
46+
if (buf instanceof Buffer) {
47+
wsResult = buf.toString()
48+
} else {
49+
t.comment('websocket closed')
50+
wsResult = 'error'
51+
}
52+
} catch (e) {
53+
wsResult = 'error'
54+
ws.terminate()
55+
}
56+
57+
try {
58+
const result = await got(url)
59+
gotResult = result.body
60+
} catch (e) {
61+
gotResult = 'error'
62+
}
63+
64+
t.is(wsResult, expected)
65+
t.is(gotResult, expected)
66+
}
67+
68+
async function handleProxy (info, { backendPath, proxyOptions, wrapperOptions }, expected, ...paths) {
69+
t.test(info, async function (t) {
70+
const backend = Fastify({ logger: { level } })
71+
await backend.register(fastifyWebSocket)
72+
73+
backend.get('/*', {
74+
handler: (req, reply) => {
75+
reply.send(req.url)
76+
},
77+
wsHandler: (conn, req) => {
78+
conn.write(req.url)
79+
conn.end()
80+
}
81+
})
82+
83+
t.teardown(() => backend.close())
84+
85+
const backendURL = await backend.listen(0)
86+
87+
const [frontend, frontendURL] = await proxyServer(t, backendURL, backendPath, proxyOptions, wrapperOptions)
88+
89+
t.teardown(() => frontend.close())
90+
91+
for (const path of paths) {
92+
await processRequest(t, frontendURL, path, expected(path))
93+
}
94+
95+
t.end()
96+
})
97+
}
98+
99+
handleProxy(
100+
'no prefix to `/`',
101+
{
102+
backendPath: '/',
103+
proxyOptions: { websocket: true }
104+
},
105+
path => path,
106+
'/',
107+
'/pub',
108+
'/pub/'
109+
)
110+
111+
handleProxy(
112+
'`/pub/` to `/`',
113+
{
114+
backendPath: '/',
115+
proxyOptions: { websocket: true, prefix: '/pub/' }
116+
},
117+
path => path.startsWith('/pub/') ? path.replace('/pub/', '/') : 'error',
118+
'/',
119+
'/pub/',
120+
'/pub/test'
121+
)
122+
123+
handleProxy(
124+
'`/pub/` to `/public/`',
125+
{
126+
backendPath: '/public/',
127+
proxyOptions: { websocket: true, prefix: '/pub/' }
128+
},
129+
path => path.startsWith('/pub/') ? path.replace('/pub/', '/public/') : 'error',
130+
'/',
131+
'/pub/',
132+
'/pub/test'
133+
)
134+
135+
handleProxy(
136+
'wrapped `/pub/` to `/public/`',
137+
{
138+
backendPath: '/public/',
139+
proxyOptions: { websocket: true },
140+
wrapperOptions: { prefix: '/pub/' }
141+
},
142+
path => path.startsWith('/pub/') ? path.replace('/pub/', '/public/') : 'error',
143+
'/',
144+
'/pub/',
145+
'/pub/test'
146+
)

0 commit comments

Comments
 (0)