2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00
socketio-jwt/test/authorizer_noqs.test.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-12-27 17:25:44 +01:00
const fixture = require('./fixture')
const request = require('request')
const io = require('socket.io-client')
2014-01-13 22:41:10 +01:00
describe('authorizer without querystring', () => {
2014-01-13 22:41:10 +01:00
//start and stop the server
before((done) => {
2020-12-27 17:25:44 +01:00
fixture.start({ handshake: false }, done)
})
2014-01-13 22:41:10 +01:00
2020-12-27 17:25:44 +01:00
after(fixture.stop)
2014-01-13 22:41:10 +01:00
describe('when the user is not logged in', () => {
it('should close the connection after a timeout if no auth message is received', (done) => {
2020-12-27 17:25:44 +01:00
io.connect('http://localhost:9000', { forceNew: true }).once(
'disconnect',
() => done()
)
})
2014-01-13 22:41:10 +01:00
it('should not respond echo', (done) => {
io.connect('http://localhost:9000', { forceNew: true })
.on('echo-response', () => done(new Error('this should not happen')))
2020-12-27 17:25:44 +01:00
.emit('echo', { hi: 123 })
2014-01-13 22:41:10 +01:00
2020-12-27 17:25:44 +01:00
setTimeout(done, 1200)
})
})
2014-01-13 22:41:10 +01:00
describe('when the user is logged in', () => {
beforeEach((done) => {
2020-12-27 17:25:44 +01:00
request.post(
{
url: 'http://localhost:9000/login',
form: { username: 'jose', password: 'Pa123' },
json: true
},
(err, resp, body) => {
this.token = body.token
done()
}
)
})
2014-01-13 22:41:10 +01:00
it('should do the authentication and connect', (done) => {
2020-12-27 17:25:44 +01:00
const socket = io.connect('http://localhost:9000', { forceNew: true })
socket
.on('echo-response', () => {
2020-12-27 17:25:44 +01:00
socket.close()
done()
})
2020-12-27 17:25:44 +01:00
.on('authenticated', () => {
socket.emit('echo')
})
.emit('authenticate', { token: this.token })
})
})
})