ref: master
public/javascripts/vendor/strophejs-1.1.3/tests/tests.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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
$(document).ready(function () { module("JIDs"); test("Normal JID", function () { var jid = "darcy@pemberley.lit/library"; equal(Strophe.getNodeFromJid(jid), "darcy", "Node should be 'darcy'"); equal(Strophe.getDomainFromJid(jid), "pemberley.lit", "Domain should be 'pemberley.lit'"); equal(Strophe.getResourceFromJid(jid), "library", "Node should be 'library'"); equal(Strophe.getBareJidFromJid(jid), "darcy@pemberley.lit", "Bare JID should be 'darcy@pemberley.lit'"); }); test("Weird node (unescaped)", function () { var jid = "darcy@netherfield.lit@pemberley.lit/library"; equal(Strophe.getNodeFromJid(jid), "darcy", "Node should be 'darcy'"); equal(Strophe.getDomainFromJid(jid), "netherfield.lit@pemberley.lit", "Domain should be 'netherfield.lit@pemberley.lit'"); equal(Strophe.getResourceFromJid(jid), "library", "Resource should be 'library'"); equal(Strophe.getBareJidFromJid(jid), "darcy@netherfield.lit@pemberley.lit", "Bare JID should be 'darcy@netherfield.lit@pemberley.lit'"); }); test("Weird node (escaped)", function () { var escapedNode = Strophe.escapeNode("darcy@netherfield.lit"); var jid = escapedNode + "@pemberley.lit/library"; equal(Strophe.getNodeFromJid(jid), "darcy\\40netherfield.lit", "Node should be 'darcy\\40netherfield.lit'"); equal(Strophe.getDomainFromJid(jid), "pemberley.lit", "Domain should be 'pemberley.lit'"); equal(Strophe.getResourceFromJid(jid), "library", "Resource should be 'library'"); equal(Strophe.getBareJidFromJid(jid), "darcy\\40netherfield.lit@pemberley.lit", "Bare JID should be 'darcy\\40netherfield.lit@pemberley.lit'"); }); test("Weird resource", function () { var jid = "books@chat.pemberley.lit/darcy@pemberley.lit/library"; equal(Strophe.getNodeFromJid(jid), "books", "Node should be 'books'"); equal(Strophe.getDomainFromJid(jid), "chat.pemberley.lit", "Domain should be 'chat.pemberley.lit'"); equal(Strophe.getResourceFromJid(jid), "darcy@pemberley.lit/library", "Resource should be 'darcy@pemberley.lit/library'"); equal(Strophe.getBareJidFromJid(jid), "books@chat.pemberley.lit", "Bare JID should be 'books@chat.pemberley.lit'"); }); module("Builder"); test("Correct namespace (#32)", function () { var stanzas = [new Strophe.Builder("message", {foo: "asdf"}).tree(), $build("iq", {}).tree(), $pres().tree()]; $.each(stanzas, function () { equal($(this).attr('xmlns'), Strophe.NS.CLIENT, "Namespace should be '" + Strophe.NS.CLIENT + "'"); }); }); test("send() accepts Builders (#27)", function () { var stanza = $pres(); var conn = new Strophe.Connection(""); // fake connection callback to avoid errors conn.connect_callback = function () {}; ok(conn._data.length === 0, "Output queue is clean"); try { conn.send(stanza); } catch (e) {} ok(conn._data.length === 1, "Output queue contains an element"); }); test("send() does not accept strings", function () { var stanza = "<presence/>"; var conn = new Strophe.Connection(""); // fake connection callback to avoid errors conn.connect_callback = function () {}; expect(1); try { conn.send(stanza); } catch (e) { equal(e.name, "StropheError", "send() should throw exception"); } }); test("Builder with XML attribute escaping test", function () { var text = "<b>"; var expected = "<presence to='<b>' xmlns='jabber:client'/>"; var pres = $pres({to: text}); equal(pres.toString(), expected, "< should be escaped"); text = "foo&bar"; expected = "<presence to='foo&bar' xmlns='jabber:client'/>"; pres = $pres({to: text}); equal(pres.toString(), expected, "& should be escaped"); }); test("c() accepts text and passes it to xmlElement", function () { var pres = $pres({from: "darcy@pemberley.lit", to: "books@chat.pemberley.lit"}) .c("nick", {xmlns: "http://jabber.org/protocol/nick"}, "Darcy"); var expected = "<presence from='darcy@pemberley.lit' to='books@chat.pemberley.lit' xmlns='jabber:client'><nick xmlns='http://jabber.org/protocol/nick'>Darcy</nick></presence>"; equal(pres.toString(), expected, "'Darcy' should be a child of <presence>"); }); module("XML"); test("XML escaping test", function () { var text = "s & p"; var textNode = Strophe.xmlTextNode(text); equal(Strophe.getText(textNode), "s & p", "should be escaped"); var text0 = "s < & > p"; var textNode0 = Strophe.xmlTextNode(text0); equal(Strophe.getText(textNode0), "s < & > p", "should be escaped"); var text1 = "s's or \"p\""; var textNode1 = Strophe.xmlTextNode(text1); equal(Strophe.getText(textNode1), "s's or "p"", "should be escaped"); var text2 = "<![CDATA[<foo>]]>"; var textNode2 = Strophe.xmlTextNode(text2); equal(Strophe.getText(textNode2), "<![CDATA[<foo>]]>", "should be escaped"); var text3 = "<![CDATA[]]]]><![CDATA[>]]>"; var textNode3 = Strophe.xmlTextNode(text3); equal(Strophe.getText(textNode3), "<![CDATA[]]]]><![CDATA[>]]>", "should be escaped"); var text4 = "<foo><![CDATA[<foo>]]>"; var textNode4 = Strophe.xmlTextNode(text4); equal(Strophe.getText(textNode4), "&lt;foo&gt;<![CDATA[<foo>]]>", "should be escaped"); }); test("XML element creation", function () { var elem = Strophe.xmlElement("message"); equal(elem.tagName, "message", "Element name should be the same"); }); test("copyElement() double escape bug", function() { var cloned = Strophe.copyElement(Strophe.xmlGenerator() .createTextNode('<><>')); equal(cloned.nodeValue, '<><>'); }); test("XML serializing", function() { var parser = new DOMParser(); // Attributes var element1 = parser.parseFromString("<foo attr1='abc' attr2='edf'>bar</foo>","text/xml").documentElement; equal(Strophe.serialize(element1), "<foo attr1='abc' attr2='edf'>bar</foo>", "should be serialized"); var element2 = parser.parseFromString("<foo attr1=\"abc\" attr2=\"edf\">bar</foo>","text/xml").documentElement; equal(Strophe.serialize(element2), "<foo attr1='abc' attr2='edf'>bar</foo>", "should be serialized"); // Escaping values var element3 = parser.parseFromString("<foo>a > 'b' & "b" < c</foo>","text/xml").documentElement; equal(Strophe.serialize(element3), "<foo>a > 'b' & "b" < c</foo>", "should be serialized"); // Escaping attributes var element4 = parser.parseFromString("<foo attr='<a> 'b''>bar</foo>","text/xml").documentElement; equal(Strophe.serialize(element4), "<foo attr='<a> 'b''>bar</foo>", "should be serialized"); var element5 = parser.parseFromString("<foo attr=\"<a> "b"\">bar</foo>","text/xml").documentElement; equal(Strophe.serialize(element5), "<foo attr='<a> \"b\"'>bar</foo>", "should be serialized"); // Empty elements var element6 = parser.parseFromString("<foo><empty></empty></foo>","text/xml").documentElement; equal(Strophe.serialize(element6), "<foo><empty/></foo>", "should be serialized"); // Children var element7 = parser.parseFromString("<foo><bar>a</bar><baz><wibble>b</wibble></baz></foo>","text/xml").documentElement; equal(Strophe.serialize(element7), "<foo><bar>a</bar><baz><wibble>b</wibble></baz></foo>", "should be serialized"); var element8 = parser.parseFromString("<foo><bar>a</bar><baz>b<wibble>c</wibble>d</baz></foo>","text/xml").documentElement; equal(Strophe.serialize(element8), "<foo><bar>a</bar><baz>b<wibble>c</wibble>d</baz></foo>", "should be serialized"); // CDATA var element9 = parser.parseFromString("<foo><![CDATA[<foo>]]></foo>","text/xml").documentElement; equal(Strophe.serialize(element9), "<foo><![CDATA[<foo>]]></foo>", "should be serialized"); var element10 = parser.parseFromString("<foo><![CDATA[]]]]><![CDATA[>]]></foo>","text/xml").documentElement; equal(Strophe.serialize(element10), "<foo><![CDATA[]]]]><![CDATA[>]]></foo>", "should be serialized"); var element11 = parser.parseFromString("<foo><foo><![CDATA[<foo>]]></foo>","text/xml").documentElement; equal(Strophe.serialize(element11), "<foo><foo><![CDATA[<foo>]]></foo>", "should be serialized"); }); module("Handler"); test("Full JID matching", function () { var elem = $msg({from: 'darcy@pemberley.lit/library'}).tree(); var hand = new Strophe.Handler(null, null, null, null, null, 'darcy@pemberley.lit/library'); equal(hand.isMatch(elem), true, "Full JID should match"); hand = new Strophe.Handler(null, null, null, null, null, 'darcy@pemberley.lit') equal(hand.isMatch(elem), false, "Bare JID shouldn't match"); }); test("Bare JID matching", function () { var elem = $msg({from: 'darcy@pemberley.lit/library'}).tree(); var hand = new Strophe.Handler(null, null, null, null, null, 'darcy@pemberley.lit/library', {matchBare: true}); equal(hand.isMatch(elem), true, "Full JID should match"); hand = new Strophe.Handler(null, null, null, null, null, 'darcy@pemberley.lit', {matchBare: true}); equal(hand.isMatch(elem), true, "Bare JID should match"); }); module("Misc"); test("Quoting strings", function () { var input = '"beep \\40"'; var saslmd5 = new Strophe.SASLMD5(); var output = saslmd5._quote(input); equal(output, "\"\\\"beep \\\\40\\\"\"", "string should be quoted and escaped"); }); test("Function binding", function () { var spy = sinon.spy(); var obj = {}; var arg1 = "foo"; var arg2 = "bar"; var arg3 = "baz"; var f = spy.bind(obj, arg1, arg2); f(arg3); equal(spy.called, true, "bound function should be called"); equal(spy.calledOn(obj), true, "bound function should have correct context"); equal(spy.alwaysCalledWithExactly(arg1, arg2, arg3), true, "bound function should get all arguments"); }); module("XHR error handling"); // Note that these tests are pretty dependent on the actual code. test("Aborted requests do nothing", function () { Strophe.Connection.prototype._onIdle = function () {}; var conn = new Strophe.Connection("http://fake"); // simulate a finished but aborted request var req = {id: 43, sends: 1, xhr: { readyState: 4 }, abort: true}; conn._requests = [req]; var spy = sinon.spy(); conn._proto._onRequestStateChange(spy, req); equal(req.abort, false, "abort flag should be toggled"); equal(conn._requests.length, 1, "_requests should be same length"); equal(spy.called, false, "callback should not be called"); }); test("Incomplete requests do nothing", function () { Strophe.Connection.prototype._onIdle = function () {}; var conn = new Strophe.Connection("http://fake"); // simulate a finished but aborted request var req = {id: 44, sends: 1, xhr: { readyState: 3 }}; conn._requests = [req]; var spy = sinon.spy(); conn._proto._onRequestStateChange(spy, req); equal(conn._requests.length, 1, "_requests should be same length"); equal(spy.called, false, "callback should not be called"); }); module("SASL Mechanisms"); test("SASL Plain Auth", function () { var conn = {pass: "password", authcid: "user", authzid: "user@xmpp.org"}; ok(Strophe.SASLPlain.test(conn), "plain should pass the test"); var saslplain = new Strophe.SASLPlain(); saslplain.onStart(conn); var response = saslplain.onChallenge(conn, null); equal(response, [conn.authzid, conn.authcid, conn.pass].join("\u0000"), "checking plain auth challenge"); saslplain.onSuccess(); }); test("SASL SCRAM-SHA-1 Auth", function () { var conn = {pass: "pencil", authcid: "user", authzid: "user@xmpp.org", _sasl_data: []}; ok(Strophe.SASLSHA1.test(conn), "sha-1 should pass the test"); var saslsha1 = new Strophe.SASLSHA1(); saslsha1.onStart(conn); // test taken from example section on: // URL: http://tools.ietf.org/html/rfc5802#section-5 var response = saslsha1.onChallenge(conn, null, "fyko+d2lbbFgONRv9qkxdawL"); equal(response, "n,,n=user,r=fyko+d2lbbFgONRv9qkxdawL", "checking first auth challenge"); var response = saslsha1.onChallenge(conn, "r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,s=QSXCR+Q6sek8bf92,i=4096"); equal(response, "c=biws,r=fyko+d2lbbFgONRv9qkxdawL3rfcNHYJY1ZVvWVs7j,p=v0X8v3Bz2T0CJGbJQyF0X+HI4Ts=", "checking second auth challenge"); saslsha1.onSuccess(); }); test("SASL DIGEST-MD-5 Auth", function () { var conn = {pass: "secret", authcid: "chris", authzid: "user@xmpp.org", servtype: "imap", domain: "elwood.innosoft.com", _sasl_data: []}; ok(Strophe.SASLMD5.test(conn), "md-5 should pass the test"); var saslmd5 = new Strophe.SASLMD5(); saslmd5.onStart(conn); // test taken from example section on: // URL: http://www.ietf.org/rfc/rfc2831.txt var response = saslmd5.onChallenge(conn, "realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",qop=\"auth\",algorithm=md5-sess,charset=utf-8", "OA6MHXh6VqTrRk"); equal(response, "charset=utf-8,username=\"chris\",realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",nc=00000001,cnonce=\"OA6MHXh6VqTrRk\",digest-uri=\"imap/elwood.innosoft.com\",response=d388dad90d4bbd760a152321f2143af7,qop=auth", "checking first auth challenge"); var response = saslmd5.onChallenge(conn, "rspauth=ea40f60335c427b5527b84dbabcdfffd"); equal(response, "", "checking second auth challenge"); saslmd5.onSuccess(); }); }); |