Skip to content

Commit 552187b

Browse files
committed
adding first tests
1 parent ecce6ac commit 552187b

File tree

4 files changed

+137
-3
lines changed

4 files changed

+137
-3
lines changed

lib/in-memory.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,5 @@ InMemory.prototype.subscribe = function (uri, client, callback) {
2727
}
2828

2929
InMemory.prototype.get = function (uri, callback) {
30-
console.log(this.subscribers, uri)
3130
return callback(null, this.subscribers[uri])
3231
}

lib/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function WsServer (server, opts) {
4545
return
4646
}
4747

48-
client.send('ack ' + uuid)
48+
client.send('ack ' + tuple[1])
4949
})
5050
})
5151

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Web sockets for Solid",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "./node_modules/mocha/bin/mocha ./test/*.js"
88
},
99
"keywords": [
1010
"solid",
@@ -20,5 +20,9 @@
2020
"debug": "^2.2.0",
2121
"node-uuid": "^1.4.3",
2222
"run-parallel": "^1.1.4"
23+
},
24+
"devDependencies": {
25+
"chai": "^3.3.0",
26+
"mocha": "^2.3.3"
2327
}
2428
}

test/websockets.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
var WebSocket = require('ws')
2+
var assert = require('chai').assert
3+
var http = require('http')
4+
var parallel = require('run-parallel')
5+
var SolidWs = require('../')
6+
7+
describe('Solid-ws', function() {
8+
var server = http.createServer()
9+
var port = 8000
10+
var pubsub = SolidWs(server)
11+
var client
12+
13+
function check(msgs, uris, done) {
14+
parallel(msgs.map(function (msg, i) {
15+
return function(cb) {
16+
assert.equal(msg.split(' ')[0], 'ack')
17+
var index = uris.indexOf(msg.split(' ')[1])
18+
assert.notEqual(index, -1, "URL not found")
19+
uris.splice(index, 1)
20+
cb()
21+
}
22+
}),
23+
function() {
24+
assert.equal(uris.length, 0)
25+
done()
26+
})
27+
}
28+
29+
before(function(done) {
30+
server.listen(port, function (err) {
31+
if (err) done(err)
32+
client = new WebSocket('http://localhost:' + port)
33+
client.on('open', done)
34+
})
35+
})
36+
37+
describe('sub', function() {
38+
it('should receive ack for any resource given', function(done) {
39+
40+
var uris = [
41+
'http://example.com/hello',
42+
'http://example.com/hello/hello.ttl',
43+
'http://example.com/hello/hello/.acl']
44+
45+
uris.map(function(uri) {
46+
client.send('sub ' + uri)
47+
})
48+
49+
var msgs = []
50+
client.on('message', function (msg) {
51+
msgs.push(msg)
52+
if (msgs.length == uris.length) {
53+
check(msgs, uris, done)
54+
}
55+
})
56+
})
57+
58+
it('should receive ack even if has already subscribed', function(done) {
59+
60+
var uris = [
61+
'http://example.com/hello',
62+
'http://example.com/hello',
63+
'http://example.com/hello/hello.ttl',
64+
'http://example.com/hello/hello.ttl',
65+
'http://example.com/hello/hello/.acl',
66+
'http://example.com/hello/hello/.acl']
67+
68+
uris.map(function(uri) {
69+
client.send('sub ' + uri)
70+
})
71+
72+
var msgs = []
73+
client.on('message', function (msg) {
74+
msgs.push(msg)
75+
if (msgs.length == uris.length) {
76+
check(msgs, uris, done)
77+
}
78+
})
79+
})
80+
})
81+
// describe('pub', function() {
82+
// describe('delete resource', function() {
83+
84+
// it('should', function(done) {
85+
// server.emit('pub '+ url)
86+
87+
// client.onmessage = function(msg) {
88+
// assert.ok(msg)
89+
// assert.equal('pub ' + url)
90+
// done()
91+
// }
92+
// })
93+
// })
94+
// describe('patch resource', function() {
95+
96+
// it('should', function(done) {
97+
// server.emit('pub '+ url)
98+
99+
// client.onmessage = function(msg) {
100+
// assert.ok(msg)
101+
// assert.equal('pub ' + url)
102+
// done()
103+
// }
104+
// })
105+
// })
106+
// describe('put resource', function() {
107+
108+
// it('should', function(done) {
109+
// server.emit('pub '+ url)
110+
111+
// client.onmessage = function(msg) {
112+
// assert.ok(msg)
113+
// assert.equal('pub ' + url)
114+
// done()
115+
// }
116+
// })
117+
// })
118+
// describe('add resource in folder', function() {
119+
120+
// it('should', function(done) {
121+
// server.emit('pub '+ url)
122+
123+
// client.onmessage = function(msg) {
124+
// assert.ok(msg)
125+
// assert.equal('pub ' + url)
126+
// done()
127+
// }
128+
// })
129+
// })
130+
// })
131+
})

0 commit comments

Comments
 (0)