Skip to content
This repository was archived by the owner on Sep 27, 2021. It is now read-only.

Commit

Permalink
fix(server): allow trailing slashes in request url
Browse files Browse the repository at this point in the history
fix #57
  • Loading branch information
thetutlage committed Jul 17, 2018
1 parent 595cc60 commit fb61e56
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Ws/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

const WebSocket = require('ws')
const url = require('url')
const GE = require('@adonisjs/generic-exceptions')
const Connection = require('../Connection')
const ClusterHop = require('../ClusterHop')
Expand Down Expand Up @@ -240,6 +241,13 @@ class Ws {
listen (server) {
this._wsServer = new WebSocket.Server(Object.assign({}, this._serverOptions, { server }))

/**
* Override the shouldHandle method to allow trailing slashes
*/
this._wsServer.shouldHandle = function (req) {
return this.options.path && url.parse(req.url).pathname.replace(/\/$/, '') === this.options.path
}

/**
* Listening for new connections
*/
Expand Down
9 changes: 8 additions & 1 deletion test/functional/ws.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ test.group('Ws', (group) => {
const client2 = helpers.startClient({}, '/adonis-ws')
client2.on('open', () => joinChannel(client2))
client2.on('message', onMessage)
})
}).timeout(0)

test('broadcast messages to all clients', (assert, done) => {
let connectedClients = []
Expand Down Expand Up @@ -449,4 +449,11 @@ test.group('Ws', (group) => {
client2.on('open', () => joinChannel(client2, 'chat:frontend'))
client2.on('message', onMessage)
})

test('work fine with slash in the end', (assert, done) => {
const client = helpers.startClient({}, '/adonis-ws/')
client.on('open', () => {
done()
})
})
})
9 changes: 9 additions & 0 deletions test/unit/ws.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,13 @@ test.group('Ws', (group) => {
}
})
})

test('work fine with slash in the end', (assert, done) => {
this.ws = new Ws(new Config())
this.httpServer = helpers.startHttpServer()
this.ws.listen(this.httpServer)

const client = helpers.startClient({}, '/adonis-ws/')
client.on('open', () => done())
})
})

0 comments on commit fb61e56

Please sign in to comment.