cirandas.net

ref: master

public/javascripts/vendor/strophejs-1.1.3/tests/pubsub.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

Strophe.Test = {
    BOSH_URL: "/xmpp-httpbind",
    XMPP_DOMAIN: 'localhost',
    PUBSUB_COMPONENT: "pubsub.localhost",
    _node_name: "", //node name created in connectCallback function
    connection: null, //connection object created in run function
    
    run: function() {
	$(document).ready(function(){
	    //Connect strophe, uses localhost to test
	    Strophe.Test.connection = new Strophe.Connection(Strophe.Test.BOSH_URL);
	    
	    //connect anonymously to run most tests
	    Strophe.Test.connection.connect(Strophe.Test.XMPP_DOMAIN,
                                            null,
			                    Strophe.Test.connectCallback);

	    //set up the test client UI
	    $("#disconnect").click(function() {
		Strophe.Test.connection.disconnect();
	    });
	    $("#run_tests").click(function() {      
		test("Anonymous connection test.", function() {
		    if(Strophe.Test.connection.connected)
		    {
			ok( true, "all good");
		    }
		    else
		    {
			ok( false, "not connected anonymously");
		    }
		});

		test("Create default node test.",function(){
		    var iqid =  Strophe.Test.connection.pubsub
                        .createNode(Strophe.Test.connection.jid,
				    Strophe.Test.PUBSUB_COMPONENT,
				    Strophe.Test._node_name, {}, 
                                    function(stanza) {
			                test("handled create node.",
				             function() {
				                 var error = $(stanza).find("error");
				                 
				                 if(error.length == 0)
				                 {
					             ok(true, "returned");
				                 }
				                 else
				                 {
					             ok(false,"error creating node.");
				                 }
				             });
				    });
		    ok(true,"sent create request. "+ iqid);
		});

		test("subscribe to a node",function() {
		    var iqid = Strophe.Test.connection.pubsub
                        .subscribe(Strophe.Test.connection.jid,
				   Strophe.Test.PUBSUB_COMPONENT,
				   Strophe.Test._node_name,
				   [],
			           function(stanza) {
			               test("items received", function() {
				           console.log(stanza);
				           if($(stanza).length > 0)
				           {
				               ok(true,"item received.");
				           }
				           else
				           {
				               ok(false,"no items.");
				           }
			               });
			           },
			           function(stanza) {
			               var error = $(stanza).find("error");
			               
			               test("handled subscribe",
				            function() {
				                if(error.length == 0)
				                {
					            ok(true,"subscribed");
				                }
				                else
				                {
					            console.log(error.get(0));
					            ok(false,
					               "not subscribed");
				                }
				                
			                    });
			           });
		    
		    if(iqid)
			ok(true,
			   "subscribed to " + Strophe.Test._node_name);
		});

		test("publish to a node",function() {
		    var iqid = Strophe.Test.connection.pubsub
                        .publish(Strophe.Test.connection.jid,
				 Strophe.Test.PUBSUB_COMPONENT,
				 Strophe.Test._node_name,
			         {test:'test'},
				 function(stanza) {
				     var error = $(stanza).find("error");
				     
				     test("handled published item",
					  function() {
					      if(error.length == 0)
					      {
						  ok(true,"got item");
					      }
					      else
					      {
						  ok(false,
						     "no item");
					      }
					      
					  });
				 });
		    
		    if(iqid)
			ok(true,
			   "published to " + Strophe.Test._node_name);
                    
		});		

		test("subscribe to a node with options",function() {
		    var keyword_elem = Strophe.xmlElement("field",
							  [["var",
							    "http://stanziq.com/search#keyword"],
							   ["type",
							    'text-single'],
							   ["label",
							    "keyword to match"]]);
		    var value = Strophe.xmlElement("value",[]);
		    var text = Strophe.xmlTextNode("crazy");
		    value.appendChild(text);
		    keyword_elem.appendChild(value);
		    
		    var iqid = Strophe.Test.connection.pubsub
                        .subscribe(Strophe.Test.connection.jid,
				   Strophe.Test.PUBSUB_COMPONENT,
				   Strophe.Test._node_name,
			           [keyword_elem],
		                   function(stanza) {console.log(stanza);},
			           function(stanza) {
			               
			               var error = $(stanza).find("error");
			               
			               test("handled subscribe with options",
				            function() {
				                if(error.length == 0)
				                {
					            ok(true,"search subscribed");
				                }
				                else
				                {
					            console.log(error.get(0));
					            ok(false,
					               "search not subscribed");
				                }
				                
			                    });
			           });
		    
		    if(iqid)
			ok(true,
			   "subscribed to search");
		});

		test("unsubscribe to a node",function() {
		    var iqid = Strophe.Test.connection.pubsub
                        .unsubscribe(Strophe.Test.connection.jid,
				     Strophe.Test.PUBSUB_COMPONENT,
				     Strophe.Test._node_name,
		                     function(stanza) {
			                 var error = $(stanza).find("error");
			                 
			                 test("handled unsubscribe",
				              function() {
			                          if(error.length == 0)
				                  {
				                      ok(true,"unsubscribed");
				                  }
			                          else
				                  {
				                      console.log(error.get(0));
				                      ok(false,
					                 "unable to unsubscribed");
				                  }
			                      });
		                     });
		    
		    if(iqid)
			ok(true,
			   "unsubscribed from search with no options.");
		});

		test("test items retrieval",function(){
		    var itemid = Strophe.Test.connection.pubsub
                        .items(Strophe.Test.connection.jid,
			       Strophe.Test.PUBSUB_COMPONENT,
			       Strophe.Test._node_name,
			       function(stanza) {
				   ok(true,"item request successful.");
			       },
			       function(stanza) {
				   ok(false,"failed to send request.");
			       });
                    
		    if(itemid)
		    {
			ok(true,"item request sent.");
		    }
		});

		test("test sendIQ interface.",function(){
		    var sendiq_good = false;
		    //setup timeout for sendIQ for 3 seconds
		    setTimeout(function() {
                        test("Timeout check", function () {
			    ok(sendiq_good, "The iq didn't timeout.");
                        });
		    }, 3000);
                    
		    //send a pubsub subscribe stanza
                    
		    var sub = $iq({from:Strophe.Test.connection.jid,
			           to:Strophe.Test.PUBSUB_COMPONENT,
			           type:'set'})
		        .c('pubsub', { xmlns:Strophe.NS.PUBSUB })
                        .c('subscribe',
		           {node:Strophe.Test._node_name,
			    jid:Strophe.Test.connection.jid});
		    var stanza=sub.tree();
		    //call sendIQ with several call backs
		    Strophe.Test.connection
                        .sendIQ(stanza, 
                                function(stanza) {
			            test("iq sent",function() {
			                sendiq_good = true;
			                ok(true,"iq sent succesfully.");
			            });
		                },
		                function(stz) {
			            test("iq fail",function() {
                                        if (stz)
			                    sendiq_good = true;
			                console.log(stanza);
			                ok(true,"failed to send iq.");
			            });
		                });
		});

		test("test sendIQ failed.",function(){
		    var sub = $iq({from:Strophe.Test.connection.jid, 
			           to:Strophe.Test.PUBSUB_COMPONENT, 
			           type:'get'});

		    //call sendIQ with several call backs
		    Strophe.Test.connection
                        .sendIQ(sub.tree(),
		                function(stanza) {
			            console.log(stanza);
			            test("iq sent",function() {
			                ok(false,
			                   "iq sent succesfully when should have failed.");
			            });
		                },
		                function(stanza) {
			            test("iq fail",function() {
			                ok(true,
			                   "success on failure test: failed to send iq.");
			            });
		                });		    
		});
	    });
	});
    },

    connectCallback: function(status,cond) {
	var error_message = null;
	if(status == Strophe.Status.CONNECTED)
	{
	    $('#run_tests').show();
	    $('#disconnect').show();
	    var bare_jid = Strophe.getBareJidFromJid(Strophe.Test.connection.jid).split("@")[0];
	    Strophe.Test._node_name = "/home/"+Strophe.Test.XMPP_DOMAIN+"/"+bare_jid;
	}
	else if (status == Strophe.Status.DISCONNECTED || status == Strophe.Status.DICONNECTING)
	{
	    $('#run_tests').hide();
	    $('#disconnect').hide();
	}	
	else if ((status == 0) || (status == Strophe.Status.CONNFAIL))
	{
	    error_message = "Failed to connect to xmpp server.";
	}
	else if (status == Strophe.Status.AUTHFAIL)
	{
	    error_message = "Failed to authenticate to xmpp server.";
	}
	if(error_message)
	{
	    $('published_item').text(error_message);
	    
	}
    }
};