|
| 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