Skip to content

Commit

Permalink
Make sure query params are included in WebSocket request
Browse files Browse the repository at this point in the history
  • Loading branch information
valeneiko committed Jan 22, 2024
1 parent 01f48d8 commit 91dfcbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,25 @@ class WebSocketProxy {
}

findUpstream (request, dest) {
const search = new URL(request.url, 'ws://127.0.0.1').search

if (typeof this.wsUpstream === 'string' && this.wsUpstream !== '') {
const target = new URL(this.wsUpstream)
target.search = new URL(request.url, 'ws://127.0.0.1').search
target.search = search
return target
}

if (typeof this.upstream === 'string' && this.upstream !== '') {
return new URL(dest, this.upstream)
const target = new URL(dest, this.upstream)
target.search = search
return target
}

const upstream = this.getUpstream(request, '')
const target = new URL(dest, upstream)
/* istanbul ignore next */
target.protocol = upstream.indexOf('http:') === 0 ? 'ws:' : 'wss'
target.search = search
return target
}

Expand Down
5 changes: 3 additions & 2 deletions test/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ test('multiple websocket upstreams with host constraints', async (t) => {
})

test('multiple websocket upstreams with distinct server options', async (t) => {
t.plan(2)
t.plan(4)

const server = Fastify()

Expand All @@ -532,7 +532,8 @@ test('multiple websocket upstreams with distinct server options', async (t) => {
t.teardown(wss.close.bind(wss))
t.teardown(origin.close.bind(origin))

wss.once('connection', (ws) => {
wss.once('connection', (ws, req) => {
t.equal(req.url, `/?q=${name}`)
ws.once('message', message => {
// echo
ws.send(message)
Expand Down

0 comments on commit 91dfcbc

Please sign in to comment.