[2397] | 1 | TestCase("JIDs", { |
---|
| 2 | testNormalJid: function () { |
---|
| 3 | var jid = "darcy@pemberley.lit/library"; |
---|
| 4 | assertEquals("Node should be 'darcy'", |
---|
| 5 | "darcy", Strophe.getNodeFromJid(jid)); |
---|
| 6 | assertEquals("Domain should be 'pemberley.lit'", |
---|
| 7 | "pemberley.lit", Strophe.getDomainFromJid(jid)); |
---|
| 8 | assertEquals("Resource should be 'library'", |
---|
| 9 | "library", Strophe.getResourceFromJid(jid)); |
---|
| 10 | assertEquals("Bare JID should be 'darcy@pemberley.lit'", |
---|
| 11 | "darcy@pemberley.lit", Strophe.getBareJidFromJid(jid)); |
---|
| 12 | }, |
---|
| 13 | |
---|
| 14 | testWeirdNode: function () { |
---|
| 15 | var jid = "darcy@netherfield.lit@pemberley.lit/library"; |
---|
| 16 | assertEquals("Node should be 'darcy'", |
---|
| 17 | "darcy", Strophe.getNodeFromJid(jid)); |
---|
| 18 | assertEquals("Domain should be 'netherfield.lit@pemberley.lit'", |
---|
| 19 | "netherfield.lit@pemberley.lit", |
---|
| 20 | Strophe.getDomainFromJid(jid)); |
---|
| 21 | assertEquals("Resource should be 'library'", |
---|
| 22 | "library", Strophe.getResourceFromJid(jid)); |
---|
| 23 | assertEquals("Bare JID should be 'darcy@netherfield.lit@pemberley.lit", |
---|
| 24 | "darcy@netherfield.lit@pemberley.lit", |
---|
| 25 | Strophe.getBareJidFromJid(jid)); |
---|
| 26 | } |
---|
| 27 | }); |
---|
| 28 | |
---|
| 29 | function foo() { |
---|
| 30 | var $ = function () {}; |
---|
| 31 | |
---|
| 32 | test("Weird node (escaped)", function () { |
---|
| 33 | var escapedNode = Strophe.escapeNode("darcy@netherfield.lit"); |
---|
| 34 | var jid = escapedNode + "@pemberley.lit/library"; |
---|
| 35 | equals(Strophe.getNodeFromJid(jid), "darcy\\40netherfield.lit", |
---|
| 36 | "Node should be 'darcy\\40netherfield.lit'"); |
---|
| 37 | equals(Strophe.getDomainFromJid(jid), |
---|
| 38 | "pemberley.lit", |
---|
| 39 | "Domain should be 'pemberley.lit'"); |
---|
| 40 | equals(Strophe.getResourceFromJid(jid), "library", |
---|
| 41 | "Resource should be 'library'"); |
---|
| 42 | equals(Strophe.getBareJidFromJid(jid), |
---|
| 43 | "darcy\\40netherfield.lit@pemberley.lit", |
---|
| 44 | "Bare JID should be 'darcy\\40netherfield.lit@pemberley.lit'"); |
---|
| 45 | }); |
---|
| 46 | |
---|
| 47 | test("Weird resource", function () { |
---|
| 48 | var jid = "books@chat.pemberley.lit/darcy@pemberley.lit/library"; |
---|
| 49 | equals(Strophe.getNodeFromJid(jid), "books", |
---|
| 50 | "Node should be 'books'"); |
---|
| 51 | equals(Strophe.getDomainFromJid(jid), "chat.pemberley.lit", |
---|
| 52 | "Domain should be 'chat.pemberley.lit'"); |
---|
| 53 | equals(Strophe.getResourceFromJid(jid), |
---|
| 54 | "darcy@pemberley.lit/library", |
---|
| 55 | "Resource should be 'darcy@pemberley.lit/library'"); |
---|
| 56 | equals(Strophe.getBareJidFromJid(jid), |
---|
| 57 | "books@chat.pemberley.lit", |
---|
| 58 | "Bare JID should be 'books@chat.pemberley.lit'"); |
---|
| 59 | }); |
---|
| 60 | |
---|
| 61 | module("Builder"); |
---|
| 62 | |
---|
| 63 | test("Correct namespace (#32)", function () { |
---|
| 64 | var stanzas = [new Strophe.Builder("message", {foo: "asdf"}).tree(), |
---|
| 65 | $build("iq", {}).tree(), |
---|
| 66 | $pres().tree()]; |
---|
| 67 | $.each(stanzas, function () { |
---|
| 68 | equals($(this).attr('xmlns'), Strophe.NS.CLIENT, |
---|
| 69 | "Namespace should be '" + Strophe.NS.CLIENT + "'"); |
---|
| 70 | }); |
---|
| 71 | }); |
---|
| 72 | |
---|
| 73 | test("send() accepts Builders (#27)", function () { |
---|
| 74 | var stanza = $pres(); |
---|
| 75 | var conn = new Strophe.Connection(""); |
---|
| 76 | // fake connection callback to avoid errors |
---|
| 77 | conn.connect_callback = function () {}; |
---|
| 78 | |
---|
| 79 | ok(conn._data.length === 0, "Output queue is clean"); |
---|
| 80 | try { |
---|
| 81 | conn.send(stanza); |
---|
| 82 | } catch (e) {} |
---|
| 83 | ok(conn._data.length === 1, "Output queue contains an element"); |
---|
| 84 | }); |
---|
| 85 | |
---|
| 86 | test("send() does not accept strings", function () { |
---|
| 87 | var stanza = "<presence/>"; |
---|
| 88 | var conn = new Strophe.Connection(""); |
---|
| 89 | // fake connection callback to avoid errors |
---|
| 90 | conn.connect_callback = function () {}; |
---|
| 91 | expect(1); |
---|
| 92 | try { |
---|
| 93 | conn.send(stanza); |
---|
| 94 | } catch (e) { |
---|
| 95 | equals(e.name, "StropheError", "send() should throw exception"); |
---|
| 96 | } |
---|
| 97 | }); |
---|
| 98 | |
---|
| 99 | test("Builder with XML attribute escaping test", function () { |
---|
| 100 | var text = "<b>"; |
---|
| 101 | var expected = "<presence to='<b>' xmlns='jabber:client'/>"; |
---|
| 102 | var pres = $pres({to: text}); |
---|
| 103 | equals(pres.toString(), expected, "< should be escaped"); |
---|
| 104 | |
---|
| 105 | text = "foo&bar"; |
---|
| 106 | expected = "<presence to='foo&bar' xmlns='jabber:client'/>"; |
---|
| 107 | pres = $pres({to: text}); |
---|
| 108 | equals(pres.toString(), expected, "& should be escaped"); |
---|
| 109 | }); |
---|
| 110 | |
---|
| 111 | module("XML"); |
---|
| 112 | |
---|
| 113 | test("XML escaping test", function () { |
---|
| 114 | var text = "s & p"; |
---|
| 115 | var textNode = Strophe.xmlTextNode(text); |
---|
| 116 | equals(Strophe.getText(textNode), "s & p", "should be escaped"); |
---|
| 117 | var text0 = "s < & > p"; |
---|
| 118 | var textNode0 = Strophe.xmlTextNode(text0); |
---|
| 119 | equals(Strophe.getText(textNode0), "s < & > p", "should be escaped"); |
---|
| 120 | }); |
---|
| 121 | |
---|
| 122 | test("XML element creation", function () { |
---|
| 123 | var elem = Strophe.xmlElement("message"); |
---|
| 124 | equals(elem.tagName, "message", "Element name should be the same"); |
---|
| 125 | }); |
---|
| 126 | |
---|
| 127 | module("Misc"); |
---|
| 128 | |
---|
| 129 | test("Quoting strings", function () { |
---|
| 130 | var input = '"beep \\40"'; |
---|
| 131 | var conn = new Strophe.Connection(); |
---|
| 132 | var output = conn._quote(input); |
---|
| 133 | equals(output, "\"\\\"beep \\\\40\\\"\"", |
---|
| 134 | "string should be quoted and escaped"); |
---|
| 135 | }); |
---|
| 136 | } |
---|