-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.js
169 lines (145 loc) · 3.28 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var assert = require('assert');
var fs = require('fs');
var net = require('net');
var tls = require('tls');
var http = require('http');
var ws = require('ws');
var css = require('./index');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // trust self signed certificate
var ssl = {
key: fs.readFileSync('./support/server.key'),
cert: fs.readFileSync('./support/server.crt')
};
describe('create-stream-server', function(){
it('should create a single tcp server', function(done){
var s = css({
s1: 'tcp://localhost:9001'
}, function(stream){
stream.on('data', function(){
done();
});
});
s.listen(function(){
var c = net.connect(9001, function(){
c.write('A');
});
});
});
it('should create a single ssl server', function(done){
var s = css({
s1: 'ssl://localhost:9002'
}, {
ssl: ssl
}, function(stream){
stream.on('data', function(){
done();
});
});
s.listen(function(){
var c = tls.connect(9002, function(){
c.write('A');
});
});
});
it('should create a single ws server', function(done){
var s = css({
s1: 'ws://localhost:9003'
}, function(stream){
stream.on('data', function(){
done();
});
});
s.listen(function(){
var c = ws.connect('ws://localhost:9003', function(){
c.send('A');
});
});
});
it('should create a single wss server', function(done){
var s = css({
s1: 'wss://localhost:9004'
}, {
ssl: ssl
}, function(stream){
stream.on('data', function(){
done();
});
});
s.listen(function(){
var c = ws.connect('wss://localhost:9004', function(){
c.send('A');
});
});
});
it('should allow object config', function(done){
var s = css({
s1: {
protocol: 'ssl',
port: 9005,
ssl: ssl
}
}, function(stream){
stream.on('data', function(){
done();
});
});
s.listen(function(){
var c = tls.connect(9005, function(){
c.write('A');
});
});
});
it('should close servers', function(done){
var s = css({
s1: 'tcp://localhost:9006'
}, function(){});
s.servers.s1.on('close', function(){
done();
});
s.listen(function(){
s.close();
});
});
it('should destroy servers', function(done){
var s = css({
s1: 'tcp://localhost:9007'
}, function(){});
s.servers.s1.on('close', function(){
done();
});
s.listen(function(){
s.destroy();
});
});
it('should create multiple servers', function(done){
var servers = css({
s1: 'tcp://localhost:9008',
s2: 'ssl://0.0.0.0:9009',
s3: {
protocol: 'wss',
host: 'localhost',
port: 9010
}
}, {
ssl: ssl
}, function(){});
servers.listen(function(){
servers.close(function(){
done();
});
});
});
it('should allow attaching ws servers to an existing http server', function(done) {
var httpServer = http.createServer();
var servers = css({
s3: {
attach: httpServer
}
}, function(){});
servers.listen(function () {
servers.close(function () {
done();
});
});
});
});