forked from sproutcore/sproutcore
/
request.js
223 lines (165 loc) · 6.3 KB
/
request.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
// ========================================================================
// SC.Request Base Tests
// ========================================================================
/*globals module test ok isObj equals expects */
var url, request, contents ;
module("SC.Request", {
setup: function() {
url = sc_static("file_exists.json"); //"/static/sproutcore/en/desktop/_src/desktop.platform/english.lproj/file_exists.json" ;
request = SC.Request.getUrl(url) ;
contents = null ;
},
teardown: function() {
url = request = contents;
}
});
test("Basic Requirements", function() {
ok(SC.Request, "SC.Request is defined") ;
ok("" !== url, "url variable is not empty") ;
ok(request !== null, "request object is not null") ;
ok(contents === null, "contents is null" ) ;
});
test("Test Asynchronous GET Request", function() {
var response, timer;
timer = setTimeout(function() {
ok(false, 'response did not invoke notify() within 2sec');
window.start();
}, 2000);
request.notify(this, function(response) {
ok(SC.ok(response), 'response should not be an error');
equals(response.get('body'), '{"message": "Yay!"}', 'should match retrieved message');
clearTimeout(timer);
window.start();
});
response = request.send();
ok(response !== null, 'request.send() should return a response object');
ok(response.get('status')<0, 'response should still not have a return code since this should be async');
stop() ; // stops the test runner - wait for response
});
test("Test Synchronous GET Request", function() {
request.set("isAsynchronous", NO);
var response = request.send();
ok(response !== null, 'send() should return response') ;
ok(SC.$ok(response), 'contents should not be an error ');
equals(response.get('body'), '{"message": "Yay!"}', 'should match retrieved message') ;
});
test("Test Asynchronous GET Request, auto-deserializing JSON", function() {
request.set("isJSON", YES);
var timer;
timer = setTimeout( function(){
ok(false, 'response did not invoke notify()');
window.start();
}, 1000);
request.notify(this, function(response) {
ok(SC.ok(response), 'response should not be error');
same(response.get('body'), {"message": "Yay!"}, 'repsonse.body');
clearTimeout(timer);
window.start();
});
request.send();
stop() ; // stops the test runner
});
test("Test auto-deserializing malformed JSON", function() {
request = SC.Request.getUrl(sc_static('malformed.json')).set('isJSON', YES);
var timer = setTimeout(function() {
ok(false, 'response did not invoke notify()');
window.start();
}, 1000);
request.notify(this, function(response) {
ok(SC.ok(response), 'response should not be error');
try {
var body = response.get('body');
ok(!SC.ok(body), 'body should be an error');
} catch(e) {
ok(false, 'getting the body should not throw an exception');
}
clearTimeout(timer);
window.start();
});
request.send();
stop();
});
test("Test Synchronous GET Request, auto-deserializing JSON", function() {
request.set("isAsynchronous", false);
request.set("isJSON", true);
var response = request.send();
ok(response !== null, 'response should not be null') ;
ok(SC.ok(response), 'contents should not be an error');
same(response.get('body'), {"message": "Yay!"}, 'contents should have message') ;
});
test("Test if Request body is being auto-serializing to JSON", function() {
var objectToPost={"content": "garbage"};
request.set("isJSON", true).set('body', objectToPost);
var jsonEncoded = request.get('encodedBody');
equals(jsonEncoded, '{"content":"garbage"}', "The json object passed in send should be encoded and set as the body");
});
test("Test Multiple Asynchronous GET Request - two immediate, and two in serial", function() {
var requestCount = 3;
var responseCount = 0;
var serialCount = 0;
var observer = function(response) {
responseCount++;
if (serialCount<=2) {
serialCount++;
SC.Request.getUrl(url).notify(this, observer).send();
requestCount++;
}
};
SC.Request.getUrl(url).notify(this, observer).send();
SC.Request.getUrl(url).notify(this, observer).send();
SC.Request.getUrl(url).notify(this, observer).send();
stop() ; // stops the test runner
setTimeout( function(){
equals(requestCount, 6, "requestCount should be 6");
equals(responseCount, 6, "responseCount should be 6");
window.start() ; // starts the test runner
}, 2000);
});
test("Test timeouts", function() {
var message;
// Sanity check 1
try {
SC.Request.getUrl(url).set('timeout', 0).send();
}
catch (e) {
message = e;
}
ok(message && message.indexOf("The timeout value must either not be specified or must be greater than 0") !== -1, 'An error should be thrown when the timeout value is 0 ms');
// Sanity check 2
try {
SC.Request.getUrl(url).set('isAsynchronous', NO).set('timeout', 10).send();
}
catch (e2) {
message = e2;
}
ok(message && message.indexOf("Timeout values cannot be used with synchronous requests") !== -1, 'An error should be thrown when trying to use a timeout with a synchronous request');
// Make sure timeouts actually fire, and fire when expected.
var changedBefore = NO,
changedAfter = NO,
timeoutRequest = SC.Request.getUrl("http://www.sproutcore.com"),
checkstop;
var now = Date.now();
// make the timeout as short as possible so that it will always happen
timeoutRequest.set('timeout', 10);
timeoutRequest.set('didTimeout', function() {
// at least timeout time must have elapsed
var elapsed = Date.now()-now;
ok(elapsed >= 10, 'timeout must not fire earlier than 10msec - actual %@'.fmt(elapsed));
// timeout did fire...just resume...
clearTimeout(checkstop);
window.start();
});
timeoutRequest.set('didReceive', function() {
ok(false, 'timeout did not fire before response was recieved. should have fired after 10msec. response time: %@msec'.fmt(Date.now() - now));
window.start(); // resume
});
SC.RunLoop.begin();
timeoutRequest.send();
SC.RunLoop.end();
stop() ; // stops the test runner
// in case nothing works
checkstop = setTimeout(function() {
ok(false, 'timeout did not fire at all');
window.start();
}, 500);
});