tests.js
6.36 KB
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
$(document).ready(function () {
module("JIDs");
test("Normal JID", function () {
var jid = "darcy@pemberley.lit/library";
equals(Strophe.getNodeFromJid(jid), "darcy",
"Node should be 'darcy'");
equals(Strophe.getDomainFromJid(jid), "pemberley.lit",
"Domain should be 'pemberley.lit'");
equals(Strophe.getResourceFromJid(jid), "library",
"Node should be 'library'");
equals(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";
equals(Strophe.getNodeFromJid(jid), "darcy",
"Node should be 'darcy'");
equals(Strophe.getDomainFromJid(jid),
"netherfield.lit@pemberley.lit",
"Domain should be 'netherfield.lit@pemberley.lit'");
equals(Strophe.getResourceFromJid(jid), "library",
"Resource should be 'library'");
equals(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";
equals(Strophe.getNodeFromJid(jid), "darcy\\40netherfield.lit",
"Node should be 'darcy\\40netherfield.lit'");
equals(Strophe.getDomainFromJid(jid),
"pemberley.lit",
"Domain should be 'pemberley.lit'");
equals(Strophe.getResourceFromJid(jid), "library",
"Resource should be 'library'");
equals(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";
equals(Strophe.getNodeFromJid(jid), "books",
"Node should be 'books'");
equals(Strophe.getDomainFromJid(jid), "chat.pemberley.lit",
"Domain should be 'chat.pemberley.lit'");
equals(Strophe.getResourceFromJid(jid),
"darcy@pemberley.lit/library",
"Resource should be 'darcy@pemberley.lit/library'");
equals(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 () {
equals($(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) {
equals(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});
equals(pres.toString(), expected, "< should be escaped");
text = "foo&bar";
expected = "<presence to='foo&bar' xmlns='jabber:client'/>";
pres = $pres({to: text});
equals(pres.toString(), expected, "& should be escaped");
});
module("XML");
test("XML escaping test", function () {
var text = "s & p";
var textNode = Strophe.xmlTextNode(text);
equals(Strophe.getText(textNode), "s & p", "should be escaped");
var text0 = "s < & > p";
var textNode0 = Strophe.xmlTextNode(text0);
equals(Strophe.getText(textNode0), "s < & > p", "should be escaped");
});
test("XML element creation", function () {
var elem = Strophe.xmlElement("message");
equals(elem.tagName, "message", "Element name should be the same");
});
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');
equals(hand.isMatch(elem), true, "Full JID should match");
hand = new Strophe.Handler(null, null, null, null, null,
'darcy@pemberley.lit')
equals(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});
equals(hand.isMatch(elem), true, "Full JID should match");
hand = new Strophe.Handler(null, null, null, null, null,
'darcy@pemberley.lit',
{matchBare: true});
equals(hand.isMatch(elem), true, "Bare JID should match");
});
module("Misc");
test("Quoting strings", function () {
var input = '"beep \\40"';
var conn = new Strophe.Connection();
var output = conn._quote(input);
equals(output, "\"\\\"beep \\\\40\\\"\"",
"string should be quoted and escaped");
});
});