|
@@ -0,0 +1,193 @@
|
|
1
|
+import { default as XmppConnection } from './XmppConnection';
|
|
2
|
+import { $iq, Strophe } from 'strophe.js';
|
|
3
|
+import { nextTick } from '../util/TestUtils';
|
|
4
|
+
|
|
5
|
+/**
|
|
6
|
+ * Mock Strophe connection.
|
|
7
|
+ */
|
|
8
|
+class MockStropheConnection {
|
|
9
|
+ /**
|
|
10
|
+ * XMPP service URL.
|
|
11
|
+ *
|
|
12
|
+ * @returns {string}
|
|
13
|
+ */
|
|
14
|
+ get service() {
|
|
15
|
+ return 'wss://localhost/xmpp-websocket';
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ /**
|
|
19
|
+ * {@see Strophe.Connection.connect}
|
|
20
|
+ */
|
|
21
|
+ connect(jid, pass, callback) {
|
|
22
|
+ this._connectCb = callback;
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ /**
|
|
26
|
+ * {@see Strophe.Connection.disconnect}
|
|
27
|
+ */
|
|
28
|
+ disconnect() {
|
|
29
|
+ this.simulateConnectionState(Strophe.Status.DISCONNECTING);
|
|
30
|
+ this.simulateConnectionState(Strophe.Status.DISCONNECTED);
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ /**
|
|
34
|
+ * Simulates transition to the new connection status.
|
|
35
|
+ *
|
|
36
|
+ * @param {Strophe.Status} newState - The new connection status to set.
|
|
37
|
+ * @returns {void}
|
|
38
|
+ */
|
|
39
|
+ simulateConnectionState(newState) {
|
|
40
|
+ this._connectCb(newState);
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ /**
|
|
44
|
+ * {@see Strophe.Connection.sendIQ}.
|
|
45
|
+ */
|
|
46
|
+ sendIQ(iq, resultCb) {
|
|
47
|
+ resultCb();
|
|
48
|
+ }
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+/**
|
|
52
|
+ * Creates any IQ.
|
|
53
|
+ * @returns {Element}
|
|
54
|
+ */
|
|
55
|
+function testIQ() {
|
|
56
|
+ return $iq({
|
|
57
|
+ to: 'remoteJid',
|
|
58
|
+ type: 'set'
|
|
59
|
+ })
|
|
60
|
+ .c('jingle', { xmlns: 'urn:xmpp:jingle:1',
|
|
61
|
+ action: 'session-info',
|
|
62
|
+ initiator: 'blabla',
|
|
63
|
+ sid: '1234' })
|
|
64
|
+ .up();
|
|
65
|
+}
|
|
66
|
+
|
|
67
|
+describe('XmppConnection', () => {
|
|
68
|
+ let connection;
|
|
69
|
+ let mockStropheConnection;
|
|
70
|
+ let sendIQSpy;
|
|
71
|
+
|
|
72
|
+ beforeEach(() => {
|
|
73
|
+ jasmine.clock().install();
|
|
74
|
+
|
|
75
|
+ spyOn(Strophe, 'Connection').and.callFake((...args) => {
|
|
76
|
+ mockStropheConnection = new MockStropheConnection(...args);
|
|
77
|
+
|
|
78
|
+ return mockStropheConnection;
|
|
79
|
+ });
|
|
80
|
+
|
|
81
|
+ connection = new XmppConnection({
|
|
82
|
+ serviceUrl: 'wss://localhost/xmpp-websocket'
|
|
83
|
+ });
|
|
84
|
+
|
|
85
|
+ sendIQSpy = spyOn(mockStropheConnection, 'sendIQ').and.callThrough();
|
|
86
|
+
|
|
87
|
+ // eslint-disable-next-line no-empty-function
|
|
88
|
+ connection.connect('jid', undefined, () => { });
|
|
89
|
+ });
|
|
90
|
+
|
|
91
|
+ afterEach(() => {
|
|
92
|
+ jasmine.clock().uninstall();
|
|
93
|
+ });
|
|
94
|
+ describe('sendIQ2', () => {
|
|
95
|
+ it('will send the IQ immediately if connected', () => {
|
|
96
|
+ mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTED);
|
|
97
|
+
|
|
98
|
+ return connection.sendIQ2(testIQ(), { timeout: 15000 })
|
|
99
|
+ .then(() => {
|
|
100
|
+ expect(sendIQSpy).toHaveBeenCalled();
|
|
101
|
+ });
|
|
102
|
+ });
|
|
103
|
+ it('will send the IQ on reconnect', () => {
|
|
104
|
+ mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
|
|
105
|
+
|
|
106
|
+ let resolved = false;
|
|
107
|
+
|
|
108
|
+ connection
|
|
109
|
+ .sendIQ2(testIQ(), { timeout: 15000 })
|
|
110
|
+ .then(() => {
|
|
111
|
+ resolved = true;
|
|
112
|
+ });
|
|
113
|
+
|
|
114
|
+ jasmine.clock().tick(10000);
|
|
115
|
+
|
|
116
|
+ return nextTick()
|
|
117
|
+ .then(() => {
|
|
118
|
+ expect(resolved).toBe(false);
|
|
119
|
+ expect(sendIQSpy).not.toHaveBeenCalled();
|
|
120
|
+
|
|
121
|
+ mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTED);
|
|
122
|
+
|
|
123
|
+ return nextTick();
|
|
124
|
+ })
|
|
125
|
+ .then(() => {
|
|
126
|
+ expect(resolved).toBe(true);
|
|
127
|
+ expect(sendIQSpy).toHaveBeenCalled();
|
|
128
|
+ });
|
|
129
|
+ });
|
|
130
|
+ it('will timeout the operation if not connected in time', () => {
|
|
131
|
+ mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
|
|
132
|
+
|
|
133
|
+ let rejected = false, resolved = false;
|
|
134
|
+
|
|
135
|
+ connection
|
|
136
|
+ .sendIQ2(testIQ(), { timeout: 15000 })
|
|
137
|
+ .then(() => {
|
|
138
|
+ resolved = true;
|
|
139
|
+ }, () => {
|
|
140
|
+ rejected = true;
|
|
141
|
+ });
|
|
142
|
+
|
|
143
|
+ jasmine.clock().tick(10000);
|
|
144
|
+
|
|
145
|
+ return nextTick()
|
|
146
|
+ .then(() => {
|
|
147
|
+ expect(sendIQSpy).not.toHaveBeenCalled();
|
|
148
|
+ expect(resolved).toBe(false);
|
|
149
|
+ expect(rejected).toBe(false);
|
|
150
|
+
|
|
151
|
+ jasmine.clock().tick(10000);
|
|
152
|
+
|
|
153
|
+ return nextTick();
|
|
154
|
+ })
|
|
155
|
+ .then(() => {
|
|
156
|
+ expect(sendIQSpy).not.toHaveBeenCalled();
|
|
157
|
+ expect(resolved).toBe(false);
|
|
158
|
+ expect(rejected).toBe(true);
|
|
159
|
+ });
|
|
160
|
+ });
|
|
161
|
+ it('will reject the promise on explicit disconnect', () => {
|
|
162
|
+ mockStropheConnection.simulateConnectionState(Strophe.Status.CONNECTING);
|
|
163
|
+
|
|
164
|
+ let rejected = false, resolved = false;
|
|
165
|
+
|
|
166
|
+ connection
|
|
167
|
+ .sendIQ2(testIQ(), { timeout: 15000 })
|
|
168
|
+ .then(() => {
|
|
169
|
+ resolved = true;
|
|
170
|
+ }, error => {
|
|
171
|
+ rejected = error;
|
|
172
|
+ });
|
|
173
|
+
|
|
174
|
+ jasmine.clock().tick(10000);
|
|
175
|
+
|
|
176
|
+ return nextTick()
|
|
177
|
+ .then(() => {
|
|
178
|
+ expect(sendIQSpy).not.toHaveBeenCalled();
|
|
179
|
+ expect(resolved).toBe(false);
|
|
180
|
+ expect(rejected).toBe(false);
|
|
181
|
+
|
|
182
|
+ connection.disconnect();
|
|
183
|
+
|
|
184
|
+ return nextTick();
|
|
185
|
+ })
|
|
186
|
+ .then(() => {
|
|
187
|
+ expect(sendIQSpy).not.toHaveBeenCalled();
|
|
188
|
+ expect(resolved).toBe(false);
|
|
189
|
+ expect(rejected).toEqual(new Error('disconnect'));
|
|
190
|
+ });
|
|
191
|
+ });
|
|
192
|
+ });
|
|
193
|
+});
|