Skip to content
Browse files

corrected ssl support, added tests and some cleanups

  • Loading branch information...
1 parent 6c51036 commit ebee5c3e2f9ec9a781a6173400f2585690271970 @einaros einaros committed Jan 1, 2012
Showing with 158 additions and 9 deletions.
  1. +2 −1 History.md
  2. +3 −3 lib/WebSocket.js
  3. +114 −5 test/WebSocket.test.js
  4. +13 −0 test/fixtures/certificate.pem
  5. +15 −0 test/fixtures/key.pem
  6. +11 −0 test/fixtures/request.pem
View
3 History.md
@@ -3,10 +3,11 @@ v0.3.9 - Jan 1st 2012
* Improved protocol framing performance [einaros]
* WSS support [kazuyukitanimura]
+* WSS tests [einaros]
* readyState exposed [justinlatimer, tricknotes]
* url property exposed [justinlatimer]
* Removed old 'state' property [einaros]
-* Test cleanup [einaros]
+* Test cleanups [einaros]
v0.3.8 - Dec 27th 2011
======================
View
6 lib/WebSocket.js
@@ -70,7 +70,7 @@ function WebSocket(address, options) {
var serverUrl = url.parse(address);
if (!serverUrl.host) throw new Error('invalid url');
- http = (serverUrl.protocol === 'wss:' || serverUrl.protocol === 'https:') ? https : http;
+ var httpObj = (serverUrl.protocol === 'wss:' || serverUrl.protocol === 'https:') ? https : http;
Object.defineProperty(this, 'url', {
writable: false,
@@ -98,7 +98,7 @@ function WebSocket(address, options) {
var agent;
if (/^v0\.4/.test(process.version)) {
isNodeV4 = true;
- agent = new http.Agent({
+ agent = new httpObj.Agent({
host: serverUrl.hostname,
port: serverUrl.port || 80
});
@@ -127,7 +127,7 @@ function WebSocket(address, options) {
else requestOptions.headers['Origin'] = options.value.origin;
}
- var req = http.request(requestOptions);
+ var req = httpObj.request(requestOptions);
var self = this;
(isNodeV4 ? agent : req).on('error', function(error) {
self.emit('error', error);
View
119 test/WebSocket.test.js
@@ -1,7 +1,10 @@
var assert = require('assert')
+ , https = require('https')
, WebSocket = require('../')
+ , WebSocketServer = require('../').Server
, fs = require('fs')
- , server = require('./testserver');
+ , server = require('./testserver')
+ , crypto = require('crypto');
var port = 20000;
@@ -33,7 +36,7 @@ describe('WebSocket', function() {
}
})
})
-
+
describe('properties', function() {
it('#url exposes the server url', function(done) {
server.createServer(++port, function(srv) {
@@ -60,7 +63,7 @@ describe('WebSocket', function() {
});
});
});
-
+
it('set to connected once connection is established', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
@@ -71,7 +74,7 @@ describe('WebSocket', function() {
});
});
});
-
+
it('set to closed once connection is closed', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
@@ -83,7 +86,7 @@ describe('WebSocket', function() {
});
});
});
-
+
it('set to closed once connection is terminated', function(done) {
server.createServer(++port, function(srv) {
var ws = new WebSocket('ws://localhost:' + port);
@@ -103,6 +106,7 @@ describe('WebSocket', function() {
CLOSING: 2,
CLOSED: 3
};
+
Object.keys(readyStates).forEach(function(state) {
describe('.' + state, function() {
it('is enumerable and immutable property', function() {
@@ -1118,4 +1122,109 @@ describe('WebSocket', function() {
});
});
})
+
+ describe('ssl', function() {
+ it('can connect to secure websocket server', function(done) {
+ var options = {
+ key: fs.readFileSync('test/fixtures/key.pem'),
+ cert: fs.readFileSync('test/fixtures/certificate.pem')
+ };
+ var app = https.createServer(options, function (req, res) {
+ res.writeHead(200);
+ res.end();
+ });
+ var wss = new WebSocketServer({server: app});
+ app.listen(++port, function() {
+ var ws = new WebSocket('wss://localhost:' + port);
+ });
+ wss.on('connection', function(ws) {
+ app.close();
+ ws.terminate();
+ wss.close();
+ done();
+ });
+ })
+
+ it('cannot connect to secure websocket server via ws://', function(done) {
+ var options = {
+ key: fs.readFileSync('test/fixtures/key.pem'),
+ cert: fs.readFileSync('test/fixtures/certificate.pem')
+ };
+ var app = https.createServer(options, function (req, res) {
+ res.writeHead(200);
+ res.end();
+ });
+ var wss = new WebSocketServer({server: app});
+ app.listen(++port, function() {
+ var ws = new WebSocket('ws://localhost:' + port);
+ ws.on('error', function() {
+ app.close();
+ ws.terminate();
+ wss.close();
+ done();
+ });
+ });
+ });
+
+ it('can send and receive text data', function(done) {
+ var options = {
+ key: fs.readFileSync('test/fixtures/key.pem'),
+ cert: fs.readFileSync('test/fixtures/certificate.pem')
+ };
+ var app = https.createServer(options, function (req, res) {
+ res.writeHead(200);
+ res.end();
+ });
+ var wss = new WebSocketServer({server: app});
+ app.listen(++port, function() {
+ var ws = new WebSocket('wss://localhost:' + port);
+ ws.on('open', function() {
+ ws.send('foobar');
+ });
+ });
+ wss.on('connection', function(ws) {
+ ws.on('message', function(message, flags) {
+ message.should.eql('foobar');
+ app.close();
+ ws.terminate();
+ wss.close();
+ done();
+ });
+ });
+ })
+
+ it('can send and receive very long binary data', function(done) {
+ var options = {
+ key: fs.readFileSync('test/fixtures/key.pem'),
+ cert: fs.readFileSync('test/fixtures/certificate.pem')
+ }
+ var app = https.createServer(options, function (req, res) {
+ res.writeHead(200);
+ res.end();
+ });
+ crypto.randomBytes(5 * 1024 * 1024, function(ex, buf) {
+ if (ex) throw ex;
+ var wss = new WebSocketServer({server: app});
+ app.listen(++port, function() {
+ var ws = new WebSocket('wss://localhost:' + port);
+ ws.on('open', function() {
+ ws.send(buf, {binary: true});
+ });
+ ws.on('message', function(message, flags) {
+ flags.binary.should.be.ok;
+ areArraysEqual(buf, message).should.be.ok;
+ app.close();
+ ws.terminate();
+ wss.close();
+ done();
+ });
+ });
+ wss.on('connection', function(ws) {
+ ws.on('message', function(message, flags) {
+ ws.send(message, {binary: true});
+ });
+ });
+ });
+ })
+ })
})
View
13 test/fixtures/certificate.pem
@@ -0,0 +1,13 @@
+-----BEGIN CERTIFICATE-----
+MIICATCCAWoCCQDPufXH86n2QzANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJu
+bzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
+cyBQdHkgTHRkMB4XDTEyMDEwMTE0NDQwMFoXDTIwMDMxOTE0NDQwMFowRTELMAkG
+A1UEBhMCbm8xEzARBgNVBAgMClNvbWUtU3Rh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xc0CmYtwwqL
+Kr5kS6mqJrxprJeluSjBd+3/ElxURrEXjwJAUvmlN1OPEhXDmRHd92mKnlkyKEeX
+OkiFCiIFKih1S5Y/sRJTQ0781nyJjtJqO7UyC3pnQu1oFEePL+UEniRztQJAMfav
+AtnpYKDSM+1jcp7uu9BemYGtzKDTTAYfoiNF42EzSJiGrWJDQn4eLgPjY0T0aAf/
+yGz3Z9ErbhMm/Ysl+QJBAL4kBxRT8gM4ByJw4sdOvSeCCANFq8fhbgm8pGWlCPb5
+JGmX3/GHFM8x2tbWMGpyZP1DLtiNEFz7eCGktWK5rqE=
+-----END RSA PRIVATE KEY-----
View
11 test/fixtures/request.pem
@@ -0,0 +1,11 @@
+-----BEGIN CERTIFICATE REQUEST-----
+MIIBhDCB7gIBADBFMQswCQYDVQQGEwJubzETMBEGA1UECAwKU29tZS1TdGF0ZTEh
+MB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEB
+AQUAA4GNADCBiQKBgQC2tDv6v//aJX8HoX7hugfReoWftqVxX2WmO8CbBFc0qfEC
+hrR/3sCNg8Y0squOmQ1deEElE4h1tFtmcI14Ll/NfVr4kKjspK3MFe4ZJmvbtO0W
+ZxXgf72AhEhw0e1mYkufFsmwiGQZHzJVh2Yll7h5PmV2TXOgHVp2A8XWFmEIEwID
+AQABoAAwDQYJKoZIhvcNAQEFBQADgYEAjsUXEARgfxZNkMjuUcudgU2w4JXS0gGI
+JQ0U1LmU0vMDSKwqndMlvCbKzEgPbJnGJDI8D4MeINCJHa5Ceyb8c+jaJYUcCabl
+lQW5Psn3+eWp8ncKlIycDRj1Qk615XuXtV0fhkrgQM2ZCm9LaQ1O1Gd/CzLihLjF
+W0MmgMKMMRk=
+-----END CERTIFICATE REQUEST-----

0 comments on commit ebee5c3

Please sign in to comment.
Something went wrong with that request. Please try again.