Skip to content

Commit

Permalink
Bumped v5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Feb 26, 2021
1 parent 75716f2 commit 5b490b1
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-http-proxy",
"version": "4.4.0",
"version": "5.0.0",
"description": "proxy http requests, for Fastify",
"main": "index.js",
"scripts": {
Expand Down
146 changes: 146 additions & 0 deletions test/ws-prefix-rewrite-core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict'

const t = require('tap')
const { once } = require('events')

const Fastify = require('fastify')
const fastifyWebSocket = require('fastify-websocket')
const proxy = require('..')
const WebSocket = require('ws')
const got = require('got')

const level = 'warn'

async function proxyServer (t, backendURL, backendPath, proxyOptions, wrapperOptions) {
const frontend = Fastify({ logger: { level } })
const registerProxy = async fastify => {
fastify.register(proxy, {
upstream: backendURL + backendPath,
http: true,
...proxyOptions
})
}

t.comment('starting proxy to ' + backendURL + backendPath)

if (wrapperOptions) {
await frontend.register(registerProxy, wrapperOptions)
} else {
await registerProxy(frontend)
}

return [frontend, await frontend.listen(0)]
}

async function processRequest (t, frontendURL, path, expected) {
const url = new URL(path, frontendURL)
t.comment('ws connecting to ' + url.toString())
const ws = new WebSocket(url)
let wsResult, gotResult

try {
await once(ws, 'open')
t.pass('socket connected')

const [buf] = await Promise.race([once(ws, 'message'), once(ws, 'close')])
if (buf instanceof Buffer) {
wsResult = buf.toString()
} else {
t.comment('websocket closed')
wsResult = 'error'
}
} catch (e) {
wsResult = 'error'
ws.terminate()
}

try {
const result = await got(url)
gotResult = result.body
} catch (e) {
gotResult = 'error'
}

t.is(wsResult, expected)
t.is(gotResult, expected)
}

async function handleProxy (info, { backendPath, proxyOptions, wrapperOptions }, expected, ...paths) {
t.test(info, async function (t) {
const backend = Fastify({ logger: { level } })
await backend.register(fastifyWebSocket)

backend.get('/*', {
handler: (req, reply) => {
reply.send(req.url)
},
wsHandler: (conn, req) => {
conn.write(req.url)
conn.end()
}
})

t.teardown(() => backend.close())

const backendURL = await backend.listen(0)

const [frontend, frontendURL] = await proxyServer(t, backendURL, backendPath, proxyOptions, wrapperOptions)

t.teardown(() => frontend.close())

for (const path of paths) {
await processRequest(t, frontendURL, path, expected(path))
}

t.end()
})
}

handleProxy(
'no prefix to `/`',
{
backendPath: '/',
proxyOptions: { websocket: true }
},
path => path,
'/',
'/pub',
'/pub/'
)

handleProxy(
'`/pub/` to `/`',
{
backendPath: '/',
proxyOptions: { websocket: true, prefix: '/pub/' }
},
path => path.startsWith('/pub/') ? path.replace('/pub/', '/') : 'error',
'/',
'/pub/',
'/pub/test'
)

handleProxy(
'`/pub/` to `/public/`',
{
backendPath: '/public/',
proxyOptions: { websocket: true, prefix: '/pub/' }
},
path => path.startsWith('/pub/') ? path.replace('/pub/', '/public/') : 'error',
'/',
'/pub/',
'/pub/test'
)

handleProxy(
'wrapped `/pub/` to `/public/`',
{
backendPath: '/public/',
proxyOptions: { websocket: true },
wrapperOptions: { prefix: '/pub/' }
},
path => path.startsWith('/pub/') ? path.replace('/pub/', '/public/') : 'error',
'/',
'/pub/',
'/pub/test'
)

0 comments on commit 5b490b1

Please sign in to comment.