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 | var EventEmitter = require('events').EventEmitter
|
7 | var utils = require('./utils')
|
8 |
|
9 | describe('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 pub to everyone, independently of the host name', function (done) {
|
103 | var urls = [
|
104 | 'http://example.com/resource.ttl',
|
105 | 'http://domain.com/resource.ttl',
|
106 | '/resource.ttl' ]
|
107 | var users = [
|
108 | 'http://nicola.io/#me',
|
109 | 'http://timbl.com/#me' ]
|
110 |
|
111 | var clients = users.map(function() {
|
112 | return new WebSocket('http://localhost:' + port)
|
113 | })
|
114 |
|
115 | var pubs = []
|
116 |
|
117 | utils.connectAll(clients, urls, function() {
|
118 | utils.ackAll(clients, function() {
|
119 | utils.pubAll(clients, pubs, function() {
|
120 | assert.equal(pubs.length, users.length)
|
121 | done()
|
122 | })
|
123 | pubsub.publish('/resource.ttl')
|
124 | })
|
125 | })
|
126 | })
|
127 |
|
128 | it('should be received by all the clients subscribed to a resource', function(done) {
|
129 |
|
130 | var url = 'http://example.com/resource.ttl'
|
131 | var users = [
|
132 | 'http://nicola.io/#me',
|
133 | 'http://timbl.com/#me' ]
|
134 |
|
135 | var clients = users.map(function() {
|
136 | return new WebSocket('http://localhost:' + port)
|
137 | })
|
138 |
|
139 | var pubs = []
|
140 |
|
141 | utils.connectAll(clients, url, function() {
|
142 | utils.ackAll(clients, function() {
|
143 | utils.pubAll(clients, pubs, function() {
|
144 | assert.equal(pubs.length, users.length)
|
145 | done()
|
146 | })
|
147 | pubsub.publish('/resource.ttl')
|
148 | })
|
149 | })
|
150 | })
|
151 | })
|
152 | })
|