UNPKG

3.21 kBJavaScriptView Raw
1var WebSocket = require('ws')
2var assert = require('chai').assert
3var http = require('http')
4var parallel = require('run-parallel')
5var SolidWs = require('../')
6var EventEmitter = require('events').EventEmitter
7var utils = require('./utils')
8
9describe('Solid-ws', function() {
10 var server = http.createServer()
11 var port = 8000
12 var pubsub = SolidWs(server)
13
14 function check(msgs, uris, done) {
15 parallel(msgs.map(function (msg, i) {
16 return function(cb) {
17 assert.equal(msg.split(' ')[0], 'ack')
18 var index = uris.indexOf(msg.split(' ')[1])
19 assert.notEqual(index, -1, "URL not found")
20 uris.splice(index, 1)
21 cb()
22 }
23 }),
24 function() {
25 assert.equal(uris.length, 0)
26 done()
27 })
28 }
29
30 before(function(done) {
31 server.listen(port, function (err) {
32 done(err)
33 })
34 })
35 after(function() {
36 server.close()
37 })
38
39 describe('sub', function() {
40 beforeEach(function(done) {
41 client = new WebSocket('http://localhost:' + port)
42 client.on('open', done)
43 })
44 afterEach(function(done) {
45 client.close()
46 done()
47 })
48 it('should receive ack in the form `ack $uri`', function(done) {
49
50 var uri = 'http://example.com/myresource'
51 client.send('sub ' + uri)
52 client.on('message', function (msg) {
53 assert.equal(msg, 'ack ' + uri)
54 done()
55 })
56 })
57 it('should receive ack for any resource given', function(done) {
58
59 var uris = [
60 'http://example.com/hello',
61 'http://example.com/hello/hello.ttl',
62 'http://example.com/hello/hello/.acl']
63
64 uris.map(function(uri) {
65 client.send('sub ' + uri)
66 })
67
68 var msgs = []
69 client.on('message', function (msg) {
70 msgs.push(msg)
71 if (msgs.length == uris.length) {
72 check(msgs, uris, done)
73 }
74 })
75 })
76
77 it('should receive ack even if has already subscribed', function(done) {
78
79 var uris = [
80 'http://example.com/hello',
81 'http://example.com/hello',
82 'http://example.com/hello/hello.ttl',
83 'http://example.com/hello/hello.ttl',
84 'http://example.com/hello/hello/.acl',
85 'http://example.com/hello/hello/.acl']
86
87 uris.map(function(uri) {
88 client.send('sub ' + uri)
89 })
90
91 var msgs = []
92 client.on('message', function (msg) {
93 msgs.push(msg)
94 if (msgs.length == uris.length) {
95 check(msgs, uris, done)
96 }
97 })
98 })
99 })
100
101 describe('pub', function() {
102 it('should be received by all the clients subscribed to a resource', function(done) {
103
104 var url = 'http://example.com/resource.ttl'
105 var users = [
106 'http://nicola.io#me',
107 'http://timbl.com#me',
108 'http://deiu.io#me' ]
109
110 var clients = users.map(function() {
111 return new WebSocket('http://localhost:' + port)
112 })
113
114 var pubs = []
115
116 utils.connectAll(clients, url, function() {
117 utils.ackAll(clients, function() {
118 utils.pubAll(clients, pubs, function() {
119 assert.equal(pubs.length, users.length)
120 done()
121 })
122 pubsub.publish(url)
123 })
124 })
125 })
126 })
127})