/
api_socket.js
199 lines (176 loc) · 5.7 KB
/
api_socket.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var specHelper = require('../_specHelper.js').specHelper;
var suite = specHelper.vows.describe('API general functions');
var apiObj = {};
var net = require('net');
var client = {};
var client2 = {};
var client3 = {};
function makeSocketRequest(thisClient, cb, message){
var rsp = function(d){
parsed = JSON.parse(d);
thisClient.removeListener('data', rsp);
cb(true, parsed);
};
thisClient.on('data', rsp);
thisClient.write(message + "\r\n");
}
suite.addBatch({
'specHelper.prepare':{
topic: function(){
var cb = this.callback;
specHelper.prepare(function(api){
client = net.connect(specHelper.params.socketServerPort, function(){
client.setEncoding('utf8');
client2 = net.connect(specHelper.params.socketServerPort, function(){
client2.setEncoding('utf8');
client3 = net.connect(specHelper.params.socketServerPort, function(){
client3.setEncoding('utf8');
apiObj = specHelper.cleanAPIObject(api);
cb();
});
});
});
})
},
'api object should exist': function(){ specHelper.assert.isObject(apiObj); },
}
});
suite.addBatch({
"socket connections should be able to connect and get JSON": {
topic: function(){
makeSocketRequest(client, this.callback, "hello");
}, 'should be a JSON response 1' : function(resp, d){
specHelper.assert.isObject(d);
specHelper.assert.equal("hello is not a known action.", d.error);
}
}
});
suite.addBatch({
"single string message are treated as actions": {
topic: function(){
makeSocketRequest(client, this.callback, "status");
}, 'works' : function(resp, d){
specHelper.assert.isObject(d.stats);
specHelper.assert.equal(d.stats.socketServer.numberOfSocketRequests, 3);
}
}
});
suite.addBatch({
"default parms are set": {
topic: function(){
makeSocketRequest(client, this.callback, "paramsView");
}, 'works' : function(resp, d){
specHelper.assert.equal(d.params.limit, 100);
specHelper.assert.equal(d.params.offset, 0);
}
}
});
suite.addBatch({
"default parms can be updated": {
topic: function(){ makeSocketRequest(client, this.callback, "paramAdd limit=50"); },
'works' : function(resp, d){ specHelper.assert.equal(d.status, "OK"); }
}
});
suite.addBatch({
"actions will fail without proper parmas set to the connection": {
topic: function(){ makeSocketRequest(client, this.callback, "cacheTest"); },
'works' : function(resp, d){ specHelper.assert.equal(d.error, "key is a required parameter for this action"); }
}
});
suite.addBatch({
"a new param can be added": {
topic: function(){ makeSocketRequest(client, this.callback, "paramAdd key=socketTestKey"); },
'works' : function(resp, d){ specHelper.assert.equal(d.status, "OK"); }
}
});
suite.addBatch({
"a new param can be viewed once added": {
topic: function(){ makeSocketRequest(client, this.callback, "paramView key"); },
'works' : function(resp, d){ specHelper.assert.equal(d.params.key, "socketTestKey");}
}
});
suite.addBatch({
"another new param can be added": {
topic: function(){ makeSocketRequest(client, this.callback, "paramAdd value=abc123"); },
'works' : function(resp, d){ specHelper.assert.equal(d.status, "OK"); }
}
});
suite.addBatch({
"actions will work once all the needed params are added": {
topic: function(){ makeSocketRequest(client, this.callback, "cacheTest"); },
'works' : function(resp, d){ specHelper.assert.equal(d.cacheTestResults.saveResp, true); }
}
});
suite.addBatch({
"updated parms persist": {
topic: function(){ makeSocketRequest(client, this.callback, "paramsView"); },
'works' : function(resp, d){ specHelper.assert.equal(d.params.limit, 50); }
}
});
suite.addBatch({
"clients start in the default room": {
topic: function(){ makeSocketRequest(client, this.callback, "roomView"); },
'works' : function(resp, d){ specHelper.assert.equal(d.room, apiObj.configData.defaultSocketRoom); }
}
});
suite.addBatch({
"clients can view additional infor about rooms they are in": {
topic: function(){ makeSocketRequest(client, this.callback, "roomView"); },
'works' : function(resp, d){ specHelper.assert.equal(d.roomStatus.membersCount, 3); }
}
});
suite.addBatch({
"rooms can be changed": {
topic: function(){ makeSocketRequest(client, this.callback, "roomChange otherRoom");},
'works' : function(resp, d){ specHelper.assert.equal(d.status, "OK"); }
}
});
suite.addBatch({
"rooms changes persist": {
topic: function(){ makeSocketRequest(client, this.callback, "roomView"); },
'works' : function(resp, d){ specHelper.assert.equal(d.room, "otherRoom"); }
}
});
suite.addBatch({
"connections in the first room see the count go down": {
topic: function(){ makeSocketRequest(client2, this.callback, "roomView"); },
'works' : function(resp, d){ specHelper.assert.equal(d.roomStatus.membersCount, 2); }
}
});
suite.addBatch({
"folks in my room hear what I say (and say works)": {
topic: function(){
cb = this.callback;
var rsp = function(d){
parsed = JSON.parse(d);
client3.removeListener('data', rsp);
cb(true, parsed);
};
client3.on('data', rsp);
client2.write("say hello?" + "\r\n");
},
'works' : function(resp, d){specHelper.assert.equal(d.message, "hello?"); }
}
});
suite.addBatch({
"folks NOT in my room DON'T hear what I say": {
topic: function(){
cb = this.callback;
var rsp = function(d){
parsed = JSON.parse(d);
client.removeListener('data', rsp);
cb(true, parsed);
};
var noDataRsp = function(){
client.removeListener('data', rsp);
cb(true, 'no data');
}
client.on('data', rsp);
client3.write("say hello?");
setTimeout(noDataRsp, 1000);
},
'works' : function(resp, d){ specHelper.assert.equal(d, "no data"); }
}
});
// export
suite.export(module);