-
Notifications
You must be signed in to change notification settings - Fork 61
/
connectionManagement.spec.js
302 lines (247 loc) · 8.03 KB
/
connectionManagement.spec.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
import { CHILD_SERVER, CHILD_SERVER_ALTERNATE } from './constants';
import { createAndAddIframe } from './utils';
/**
* Asserts that no connection is successfully made between the parent and the
* child.
*/
const expectNoSuccessfulConnection = (connectionPromise, iframe) => {
const spy = jasmine.createSpy();
connectionPromise.then(spy);
return new Promise((resolve) => {
iframe.addEventListener('load', function () {
// Give Penpal time to try to make a handshake.
setTimeout(() => {
expect(spy).not.toHaveBeenCalled();
resolve();
}, 100);
});
});
};
describe('connection management', () => {
it('connects to iframe when correct child origin provided', async () => {
const iframe = createAndAddIframe();
const connection = Penpal.connectToChild({
debug: true,
iframe,
childOrigin: CHILD_SERVER,
});
// We're setting src after calling connectToChild to ensure
// that we don't throw an error in such a case. src is only
// needed when childOrigin is not passed.
iframe.src = `${CHILD_SERVER}/default.html`;
await connection.promise;
});
it('connects to iframe connecting to parent with matching origin', async () => {
const iframe = createAndAddIframe();
iframe.src = `${CHILD_SERVER}/matchingParentOrigin.html`;
const connection = Penpal.connectToChild({
debug: true,
iframe,
});
await connection.promise;
});
it('connects to iframe connecting to parent with matching origin regex', async () => {
const iframe = createAndAddIframe();
iframe.src = `${CHILD_SERVER}/matchingParentOriginRegex.html`;
const connection = Penpal.connectToChild({
debug: true,
iframe,
});
await connection.promise;
});
it("doesn't connect to iframe when incorrect child origin provided", async () => {
const iframe = createAndAddIframe();
const connection = Penpal.connectToChild({
debug: true,
iframe,
childOrigin: 'http://bogus.com',
});
// We're setting src after calling connectToChild to ensure
// that we don't throw an error in such a case. src is only
// needed when childOrigin is not passed.
iframe.src = `${CHILD_SERVER}/default.html`;
await expectNoSuccessfulConnection(connection.promise, iframe);
});
it("doesn't connect to iframe connecting to mismatched parent origin", async () => {
const iframe = createAndAddIframe(
`${CHILD_SERVER}/mismatchedParentOrigin.html`
);
const connection = Penpal.connectToChild({
iframe,
});
await expectNoSuccessfulConnection(connection.promise, iframe);
});
it("doesn't connect to iframe connecting to mismatched parent origin regex", async () => {
const iframe = createAndAddIframe(
`${CHILD_SERVER}/mismatchedParentOriginRegex.html`
);
const connection = Penpal.connectToChild({
iframe,
});
await expectNoSuccessfulConnection(connection.promise, iframe);
});
it('connects to iframe when child redirects to different origin and child origin is set to *', async () => {
const redirectToUrl = encodeURIComponent(
`${CHILD_SERVER_ALTERNATE}/default.html`
);
const iframe = createAndAddIframe(
`${CHILD_SERVER}/redirect.html?to=${redirectToUrl}`
);
const connection = Penpal.connectToChild({
debug: true,
iframe,
childOrigin: '*',
});
await connection.promise;
});
it("doesn't connect to iframe when child redirects to different origin and child origin is not set", async () => {
const redirectToUrl = encodeURIComponent(
`${CHILD_SERVER_ALTERNATE}/default.html`
);
const iframe = createAndAddIframe(
`${CHILD_SERVER}/redirect.html?to=${redirectToUrl}`
);
const connection = Penpal.connectToChild({
debug: true,
iframe,
});
await expectNoSuccessfulConnection(connection.promise, iframe);
});
it('reconnects after child reloads', (done) => {
const connection = Penpal.connectToChild({
iframe: createAndAddIframe(`${CHILD_SERVER}/default.html`),
});
connection.promise.then((child) => {
const previousMultiply = child.multiply;
const intervalId = setInterval(function () {
// Detect reconnection
if (child.multiply !== previousMultiply) {
clearInterval(intervalId);
child.multiply(2, 4).then((value) => {
expect(value).toEqual(8);
connection.destroy();
done();
});
}
}, 10);
child.reload();
});
});
// Issue #18
it('properly disconnects previous call receiver upon reconnection', (done) => {
const add = jasmine.createSpy().and.callFake((num1, num2) => {
return num1 + num2;
});
const connection = Penpal.connectToChild({
iframe: createAndAddIframe(`${CHILD_SERVER}/default.html`),
methods: {
add,
},
});
connection.promise.then((child) => {
const previousAddUsingParent = child.addUsingParent;
const intervalId = setInterval(function () {
// Detect reconnection
if (child.addUsingParent !== previousAddUsingParent) {
clearInterval(intervalId);
child.addUsingParent().then(() => {
expect(add.calls.count()).toEqual(1);
connection.destroy();
done();
});
}
}, 10);
child.reload();
});
});
it('reconnects after child navigates to other page with different methods', (done) => {
const connection = Penpal.connectToChild({
iframe: createAndAddIframe(`${CHILD_SERVER}/default.html`),
});
connection.promise.then((child) => {
const intervalId = setInterval(function () {
// Detect reconnection
if (child.divide) {
clearInterval(intervalId);
expect(child.multiply).not.toBeDefined();
child.divide(6, 3).then((value) => {
expect(value).toEqual(2);
connection.destroy();
done();
});
}
}, 10);
child.navigate();
});
});
it('rejects promise if connectToChild times out', async () => {
const connection = Penpal.connectToChild({
iframe: createAndAddIframe(
'http://www.fakeresponse.com/api/?sleep=10000'
),
timeout: 0,
});
let error;
try {
await connection.promise;
} catch (e) {
error = e;
}
expect(error).toEqual(jasmine.any(Error));
expect(error.message).toBe('Connection timed out after 0ms');
expect(error.code).toBe(Penpal.ErrorCode.ConnectionTimeout);
});
it(
"doesn't destroy connection if connection succeeds then " +
'timeout passes (connectToChild)',
async () => {
jasmine.clock().install();
const iframe = createAndAddIframe(`${CHILD_SERVER}/default.html`);
const connection = Penpal.connectToChild({
iframe,
timeout: 100000,
});
await connection.promise;
jasmine.clock().tick(100001);
expect(iframe.parentNode).not.toBeNull();
jasmine.clock().uninstall();
connection.destroy();
}
);
it(
"doesn't destroy connection if connection succeeds then " +
'timeout passes (connectToParent)',
(done) => {
var connection = Penpal.connectToChild({
iframe: createAndAddIframe(`${CHILD_SERVER}/timeout.html`),
methods: {
reportStillConnected() {
connection.destroy();
done();
},
},
});
}
);
it(
'destroys connection if iframe has been removed from DOM ' +
'and method is called',
async () => {
const iframe = createAndAddIframe(`${CHILD_SERVER}/default.html`);
var connection = Penpal.connectToChild({
iframe,
appendTo: document.body,
});
const child = await connection.promise;
document.body.removeChild(iframe);
let error;
try {
child.multiply(2, 3);
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.code).toBe(Penpal.ErrorCode.ConnectionDestroyed);
}
);
});