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

Commit 8484c83

Browse files
author
Daniel Brain
committed
More tests
1 parent eb836af commit 8484c83

File tree

12 files changed

+236
-40
lines changed

12 files changed

+236
-40
lines changed

karma.conf.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module.exports = function(config) {
1818
// list of files / patterns to load in the browser
1919
files: [
2020
{ pattern: 'test/test.js', included: true, served: true },
21-
{ pattern: 'test/child.js', included: false, served: true },
22-
{ pattern: 'test/child.htm', included: false, served: true }
21+
{ pattern: 'test/*.js', included: false, served: true },
22+
{ pattern: 'test/*.htm', included: false, served: true },
2323
],
2424

2525
plugins: [
@@ -73,7 +73,7 @@ module.exports = function(config) {
7373
postLoaders: [
7474
{
7575
test: /\.js$/,
76-
exclude: /(node_modules|bower_components|dist)/,
76+
exclude: /(node_modules|bower_components|dist|test)/,
7777
loader: 'istanbul-instrumenter'
7878
}
7979
]

src/conf/config.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11

2-
2+
import { CONSTANTS } from './constants';
33

44
export let CONFIG = {
55

66
ALLOW_POSTMESSAGE_POPUP: true,
77

88
DEBUG: false,
99

10-
ACK_TIMEOUT: 3000,
10+
ACK_TIMEOUT: 1000,
1111

1212
LOG_TO_PAGE: false,
1313

14-
MOCK_MODE: false
14+
MOCK_MODE: false,
15+
16+
ALLOWED_POST_MESSAGE_METHODS: {
17+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE ]: true,
18+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE_GLOBAL_METHOD ]: true,
19+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE_UP_THROUGH_BRIDGE ]: true,
20+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE_DOWN_THROUGH_BRIDGE ]: true
21+
}
1522
};

src/conf/constants.js

+7
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,12 @@ export let CONSTANTS = {
2929

3030
SERIALIZATION_TYPES: {
3131
METHOD: 'postrobot_method'
32+
},
33+
34+
SEND_STRATEGIES: {
35+
POST_MESSAGE: 'postrobot_post_message',
36+
POST_MESSAGE_GLOBAL_METHOD: 'postrobot_post_message_global_method',
37+
POST_MESSAGE_UP_THROUGH_BRIDGE: 'postrobot_post_message_up_through_bridge',
38+
POST_MESSAGE_DOWN_THROUGH_BRIDGE: 'postrobot_post_message_down_through_bridge'
3239
}
3340
};

src/drivers/send/index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,25 @@ export let sendMessage = promise.method((win, message, domain, isProxy) => {
4040

4141
util.debug('Running send message strategies', message);
4242

43-
return promise.Promise.all(util.map(util.keys(SEND_MESSAGE_STRATEGIES), strategyName => {
43+
return promise.map(util.keys(SEND_MESSAGE_STRATEGIES), strategyName => {
4444

45-
return SEND_MESSAGE_STRATEGIES[strategyName](win, message, domain).then(() => {
45+
return promise.run(() => {
46+
47+
if (!CONFIG.ALLOWED_POST_MESSAGE_METHODS[strategyName]) {
48+
throw new Error(`Strategy disallowed: ${strategyName}`);
49+
}
50+
51+
return SEND_MESSAGE_STRATEGIES[strategyName](win, message, domain);
52+
53+
}).then(() => {
4654
util.debug(strategyName, 'success');
4755
return true;
4856
}, err => {
4957
util.debugError(strategyName, 'error\n\n', err.stack || err.toString());
5058
return false;
5159
});
5260

53-
})).then(results => {
61+
}).then(results => {
5462

5563
if (!util.some(results)) {
5664
throw new Error('No post-message strategy succeeded');

src/drivers/send/strategies.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import { emulateIERestrictions, getBridge, getBridgeFor } from '../../compat';
55

66
export let SEND_MESSAGE_STRATEGIES = {
77

8-
POST_MESSAGE: promise.method((win, message, domain) => {
8+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE ]: promise.method((win, message, domain) => {
99

1010
emulateIERestrictions(window, win);
1111

1212
return win.postMessage(JSON.stringify(message, 0, 2), domain);
1313
}),
1414

15-
POST_MESSAGE_GLOBAL_METHOD: promise.method((win, message, domain) => {
15+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE_GLOBAL_METHOD ]: promise.method((win, message, domain) => {
1616

1717
if (domain !== '*') {
1818

@@ -44,7 +44,7 @@ export let SEND_MESSAGE_STRATEGIES = {
4444
});
4545
}),
4646

47-
POST_MESSAGE_UP_THROUGH_BRIDGE: promise.method((win, message, domain) => {
47+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE_UP_THROUGH_BRIDGE ]: promise.method((win, message, domain) => {
4848

4949
let frame = getBridgeFor(win);
5050

@@ -59,7 +59,7 @@ export let SEND_MESSAGE_STRATEGIES = {
5959
return frame[CONSTANTS.WINDOW_PROPS.POSTROBOT].postMessageParent(window, JSON.stringify(message, 0, 2), domain);
6060
}),
6161

62-
POST_MESSAGE_DOWN_THROUGH_BRIDGE: promise.method((win, message, domain) => {
62+
[ CONSTANTS.SEND_STRATEGIES.POST_MESSAGE_DOWN_THROUGH_BRIDGE ]: promise.method((win, message, domain) => {
6363

6464
let bridge = getBridge();
6565

src/interface/config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ export function enableMockMode() {
77

88
export function disableMockMode() {
99
CONFIG.MOCK_MODE = false;
10-
}
10+
}
11+
12+
export { CONFIG, CONSTANTS } from '../conf';

src/interface/server.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function listen(options) {
2020
if (options.once) {
2121
let handler = options.handler;
2222
options.handler = util.once(function() {
23-
removeRequestListener(options)
23+
removeRequestListener(options);
2424
return handler.apply(this, arguments);
2525
});
2626
}

src/lib/promise.js

+11
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,16 @@ export let promise = {
4747
return reject(err);
4848
}
4949
});
50+
},
51+
52+
map(items, method) {
53+
54+
let results = [];
55+
for (let i = 0; i < items.length; i++) {
56+
results.push(promise.run(() => {
57+
return method(items[i]);
58+
}));
59+
}
60+
return promise.Promise.all(results);
5061
}
5162
};

test/bridge.htm

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

test/child.htm

+5-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<script src="/base/test/child.js"></script>
1+
<script src="/base/test/child.js"></script>
2+
3+
<script>
4+
postRobot.openBridge('/base/test/child.htm');
5+
</script>

test/child.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ postRobot.on('sendMessageToParent', function(source, data) {
1313

1414
postRobot.on('setupListener', function(source, data) {
1515
postRobot.once(data.messageName, function() {
16-
return data.data;
16+
return data.handler ? data.handler() : data.data;
1717
});
1818
});
1919

2020
postRobot.on('waitForMessage', function(source, data) {
2121
return postRobot.once(data.messageName, function() {
22-
return data.data;
22+
return data.handler ? data.handler() : data.data;
2323
});
2424
});

0 commit comments

Comments
 (0)