mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
126 lines
3.4 KiB
JavaScript
126 lines
3.4 KiB
JavaScript
|
|
var http = require('http').Server;
|
|
var io = require('..');
|
|
var ioc = require('socket.io-client');
|
|
var request = require('supertest');
|
|
var expect = require('expect.js');
|
|
|
|
// creates a socket.io client for the given server
|
|
function client(srv){
|
|
var addr = srv.address();
|
|
if (!addr) addr = srv.listen().address();
|
|
return ioc('ws://' + addr.address + ':' + addr.port);
|
|
}
|
|
|
|
describe('socket.io', function(){
|
|
describe('server attachment', function(){
|
|
describe('http.Server', function(){
|
|
it('should serve static files', function(done){
|
|
var srv = http();
|
|
io(srv);
|
|
request(srv)
|
|
.get('/socket.io/socket.io.js')
|
|
.buffer(true)
|
|
.end(function(err, res){
|
|
if (err) return done(err);
|
|
var ctype = res.headers['content-type'];
|
|
expect(ctype).to.be('application/javascript');
|
|
expect(res.text).to.match(/engine\.io/);
|
|
expect(res.status).to.be(200);
|
|
done();
|
|
});
|
|
});
|
|
|
|
it('should not serve static files', function(done){
|
|
var srv = http();
|
|
io(srv, { static: false });
|
|
request(srv)
|
|
.get('/socket.io/socket.io.js')
|
|
.expect(400, done);
|
|
});
|
|
|
|
it('should work with #attach', function(done){
|
|
var srv = http(function(req, res){
|
|
res.writeHead(404);
|
|
res.end();
|
|
});
|
|
var sockets = io();
|
|
sockets.attach(srv);
|
|
request(srv)
|
|
.get('/socket.io/socket.io.js')
|
|
.end(function(err, res){
|
|
if (err) return done(err);
|
|
expect(res.status).to.be(200);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('port', function(done){
|
|
it('should be bound', function(done){
|
|
var sockets = io(54010);
|
|
request('http://localhost:54010')
|
|
.get('/socket.io/socket.io.js')
|
|
.expect(200, done);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('namespaces', function(){
|
|
var Socket = require('../lib/socket');
|
|
var Namespace = require('../lib/namespace');
|
|
|
|
describe('default', function(){
|
|
it('should be accessible through .sockets', function(){
|
|
var sio = io();
|
|
expect(sio.sockets).to.be.a(Namespace);
|
|
});
|
|
|
|
it('should be aliased', function(){
|
|
var sio = io();
|
|
expect(sio.use).to.be.a('function');
|
|
expect(sio.to).to.be.a('function');
|
|
expect(sio.in).to.be.a('function');
|
|
expect(sio.emit).to.be.a('function');
|
|
expect(sio.send).to.be.a('function');
|
|
expect(sio.write).to.be.a('function');
|
|
});
|
|
|
|
it('should automatically connect', function(done){
|
|
var srv = http();
|
|
var sio = io(srv);
|
|
srv.listen(function(){
|
|
var socket = client(srv);
|
|
socket.on('connect', function(){
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should fire a `connection` event', function(done){
|
|
var srv = http();
|
|
var sio = io(srv);
|
|
srv.listen(function(){
|
|
var socket = client(srv);
|
|
sio.on('connection', function(socket){
|
|
expect(socket).to.be.a(Socket);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
|
|
it('should fire a `connect` event', function(done){
|
|
var srv = http();
|
|
var sio = io(srv);
|
|
srv.listen(function(){
|
|
var socket = client(srv);
|
|
sio.on('connect', function(socket){
|
|
expect(socket).to.be.a(Socket);
|
|
done();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|