| 1 |
var originalXMLHttpRequest; |
| 2 |
var describe = QUnit.module; |
| 3 |
var it = QUnit.test; |
| 4 |
|
| 5 |
describe('passthrough requests', function (config) { |
| 6 |
config.beforeEach(function () { |
| 7 |
originalXMLHttpRequest = window.XMLHttpRequest; |
| 8 |
this.pretender = new Pretender(); |
| 9 |
}); |
| 10 |
|
| 11 |
config.afterEach(function () { |
| 12 |
this.pretender.shutdown(); |
| 13 |
window.XMLHttpRequest = originalXMLHttpRequest; |
| 14 |
}); |
| 15 |
|
| 16 |
it('allows matched paths to be pass-through', function (assert) { |
| 17 |
var pretender = this.pretender; |
| 18 |
var done = assert.async(); |
| 19 |
|
| 20 |
pretender.post('/some/:route', pretender.passthrough); |
| 21 |
|
| 22 |
var passthroughInvoked = false; |
| 23 |
pretender.passthroughRequest = function (verb, path, request) { |
| 24 |
passthroughInvoked = true; |
| 25 |
assert.equal(verb, 'POST'); |
| 26 |
assert.equal(path, '/some/path'); |
| 27 |
assert.equal(request.requestBody, 'some=data'); |
| 28 |
}; |
| 29 |
|
| 30 |
$.ajax({ |
| 31 |
url: '/some/path', |
| 32 |
method: 'POST', |
| 33 |
headers: { |
| 34 |
'test-header': 'value', |
| 35 |
}, |
| 36 |
data: { |
| 37 |
some: 'data', |
| 38 |
}, |
| 39 |
error: function (xhr) { |
| 40 |
assert.equal(xhr.status, 404); |
| 41 |
assert.ok(passthroughInvoked); |
| 42 |
done(); |
| 43 |
}, |
| 44 |
}); |
| 45 |
}); |
| 46 |
|
| 47 |
it( |
| 48 |
'asynchronous request with pass-through has timeout,' + |
| 49 |
'withCredentials and onprogress event', |
| 50 |
function (assert) { |
| 51 |
var pretender = this.pretender; |
| 52 |
var done = assert.async(); |
| 53 |
|
| 54 |
function testXHR() { |
| 55 |
this.pretender = pretender; |
| 56 |
this.open = function () {}; |
| 57 |
this.setRequestHeader = function () {}; |
| 58 |
this.upload = {}; |
| 59 |
this.send = { |
| 60 |
pretender: pretender, |
| 61 |
apply: function (xhr /*, argument*/) { |
| 62 |
assert.ok('timeout' in xhr); |
| 63 |
assert.ok('withCredentials' in xhr); |
| 64 |
assert.ok('onprogress' in xhr); |
| 65 |
this.pretender.resolve(xhr); |
| 66 |
done(); |
| 67 |
}, |
| 68 |
}; |
| 69 |
} |
| 70 |
pretender._nativeXMLHttpRequest = testXHR; |
| 71 |
|
| 72 |
pretender.post('/some/path', pretender.passthrough); |
| 73 |
|
| 74 |
var xhr = new window.XMLHttpRequest(); |
| 75 |
xhr.open('POST', '/some/path'); |
| 76 |
xhr.timeout = 1000; |
| 77 |
xhr.withCredentials = true; |
| 78 |
xhr.send('some data'); |
| 79 |
} |
| 80 |
); |
| 81 |
|
| 82 |
it( |
| 83 |
'asynchronous request with pass-through and ' + |
| 84 |
'arraybuffer as responseType', |
| 85 |
function (assert) { |
| 86 |
var pretender = this.pretender; |
| 87 |
var done = assert.async(); |
| 88 |
|
| 89 |
function testXHR() { |
| 90 |
this.pretender = pretender; |
| 91 |
this.open = function () {}; |
| 92 |
this.setRequestHeader = function () {}; |
| 93 |
this.upload = {}; |
| 94 |
this.responseType = ''; |
| 95 |
this.send = { |
| 96 |
pretender: pretender, |
| 97 |
apply: function (xhr /*, argument*/) { |
| 98 |
assert.equal(xhr.responseType, 'arraybuffer'); |
| 99 |
this.pretender.resolve(xhr); |
| 100 |
done(); |
| 101 |
}, |
| 102 |
}; |
| 103 |
} |
| 104 |
pretender._nativeXMLHttpRequest = testXHR; |
| 105 |
|
| 106 |
pretender.get('/some/path', pretender.passthrough); |
| 107 |
|
| 108 |
var xhr = new window.XMLHttpRequest(); |
| 109 |
xhr.open('GET', '/some/path'); |
| 110 |
xhr.responseType = 'arraybuffer'; |
| 111 |
xhr.send(); |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
it('synchronous request has timeout=0, withCredentials and onprogress event', function (assert) { |
| 116 |
var pretender = this.pretender; |
| 117 |
var done = assert.async(); |
| 118 |
|
| 119 |
function testXHR() { |
| 120 |
this.open = function () {}; |
| 121 |
this.setRequestHeader = function () {}; |
| 122 |
this.upload = {}; |
| 123 |
this.send = { |
| 124 |
pretender: pretender, |
| 125 |
apply: function (xhr /*, argument*/) { |
| 126 |
assert.equal(xhr.timeout, 0); |
| 127 |
assert.ok(!('withCredentials' in xhr)); |
| 128 |
assert.ok(!('onprogress' in xhr)); |
| 129 |
this.pretender.resolve(xhr); |
| 130 |
done(); |
| 131 |
}, |
| 132 |
}; |
| 133 |
} |
| 134 |
Object.defineProperty(testXHR, 'timeout', { |
| 135 |
get() { |
| 136 |
return 0; |
| 137 |
}, |
| 138 |
set() { |
| 139 |
throw new Error( |
| 140 |
'Timeouts cannot be set for synchronous requests made from a document.' |
| 141 |
); |
| 142 |
}, |
| 143 |
}); |
| 144 |
pretender._nativeXMLHttpRequest = testXHR; |
| 145 |
|
| 146 |
pretender.post('/some/path', pretender.passthrough); |
| 147 |
|
| 148 |
var xhr = new window.XMLHttpRequest(); |
| 149 |
xhr.open('POST', '/some/path', false); |
| 150 |
xhr.withCredentials = true; |
| 151 |
xhr.send('some data'); |
| 152 |
}); |
| 153 |
|
| 154 |
it('synchronous request has timeout missing will set to 0', function (assert) { |
| 155 |
var pretender = this.pretender; |
| 156 |
var done = assert.async(); |
| 157 |
|
| 158 |
function testXHR() { |
| 159 |
this.open = function () {}; |
| 160 |
this.setRequestHeader = function () {}; |
| 161 |
this.upload = {}; |
| 162 |
this.send = { |
| 163 |
pretender: pretender, |
| 164 |
apply: function (xhr /*, argument*/) { |
| 165 |
assert.equal(xhr.timeout, 0); |
| 166 |
assert.ok(!('withCredentials' in xhr)); |
| 167 |
assert.ok(!('onprogress' in xhr)); |
| 168 |
this.pretender.resolve(xhr); |
| 169 |
done(); |
| 170 |
}, |
| 171 |
}; |
| 172 |
} |
| 173 |
pretender._nativeXMLHttpRequest = testXHR; |
| 174 |
|
| 175 |
pretender.post('/some/path', pretender.passthrough); |
| 176 |
|
| 177 |
var xhr = new window.XMLHttpRequest(); |
| 178 |
xhr.open('POST', '/some/path', false); |
| 179 |
xhr.withCredentials = true; |
| 180 |
xhr.send('some data'); |
| 181 |
}); |
| 182 |
|
| 183 |
it('asynchronous request fires events', function (assert) { |
| 184 |
assert.expect(6); |
| 185 |
|
| 186 |
var pretender = this.pretender; |
| 187 |
var done = assert.async(); |
| 188 |
|
| 189 |
pretender.post('/some/:route', pretender.passthrough); |
| 190 |
|
| 191 |
var onEvents = { |
| 192 |
load: false, |
| 193 |
progress: false, |
| 194 |
readystatechange: false, |
| 195 |
}; |
| 196 |
var listenerEvents = { |
| 197 |
load: false, |
| 198 |
progress: false, |
| 199 |
readystatechange: false, |
| 200 |
}; |
| 201 |
|
| 202 |
var xhr = new window.XMLHttpRequest(); |
| 203 |
xhr.open('POST', '/some/otherpath'); |
| 204 |
|
| 205 |
xhr.addEventListener('progress', function _progress() { |
| 206 |
listenerEvents.progress = true; |
| 207 |
}); |
| 208 |
|
| 209 |
xhr.onprogress = function _onprogress() { |
| 210 |
onEvents.progress = true; |
| 211 |
}; |
| 212 |
|
| 213 |
xhr.addEventListener('load', function _load() { |
| 214 |
listenerEvents.load = true; |
| 215 |
finishNext(); |
| 216 |
}); |
| 217 |
|
| 218 |
xhr.onload = function _onload() { |
| 219 |
onEvents.load = true; |
| 220 |
finishNext(); |
| 221 |
}; |
| 222 |
|
| 223 |
xhr.addEventListener('readystatechange', function _load() { |
| 224 |
if (xhr.readyState == 4) { |
| 225 |
listenerEvents.readystatechange = true; |
| 226 |
finishNext(); |
| 227 |
} |
| 228 |
}); |
| 229 |
|
| 230 |
xhr.onreadystatechange = function _onload() { |
| 231 |
if (xhr.readyState == 4) { |
| 232 |
onEvents.readystatechange = true; |
| 233 |
finishNext(); |
| 234 |
} |
| 235 |
}; |
| 236 |
|
| 237 |
xhr.send(); |
| 238 |
|
| 239 |
// call `finish` in next tick to ensure both load event handlers |
| 240 |
// have a chance to fire. |
| 241 |
function finishNext() { |
| 242 |
setTimeout(finishOnce, 1); |
| 243 |
} |
| 244 |
|
| 245 |
var finished = false; |
| 246 |
function finishOnce() { |
| 247 |
if (!finished) { |
| 248 |
finished = true; |
| 249 |
|
| 250 |
assert.ok(onEvents.load, 'onload called'); |
| 251 |
assert.ok(onEvents.progress, 'onprogress called'); |
| 252 |
assert.ok(onEvents.readystatechange, 'onreadystate called'); |
| 253 |
|
| 254 |
assert.ok(listenerEvents.load, 'load listener called'); |
| 255 |
assert.ok(listenerEvents.progress, 'progress listener called'); |
| 256 |
assert.ok( |
| 257 |
listenerEvents.readystatechange, |
| 258 |
'readystate listener called' |
| 259 |
); |
| 260 |
|
| 261 |
done(); |
| 262 |
} |
| 263 |
} |
| 264 |
}); |
| 265 |
|
| 266 |
it('asynchronous request fires upload progress events', function (assert) { |
| 267 |
assert.expect(2); |
| 268 |
|
| 269 |
var pretender = this.pretender; |
| 270 |
var done = assert.async(); |
| 271 |
|
| 272 |
pretender.post('/some/:route', pretender.passthrough); |
| 273 |
|
| 274 |
var onEvents = { |
| 275 |
progress: false, |
| 276 |
}; |
| 277 |
var listenerEvents = { |
| 278 |
progress: false, |
| 279 |
}; |
| 280 |
|
| 281 |
var xhr = new window.XMLHttpRequest(); |
| 282 |
xhr.open('POST', '/some/otherpath'); |
| 283 |
|
| 284 |
xhr.upload.addEventListener('progress', function _progress() { |
| 285 |
listenerEvents.progress = true; |
| 286 |
}); |
| 287 |
|
| 288 |
xhr.upload.onprogress = function _onprogress() { |
| 289 |
onEvents.progress = true; |
| 290 |
}; |
| 291 |
|
| 292 |
xhr.onload = function _onload() { |
| 293 |
setTimeout(finish, 1); |
| 294 |
}; |
| 295 |
|
| 296 |
xhr.send('some data'); |
| 297 |
|
| 298 |
// ensure the test ends |
| 299 |
var failTimer = setTimeout(function () { |
| 300 |
assert.ok(false, 'test timed out'); |
| 301 |
done(); |
| 302 |
}, 500); |
| 303 |
|
| 304 |
var finished = false; |
| 305 |
function finish() { |
| 306 |
if (!finished) { |
| 307 |
finished = true; |
| 308 |
clearTimeout(failTimer); |
| 309 |
assert.ok(onEvents.progress, 'onprogress called'); |
| 310 |
assert.ok(listenerEvents.progress, 'progress listener called'); |
| 311 |
|
| 312 |
done(); |
| 313 |
} |
| 314 |
} |
| 315 |
}); |
| 316 |
|
| 317 |
it('asynchronous request with pass-through and empty response', function (assert) { |
| 318 |
var done = assert.async(); |
| 319 |
var pretender = this.pretender; |
| 320 |
|
| 321 |
function testXHR() { |
| 322 |
this.pretender = pretender; |
| 323 |
this.open = function () {}; |
| 324 |
this.setRequestHeader = function () {}; |
| 325 |
this.responseText = ''; |
| 326 |
this.response = ''; |
| 327 |
this.onload = true; |
| 328 |
this.send = { |
| 329 |
pretender: pretender, |
| 330 |
apply: function (xhr /*, argument*/) { |
| 331 |
xhr.onload({ target: xhr, type: 'load' }); |
| 332 |
}, |
| 333 |
}; |
| 334 |
} |
| 335 |
pretender._nativeXMLHttpRequest = testXHR; |
| 336 |
|
| 337 |
pretender.get('/some/path', pretender.passthrough); |
| 338 |
|
| 339 |
var xhr = new window.XMLHttpRequest(); |
| 340 |
xhr.open('GET', '/some/path'); |
| 341 |
xhr.addEventListener('load', function _onload(event) { |
| 342 |
assert.equal( |
| 343 |
xhr.responseText, |
| 344 |
event.target.responseText, |
| 345 |
'responseText for real and fake xhr are both blank strings' |
| 346 |
); |
| 347 |
assert.equal( |
| 348 |
xhr.response, |
| 349 |
event.target.response, |
| 350 |
'response for real and fake xhr are both blank strings' |
| 351 |
); |
| 352 |
done(); |
| 353 |
}); |
| 354 |
|
| 355 |
xhr.send(); |
| 356 |
}); |
| 357 |
|
| 358 |
describe('the `.passthrough()` property', function () { |
| 359 |
it('allows a passthrough on an unhandledRequest', function (assert) { |
| 360 |
var done = assert.async(); |
| 361 |
|
| 362 |
this.pretender.unhandledRequest = function (_verb, _path, request) { |
| 363 |
request.passthrough(); |
| 364 |
}; |
| 365 |
|
| 366 |
$.ajax({ |
| 367 |
url: '/some/path', |
| 368 |
error: function (xhr) { |
| 369 |
assert.equal(xhr.status, 404); |
| 370 |
done(); |
| 371 |
}, |
| 372 |
}); |
| 373 |
}); |
| 374 |
|
| 375 |
it('returns a native xhr', function (assert) { |
| 376 |
var done = assert.async(); |
| 377 |
|
| 378 |
var pretender = this.pretender; |
| 379 |
|
| 380 |
function testXHR() { |
| 381 |
this.pretender = pretender; |
| 382 |
this.open = function () {}; |
| 383 |
this.setRequestHeader = function () {}; |
| 384 |
this.responseText = ''; |
| 385 |
this.response = ''; |
| 386 |
this.onload = true; |
| 387 |
this.send = { |
| 388 |
pretender: pretender, |
| 389 |
apply: function (xhr /*, argument*/) { |
| 390 |
xhr.onload({ target: xhr, type: 'load' }); |
| 391 |
}, |
| 392 |
}; |
| 393 |
} |
| 394 |
pretender._nativeXMLHttpRequest = testXHR; |
| 395 |
|
| 396 |
var xhr = new window.XMLHttpRequest(); |
| 397 |
xhr.open('GET', '/some/path'); |
| 398 |
|
| 399 |
this.pretender.unhandledRequest = function (_verb, _path, request) { |
| 400 |
var referencedXhr = request.passthrough(); |
| 401 |
assert.ok(referencedXhr instanceof testXHR); |
| 402 |
done(); |
| 403 |
}; |
| 404 |
|
| 405 |
xhr.send(); |
| 406 |
}); |
| 407 |
}); |
| 408 |
}); |