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