UNPKG

3.69 kBJavaScriptView Raw
1'use strict';
2
3const assert = require('assert'),
4 fs = require('fs'),
5 WebSocket = require('ws'),
6 Koa = require('koa'),
7 route = require('koa-route');
8
9const koaws = require('..');
10
11describe('should route ws messages seperately', function() {
12 const app = koaws(new Koa());
13
14 app.ws.use(function(ctx, next){
15 ctx.websocket.on('message', function(message) {
16 if (message == '123') {
17 ctx.websocket.send(message);
18 }
19 });
20 return next();
21 });
22
23 app.ws.use(route.all('/abc', function(ctx){
24 ctx.websocket.on('message', function(message) {
25 ctx.websocket.send(message);
26 });
27 }));
28
29 app.ws.use(route.all('/abc', function(ctx){
30 ctx.websocket.on('message', function(message) {
31 ctx.websocket.send(message);
32 });
33 }));
34
35 app.ws.use(route.all('/def', function(ctx){
36 ctx.websocket.on('message', function(message) {
37 ctx.websocket.send(message);
38 });
39 }));
40
41 let server = null;
42
43 before(function(done){
44 server = app.listen(done);
45 });
46
47 after(function(done){
48 server.close(done);
49 });
50
51 it('sends 123 message to any route', function(done){
52 const ws = new WebSocket('ws://localhost:' + server.address().port + '/not-a-route');
53 ws.on('open', function() {
54 ws.send('123');
55 });
56 ws.on('message', function(message) {
57 assert(message === '123');
58 done();
59 ws.close();
60 });
61 });
62
63 it('sends abc message to abc route', function(done){
64 const ws = new WebSocket('ws://localhost:' + server.address().port + '/abc');
65 ws.on('open', function() {
66 ws.send('abc');
67 });
68 ws.on('message', function(message) {
69 assert(message === 'abc');
70 done();
71 ws.close();
72 });
73 });
74
75 it('sends def message to def route', function(done){
76 const ws = new WebSocket('ws://localhost:' + server.address().port + '/def');
77 ws.on('open', function() {
78 ws.send('def');
79 });
80 ws.on('message', function(message) {
81 assert(message === 'def');
82 done();
83 ws.close();
84 });
85 });
86
87 it('handles urls with query parameters', function(done){
88 const ws = new WebSocket('ws://localhost:' + server.address().port + '/abc?foo=bar');
89 ws.on('open', function() {
90 ws.send('abc');
91 });
92 ws.on('message', function(message) {
93 assert(message === 'abc');
94 done();
95 ws.close();
96 });
97 });
98});
99
100describe('should support custom ws server options', function() {
101
102 const app = koaws(new Koa(), {
103 handleProtocols: function (protocols) {
104 if (protocols.indexOf('bad_protocol') !== -1)
105 return false;
106 return protocols.pop();
107 }
108 });
109
110 let server = null;
111
112 before(function(done){
113 server = app.listen(done);
114 });
115
116 after(function(done){
117 server.close(done);
118 });
119
120 it('reject bad protocol use wsOptions', function(done){
121 const ws = new WebSocket('ws://localhost:' + server.address().port + '/abc', ['bad_protocol']);
122 ws.on('error', function() {
123 assert(true);
124 done();
125 ws.close();
126 });
127 });
128});
129
130describe('should support custom http server options', function() {
131
132 // The cert is self-signed.
133 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
134
135 const secureApp = koaws(new Koa(), {}, {
136 key: fs.readFileSync('./test/key.pem'),
137 cert: fs.readFileSync('./test/cert.pem'),
138 });
139
140 let server = null;
141
142 before(function(done){
143 server = secureApp.listen(done);
144 });
145
146 after(function(done){
147 server.close(done);
148 });
149
150 it('supports wss protocol', function(done){
151 const ws = new WebSocket('wss://localhost:' + server.address().port + '/abc');
152 ws.on('open', function() {
153 assert(true);
154 done();
155 ws.close();
156 });
157 });
158});