forked from ryanb/private_pub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivate_pub_spec.js
165 lines (151 loc) · 6.03 KB
/
private_pub_spec.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
describe("PrivatePub", function() {
var pub, doc;
beforeEach(function() {
Faye = {}; // To simulate global Faye object
doc = {};
pub = buildPrivatePub(doc);
});
it("adds a subscription callback", function() {
pub.subscribe("hello", "callback");
expect(pub.subscriptionCallbacks["hello"]).toEqual("callback");
});
it("has a fayeExtension which adds matching subscription signature and timestamp to outgoing message", function() {
var called = false;
var message = {channel: "/meta/subscribe", subscription: "hello"}
pub.subscriptions["hello"] = {signature: "abcd", timestamp: "1234"}
pub.fayeExtension.outgoing(message, function(message) {
expect(message.ext.private_pub_signature).toEqual("abcd");
expect(message.ext.private_pub_timestamp).toEqual("1234");
called = true;
});
expect(called).toBeTruthy();
});
it("evaluates javascript in message response", function() {
pub.handleResponse({eval: 'self.subscriptions.foo = "bar"'});
expect(pub.subscriptions.foo).toEqual("bar");
});
it("triggers callback matching message channel in response", function() {
var called = false;
pub.subscribe("test", function(data, channel) {
expect(data).toEqual("abcd");
expect(channel).toEqual("test");
called = true;
});
pub.handleResponse({channel: "test", data: "abcd"});
expect(called).toBeTruthy();
});
it("adds a faye subscription with response handler when signing", function() {
var faye = {subscribe: jasmine.createSpy()};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = {server: "server", channel: "somechannel"};
pub.sign(options);
expect(faye.subscribe).toHaveBeenCalledWith("somechannel", pub.handleResponse);
expect(pub.subscriptions.server).toEqual("server");
expect(pub.subscriptions.somechannel).toEqual(options);
});
it("adds a faye subscription with response handler when signing", function() {
var faye = {subscribe: jasmine.createSpy()};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = {server: "server", channel: "somechannel"};
pub.sign(options);
expect(faye.subscribe).toHaveBeenCalledWith("somechannel", pub.handleResponse);
expect(pub.subscriptions.server).toEqual("server");
expect(pub.subscriptions.somechannel).toEqual(options);
});
it("takes a callback for subscription object when signing", function(){
var faye = {subscribe: function(){ return "subscription"; }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = { server: "server", channel: "somechannel" };
options.subscription = jasmine.createSpy();
pub.sign(options);
expect(options.subscription).toHaveBeenCalledWith("subscription");
});
it("returns the subscription object for a subscribed channel", function(){
var faye = {subscribe: function(){ return "subscription"; }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = { server: "server", channel: "somechannel" };
pub.sign(options);
expect(pub.subscription("somechannel")).toEqual("subscription")
});
it("unsubscribes a channel by name", function(){
var sub = { cancel: jasmine.createSpy() };
var faye = {subscribe: function(){ return sub; }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = { server: "server", channel: "somechannel" };
pub.sign(options);
expect(pub.subscription("somechannel")).toEqual(sub);
pub.unsubscribe("somechannel");
expect(sub.cancel).toHaveBeenCalled();
expect(pub.subscription("somechannel")).toBeFalsy();
});
it("unsubscribes all channels", function(){
var created = 0;
var sub = function() {
created ++;
var sub = { cancel: function(){ created --; } };
return sub;
};
var faye = { subscribe: function(){ return sub(); }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
pub.sign({server: "server", channel: "firstchannel"});
pub.sign({server: "server", channel: "secondchannel"});
expect(created).toEqual(2);
expect(pub.subscription("firstchannel")).toBeTruthy();
expect(pub.subscription("secondchannel")).toBeTruthy();
pub.unsubscribeAll()
expect(created).toEqual(0);
expect(pub.subscription("firstchannel")).toBeFalsy();
expect(pub.subscription("secondchannel")).toBeFalsy();
});
it("triggers faye callback function immediately when fayeClient is available", function() {
var called = false;
pub.fayeClient = "faye";
pub.faye(function(faye) {
expect(faye).toEqual("faye");
called = true;
});
expect(called).toBeTruthy();
});
it("adds fayeCallback when client and server aren't available", function() {
pub.faye("callback");
expect(pub.fayeCallbacks[0]).toEqual("callback");
});
it("adds a script tag loading faye js when the server is present", function() {
script = {};
doc.createElement = function() { return script; };
doc.documentElement = {appendChild: jasmine.createSpy()};
pub.subscriptions.server = "path/to/faye";
pub.faye("callback");
expect(pub.fayeCallbacks[0]).toEqual("callback");
expect(script.type).toEqual("text/javascript");
expect(script.src).toEqual("path/to/faye.js");
expect(script.onload).toEqual(pub.connectToFaye);
expect(doc.documentElement.appendChild).toHaveBeenCalledWith(script);
});
it("connects to faye server, adds extension, and executes callbacks", function() {
callback = jasmine.createSpy();
client = {addExtension: jasmine.createSpy()};
Faye.Client = function(server) {
expect(server).toEqual("server")
return client;
};
pub.subscriptions.server = "server";
pub.fayeCallbacks.push(callback);
pub.connectToFaye();
expect(pub.fayeClient).toEqual(client);
expect(client.addExtension).toHaveBeenCalledWith(pub.fayeExtension);
expect(callback).toHaveBeenCalledWith(client);
});
});