Skip to content
This repository was archived by the owner on Jan 31, 2025. It is now read-only.

Commit db28227

Browse files
author
Daniel Brain
committed
Improve mock domain logic
1 parent f394277 commit db28227

File tree

7 files changed

+32
-55
lines changed

7 files changed

+32
-55
lines changed

src/drivers/receive/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { CONFIG, CONSTANTS, POST_MESSAGE_NAMES_LIST } from '../../conf';
3-
import { getWindowById, registerWindow, deserializeMethods, log, getOpener, getWindowId, isWindowClosed, isSameDomain } from '../../lib';
3+
import { getWindowById, registerWindow, deserializeMethods, log, getOpener, getWindowId, isWindowClosed, isSameDomain, util } from '../../lib';
44
import { emulateIERestrictions, registerBridge } from '../../compat';
55

66
import { sendMessage } from '../send';
@@ -83,12 +83,20 @@ export function receiveMessage(event) {
8383

8484
let { source, origin, data } = event;
8585

86+
if (isSameDomain(source, false)) {
87+
origin = util.getDomain(source);
88+
}
89+
8690
let message = parseMessage(data);
8791

8892
if (!message) {
8993
return;
9094
}
9195

96+
if (message.sourceDomain.indexOf('mock://') === 0) {
97+
origin = message.sourceDomain;
98+
}
99+
92100
if (receivedMessages.indexOf(message.id) === -1) {
93101
receivedMessages.push(message.id);
94102
} else {

src/lib/util.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,15 @@ export let util = {
247247
return safeInterval;
248248
},
249249

250-
getDomain(win) {
250+
getDomain(win, allowMockDomain = true) {
251+
251252
win = win || window;
252-
return win.mockDomain || `${win.location.protocol}//${win.location.host}`;
253+
254+
if (win.mockDomain && allowMockDomain && win.mockDomain.indexOf('mock://') === 0) {
255+
return win.mockDomain;
256+
}
257+
258+
return `${win.location.protocol}//${win.location.host}`;
253259
},
254260

255261
getDomainFromUrl(url) {

src/lib/windows.js

+10-17
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,33 @@ function safeGet(obj, prop) {
1717
let domainMatches = [];
1818
let domainMatchTimeout;
1919

20-
export function isSameDomain(win) {
20+
export function isSameDomain(win, allowMockDomain = true) {
2121

2222
for (let match of domainMatches) {
2323
if (match.win === win) {
24-
25-
if (!match.match) {
26-
return false;
27-
}
28-
29-
match.match = false;
30-
31-
try {
32-
match.match = util.getDomain(window) === util.getDomain(win);
33-
} catch (err) {
34-
return;
35-
}
36-
37-
return match.match;
24+
return allowMockDomain ? match.match : match.actualMatch;
3825
}
3926
}
4027

4128
let match = false;
29+
let actualMatch = false;
4230

4331
try {
4432
if (util.getDomain(window) === util.getDomain(win)) {
4533
match = true;
4634
}
35+
36+
if (util.getDomain(window, false) === util.getDomain(win, false)) {
37+
actualMatch = true;
38+
}
4739
} catch (err) {
4840
// pass
4941
}
5042

5143
domainMatches.push({
5244
win,
53-
match
45+
match,
46+
actualMatch
5447
});
5548

5649
if (!domainMatchTimeout) {
@@ -60,7 +53,7 @@ export function isSameDomain(win) {
6053
}, 1);
6154
}
6255

63-
return match;
56+
return allowMockDomain ? match : actualMatch;
6457
}
6558

6659
export function isWindowClosed(win) {

test/bridge.htm

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script>
22
window.__coverage__ = window.parent.__coverage__;
3-
window.mockDomain = 'http://test-post-robot-child.com';
3+
window.mockDomain = 'mock://test-post-robot-child.com';
44
</script>
55

66
<script src="/base/test/child.js"></script>

test/child.htm

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
<script src="/base/test/child.js"></script>
77

88
<script>
9-
postRobot.openBridge('/base/test/bridge.htm', 'http://test-post-robot.com');
9+
postRobot.openBridge('/base/test/bridge.htm', 'mock://test-post-robot.com');
1010
</script>

test/common.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,4 @@ window.console.karma = function() {
33
var karma = window.karma || (window.top && window.top.karma) || (window.opener && window.opener.karma);
44
karma.log('debug', arguments);
55
console.log.apply(console, arguments);
6-
};
7-
8-
let addEventListener = window.addEventListener;
9-
10-
window.addEventListener = function(name, method) {
11-
return addEventListener.call(window, name, function(event) {
12-
let { origin, source, data } = event;
13-
14-
try {
15-
if (source.mockDomain) {
16-
origin = source.mockDomain;
17-
}
18-
} catch (err) {
19-
// pass
20-
}
21-
22-
return method({ origin, source, data });
23-
});
24-
}
6+
};

test/test.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import postRobot from 'src/index';
55
import { onWindowReady, promise } from 'src/lib';
66

77
postRobot.CONFIG.LOG_TO_PAGE = true;
8-
window.mockDomain = 'http://test-post-robot.com';
8+
window.mockDomain = 'mock://test-post-robot.com';
99

1010
function createIframe(name, callback) {
1111
var frame = document.createElement('iframe');
@@ -28,7 +28,7 @@ let bridge;
2828
let childWindow, childFrame, otherChildFrame;
2929

3030
before(function() {
31-
return postRobot.openBridge('/base/test/bridge.htm', 'http://test-post-robot-child.com').then(frame => {
31+
return postRobot.openBridge('/base/test/bridge.htm', 'mock://test-post-robot-child.com').then(frame => {
3232
bridge = frame;
3333
}).then(function() {
3434

@@ -335,18 +335,6 @@ describe('[post-robot] error cases', function() {
335335
});
336336
});
337337

338-
it.skip('should error out trying to set up a second bridge', function() {
339-
340-
try {
341-
postRobot.openBridge('/base/test/child.htm');
342-
} catch (err) {
343-
assert.ok(err);
344-
return;
345-
}
346-
347-
throw new Error('Expected opening second bridge to throw an error');
348-
});
349-
350338
it('should fail to send a message when the expected domain does not match', function() {
351339

352340
postRobot.on('foobu', { domain: 'http://www.zombo.com' }, function() {

0 commit comments

Comments
 (0)