Skip to content

Commit 8e91807

Browse files
Add utility to conditionally send a request to the server
1 parent 9b1fd50 commit 8e91807

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

test/encoding.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
</head>
1212
<body>
1313
<div id="qunit"></div>
14-
<div id="qunit-fixture"></div>
14+
<div id="qunit-fixture">
15+
<iframe id="request_target"></iframe>
16+
</div>
1517
</body>
1618
</html>

test/tests.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ QUnit.test('malformed cookie value in IE', function (assert) {
4848
var done = assert.async();
4949
// Sandbox in an iframe so that we can poke around with document.cookie.
5050
var iframe = document.createElement('iframe');
51-
var addEvent = function (element, eventName, fn) {
52-
var method = 'addEventListener';
53-
if (element.attachEvent) {
54-
eventName = 'on' + eventName;
55-
method = 'attachEvent';
56-
}
57-
element[ method ](eventName, fn);
58-
};
5951
iframe.src = 'malformed_cookie.html';
6052
addEvent(iframe, 'load', function () {
6153
if (iframe.contentWindow.ok) {

test/utils.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,61 @@
4444
});
4545
}
4646
};
47+
48+
window.addEvent = function (element, eventName, fn) {
49+
var method = 'addEventListener';
50+
if (element.attachEvent) {
51+
eventName = 'on' + eventName;
52+
method = 'attachEvent';
53+
}
54+
element[ method ](eventName, fn);
55+
};
56+
57+
window.using = function( assert ) {
58+
function getQuery(key) {
59+
var queries = location.href.split('?')[1];
60+
if (!queries) {
61+
return;
62+
}
63+
var pairs = queries.split( /&|=/ );
64+
var indexBaseURL = pairs.indexOf(key);
65+
var result = pairs[indexBaseURL + 1];
66+
if (result) {
67+
return decodeURIComponent(result);
68+
}
69+
}
70+
function setCookie(name, value) {
71+
return {
72+
then: function(callback) {
73+
var iframe = document.getElementById('request_target');
74+
var serverURL = getQuery('integration_baseurl');
75+
Cookies.set(name, value);
76+
if (!serverURL) {
77+
callback(Cookies.get(name));
78+
} else {
79+
var requestURL = serverURL + '/encoding/' + name;
80+
var done = assert.async();
81+
addEvent(iframe, 'load', function () {
82+
var content = iframe.contentWindow.document.innerHTML;
83+
if ( !content ) {
84+
ok(false, [
85+
'"' + requestURL + '"',
86+
'should return an object literal in the content body',
87+
'with name and value keys'
88+
].join(' '));
89+
return;
90+
}
91+
var result = JSON.parse(content);
92+
callback(result[name]);
93+
done();
94+
});
95+
iframe.src = requestURL;
96+
}
97+
}
98+
};
99+
}
100+
return {
101+
setCookie: setCookie
102+
};
103+
};
47104
}());

0 commit comments

Comments
 (0)