| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
(function (ajax) { |
| 5 |
var BIND_ARGS = 'bind' |
| 6 |
, PASS_ARGS = 'pass' |
| 7 |
, FakeXHR = (function () { |
| 8 |
function FakeXHR () { |
| 9 |
this.args = {} |
| 10 |
FakeXHR.last = this |
| 11 |
} |
| 12 |
FakeXHR.setup = function () { |
| 13 |
FakeXHR.oldxhr = window['XMLHttpRequest'] |
| 14 |
FakeXHR.oldaxo = window['ActiveXObject'] |
| 15 |
window['XMLHttpRequest'] = FakeXHR |
| 16 |
window['ActiveXObject'] = FakeXHR |
| 17 |
FakeXHR.last = null |
| 18 |
} |
| 19 |
FakeXHR.restore = function () { |
| 20 |
window['XMLHttpRequest'] = FakeXHR.oldxhr |
| 21 |
window['ActiveXObject'] = FakeXHR.oldaxo |
| 22 |
} |
| 23 |
FakeXHR.prototype.methodCallCount = function (name) { |
| 24 |
return this.args[name] ? this.args[name].length : 0 |
| 25 |
} |
| 26 |
FakeXHR.prototype.methodCallArgs = function (name, i, j) { |
| 27 |
var a = this.args[name] |
| 28 |
&& this.args[name].length > i ? this.args[name][i] : null |
| 29 |
if (arguments.length > 2) return a && a.length > j ? a[j] : null |
| 30 |
return a |
| 31 |
} |
| 32 |
v.each(['open', 'send', 'setRequestHeader' ], function (f) { |
| 33 |
FakeXHR.prototype[f] = function () { |
| 34 |
if (!this.args[f]) this.args[f] = [] |
| 35 |
this.args[f].push(arguments) |
| 36 |
} |
| 37 |
}) |
| 38 |
return FakeXHR |
| 39 |
}()) |
| 40 |
|
| 41 |
sink('Setup', function (test, ok, before, after) { |
| 42 |
before(function () { |
| 43 |
ajax.ajaxSetup({ |
| 44 |
dataFilter: function (resp, type) { |
| 45 |
|
| 46 |
return resp.substring('])}while(1);</x>'.length) |
| 47 |
} |
| 48 |
}) |
| 49 |
}) |
| 50 |
after(function () { |
| 51 |
ajax.ajaxSetup({ |
| 52 |
|
| 53 |
dataFilter: function (resp, type) { |
| 54 |
return resp |
| 55 |
} |
| 56 |
}) |
| 57 |
}) |
| 58 |
test('dataFilter', function (complete) { |
| 59 |
ajax({ |
| 60 |
url: '/tests/fixtures/fixtures_with_prefix.json' |
| 61 |
, type: 'json' |
| 62 |
, success: function (resp) { |
| 63 |
ok(resp, 'received response') |
| 64 |
ok( |
| 65 |
resp && resp.boosh == 'boosh' |
| 66 |
, 'correctly evaluated response as JSON' |
| 67 |
) |
| 68 |
complete() |
| 69 |
} |
| 70 |
}) |
| 71 |
}) |
| 72 |
}) |
| 73 |
|
| 74 |
sink('Mime Types', function (test, ok) { |
| 75 |
test('JSON', function (complete) { |
| 76 |
ajax({ |
| 77 |
url: '/tests/fixtures/fixtures.json' |
| 78 |
, type: 'json' |
| 79 |
, success: function (resp) { |
| 80 |
ok(resp, 'received response') |
| 81 |
ok( |
| 82 |
resp && resp.boosh == 'boosh' |
| 83 |
, 'correctly evaluated response as JSON' |
| 84 |
) |
| 85 |
complete() |
| 86 |
} |
| 87 |
}) |
| 88 |
}) |
| 89 |
|
| 90 |
test('JSONP', function (complete) { |
| 91 |
|
| 92 |
reqwest.getcallbackPrefix = function (id) { |
| 93 |
return 'reqwest_' + id |
| 94 |
} |
| 95 |
ajax({ |
| 96 |
url: '/tests/fixtures/fixtures_jsonp.jsonp?callback=?' |
| 97 |
, type: 'jsonp' |
| 98 |
, success: function (resp) { |
| 99 |
ok(resp, 'received response for unique generated callback') |
| 100 |
ok( |
| 101 |
resp && resp.boosh == 'boosh' |
| 102 |
, 'correctly evaled response for unique generated cb as JSONP' |
| 103 |
) |
| 104 |
complete() |
| 105 |
} |
| 106 |
}) |
| 107 |
}) |
| 108 |
|
| 109 |
test('JS', function (complete) { |
| 110 |
ajax({ |
| 111 |
url: '/tests/fixtures/fixtures.js' |
| 112 |
, type: 'js' |
| 113 |
, success: function () { |
| 114 |
ok( |
| 115 |
typeof boosh !== 'undefined' && boosh == 'boosh' |
| 116 |
, 'evaluated response as JavaScript' |
| 117 |
) |
| 118 |
complete() |
| 119 |
} |
| 120 |
}) |
| 121 |
}) |
| 122 |
|
| 123 |
test('HTML', function (complete) { |
| 124 |
ajax({ |
| 125 |
url: '/tests/fixtures/fixtures.html' |
| 126 |
, type: 'html' |
| 127 |
, success: function (resp) { |
| 128 |
ok(resp == '<p>boosh</p>', 'evaluated response as HTML') |
| 129 |
complete() |
| 130 |
} |
| 131 |
}) |
| 132 |
}) |
| 133 |
|
| 134 |
test('XML', function (complete) { |
| 135 |
ajax({ |
| 136 |
url: '/tests/fixtures/fixtures.xml' |
| 137 |
, type: 'xml' |
| 138 |
, success: function (resp) { |
| 139 |
ok(resp |
| 140 |
&& resp.documentElement |
| 141 |
&& resp.documentElement.nodeName == 'root' |
| 142 |
, 'XML Response root is <root>' |
| 143 |
) |
| 144 |
ok(resp |
| 145 |
&& resp.documentElement |
| 146 |
&& resp.documentElement.hasChildNodes |
| 147 |
&& resp.documentElement.firstChild.nodeName == 'boosh' |
| 148 |
&& resp.documentElement.firstChild.firstChild.nodeValue |
| 149 |
== 'boosh' |
| 150 |
, 'Correct XML response' |
| 151 |
) |
| 152 |
complete() |
| 153 |
} |
| 154 |
, error: function (err) { |
| 155 |
ok(false, err.responseText) |
| 156 |
complete() |
| 157 |
} |
| 158 |
}) |
| 159 |
}) |
| 160 |
|
| 161 |
test('XML (404)', function (complete) { |
| 162 |
ajax({ |
| 163 |
url:'/tests/fixtures/badfixtures.xml' |
| 164 |
, type:'xml' |
| 165 |
, success: function (resp) { |
| 166 |
if (resp == null) { |
| 167 |
ok(true, 'XML response is null') |
| 168 |
complete() |
| 169 |
} else { |
| 170 |
ok(resp |
| 171 |
&& resp.documentElement |
| 172 |
&& resp.documentElement.firstChild |
| 173 |
&& (/error/i).test(resp.documentElement.firstChild.nodeValue) |
| 174 |
, 'XML response reports parsing error' |
| 175 |
) |
| 176 |
complete() |
| 177 |
} |
| 178 |
} |
| 179 |
, error: function () { |
| 180 |
ok(true, 'No XML response (error())') |
| 181 |
complete() |
| 182 |
} |
| 183 |
}) |
| 184 |
}) |
| 185 |
}) |
| 186 |
|
| 187 |
sink('JSONP', function (test, ok) { |
| 188 |
test('Named callback in query string', function (complete) { |
| 189 |
ajax({ |
| 190 |
url: '/tests/fixtures/fixtures_jsonp2.jsonp?foo=bar' |
| 191 |
, type: 'jsonp' |
| 192 |
, jsonpCallback: 'foo' |
| 193 |
, success: function (resp) { |
| 194 |
ok(resp, 'received response for custom callback') |
| 195 |
ok( |
| 196 |
resp && resp.boosh == 'boosh' |
| 197 |
, 'correctly evaluated response as JSONP with custom callback' |
| 198 |
) |
| 199 |
complete() |
| 200 |
} |
| 201 |
}) |
| 202 |
}) |
| 203 |
|
| 204 |
test('Unnamed callback in query string', function (complete) { |
| 205 |
ajax({ |
| 206 |
url: '/tests/fixtures/fixtures_jsonp3.jsonp?foo=?' |
| 207 |
, type: 'jsonp' |
| 208 |
, jsonpCallback: 'foo' |
| 209 |
, success: function (resp) { |
| 210 |
ok(resp, 'received response for custom wildcard callback') |
| 211 |
ok( |
| 212 |
resp && resp.boosh == 'boosh' |
| 213 |
, 'correctly evaled response as JSONP with custom wildcard cb' |
| 214 |
) |
| 215 |
complete() |
| 216 |
} |
| 217 |
}) |
| 218 |
}) |
| 219 |
|
| 220 |
test('No callback, no query string', function (complete) { |
| 221 |
ajax({ |
| 222 |
url: '/tests/fixtures/fixtures_jsonp3.jsonp' |
| 223 |
, type: 'jsonp' |
| 224 |
, jsonpCallback: 'foo' |
| 225 |
, success: function (resp) { |
| 226 |
ok(resp, 'received response for custom wildcard callback') |
| 227 |
ok( |
| 228 |
resp && resp.boosh == 'boosh' |
| 229 |
, 'correctly evaled response as JSONP with custom cb not in url' |
| 230 |
) |
| 231 |
complete() |
| 232 |
} |
| 233 |
}) |
| 234 |
}) |
| 235 |
|
| 236 |
test('No callback in existing query string', function (complete) { |
| 237 |
ajax({ |
| 238 |
url: '/tests/none.jsonp?echo&somevar=some+long+str+here' |
| 239 |
, type: 'jsonp' |
| 240 |
, jsonpCallbackName: 'yohoho' |
| 241 |
, success: function (resp) { |
| 242 |
ok(resp && resp.query, 'received response from echo callback') |
| 243 |
ok( |
| 244 |
resp && resp.query && resp.query.somevar == 'some long str here' |
| 245 |
, 'correctly evaluated response as JSONP with echo callback' |
| 246 |
) |
| 247 |
complete() |
| 248 |
} |
| 249 |
}) |
| 250 |
}) |
| 251 |
|
| 252 |
test('Append data to existing query string', function (complete) { |
| 253 |
ajax({ |
| 254 |
url: '/tests/none.jsonp?echo' |
| 255 |
, type: 'jsonp' |
| 256 |
, data: { somevar: 'some long str here', anothervar: 'yo ho ho!' } |
| 257 |
, success: function (resp) { |
| 258 |
ok(resp && resp.query, 'received response from echo callback') |
| 259 |
ok( |
| 260 |
resp && resp.query && resp.query.somevar == 'some long str here' |
| 261 |
, 'correctly sent and received data object from JSONP echo (1)' |
| 262 |
) |
| 263 |
ok( |
| 264 |
resp && resp.query && resp.query.anothervar == 'yo ho ho!' |
| 265 |
, 'correctly sent and received data object from JSONP echo (2)' |
| 266 |
) |
| 267 |
complete() |
| 268 |
} |
| 269 |
}) |
| 270 |
}) |
| 271 |
|
| 272 |
test('Generate complete query string from data', function (complete) { |
| 273 |
ajax({ |
| 274 |
url: '/tests/none.jsonp' |
| 275 |
, type: 'jsonp' |
| 276 |
, data: [ |
| 277 |
{ name: 'somevar', value: 'some long str here' } |
| 278 |
, { name: 'anothervar', value: 'yo ho ho!' } |
| 279 |
, { name: 'echo', value: true } |
| 280 |
] |
| 281 |
, success: function (resp) { |
| 282 |
ok(resp && resp.query, 'received response from echo callback') |
| 283 |
ok( |
| 284 |
resp && resp.query && resp.query.somevar == 'some long str here' |
| 285 |
, 'correctly sent and received data array from JSONP echo (1)' |
| 286 |
) |
| 287 |
ok( |
| 288 |
resp && resp.query && resp.query.anothervar == 'yo ho ho!' |
| 289 |
, 'correctly sent and received data array from JSONP echo (2)' |
| 290 |
) |
| 291 |
complete() |
| 292 |
} |
| 293 |
}) |
| 294 |
}) |
| 295 |
|
| 296 |
test('Append data to query string and insert callback name' |
| 297 |
, function (complete) { |
| 298 |
|
| 299 |
ajax({ |
| 300 |
|
| 301 |
url: '/tests/none.jsonp?callback=?' |
| 302 |
, type: 'jsonp' |
| 303 |
, jsonpCallbackName: 'reqwest_foo' |
| 304 |
, data: { foo: 'bar', boo: 'baz', echo: true } |
| 305 |
, success: function (resp) { |
| 306 |
ok(resp && resp.query, 'received response from echo callback') |
| 307 |
ok( |
| 308 |
resp && resp.query && resp.query.callback == 'reqwest_foo' |
| 309 |
, 'correctly matched callback in URL' |
| 310 |
) |
| 311 |
complete() |
| 312 |
} |
| 313 |
}) |
| 314 |
}) |
| 315 |
}) |
| 316 |
|
| 317 |
sink('Callbacks', function (test, ok) { |
| 318 |
|
| 319 |
test('sync version', function (done) { |
| 320 |
var r = ajax({ |
| 321 |
method: 'get' |
| 322 |
, url: '/tests/fixtures/fixtures.json' |
| 323 |
, type: 'json' |
| 324 |
, async: false |
| 325 |
}) |
| 326 |
var request = r.request, |
| 327 |
responseText = request.response !== undefined ? request.response : request.responseText |
| 328 |
ok(eval('(' + responseText + ')').boosh == 'boosh', 'can make sync calls') |
| 329 |
done() |
| 330 |
}) |
| 331 |
|
| 332 |
test('no callbacks', function (complete) { |
| 333 |
var pass = true |
| 334 |
try { |
| 335 |
ajax('/tests/fixtures/fixtures.js') |
| 336 |
} catch (ex) { |
| 337 |
pass = false |
| 338 |
} finally { |
| 339 |
ok(pass, 'successfully doesnt fail without callback') |
| 340 |
complete() |
| 341 |
} |
| 342 |
}) |
| 343 |
|
| 344 |
test('complete is called', function (complete) { |
| 345 |
ajax({ |
| 346 |
url: '/tests/fixtures/fixtures.js' |
| 347 |
, complete: function () { |
| 348 |
ok(true, 'called complete') |
| 349 |
complete() |
| 350 |
} |
| 351 |
}) |
| 352 |
}) |
| 353 |
|
| 354 |
test('invalid JSON sets error on resp object', function (complete) { |
| 355 |
ajax({ |
| 356 |
url: '/tests/fixtures/invalidJSON.json' |
| 357 |
, type: 'json' |
| 358 |
, success: function () { |
| 359 |
ok(false, 'success callback fired') |
| 360 |
complete() |
| 361 |
} |
| 362 |
, error: function (resp, msg) { |
| 363 |
ok( |
| 364 |
msg == 'Could not parse JSON in response' |
| 365 |
, 'error callback fired' |
| 366 |
) |
| 367 |
complete() |
| 368 |
} |
| 369 |
}) |
| 370 |
}) |
| 371 |
|
| 372 |
test('multiple parallel named JSONP callbacks', 8, function () { |
| 373 |
ajax({ |
| 374 |
url: '/tests/fixtures/fixtures_jsonp_multi.jsonp?callback=reqwest_0' |
| 375 |
, type: 'jsonp' |
| 376 |
, success: function (resp) { |
| 377 |
ok(resp, 'received response from call #1') |
| 378 |
ok( |
| 379 |
resp && resp.a == 'a' |
| 380 |
, 'evaluated response from call #1 as JSONP' |
| 381 |
) |
| 382 |
} |
| 383 |
}) |
| 384 |
ajax({ |
| 385 |
url: '/tests/fixtures/fixtures_jsonp_multi_b.jsonp?callback=reqwest_0' |
| 386 |
, type: 'jsonp' |
| 387 |
, success: function (resp) { |
| 388 |
ok(resp, 'received response from call #2') |
| 389 |
ok( |
| 390 |
resp && resp.b == 'b' |
| 391 |
, 'evaluated response from call #2 as JSONP' |
| 392 |
) |
| 393 |
} |
| 394 |
}) |
| 395 |
ajax({ |
| 396 |
url: '/tests/fixtures/fixtures_jsonp_multi_c.jsonp?callback=reqwest_0' |
| 397 |
, type: 'jsonp' |
| 398 |
, success: function (resp) { |
| 399 |
ok(resp, 'received response from call #2') |
| 400 |
ok( |
| 401 |
resp && resp.c == 'c' |
| 402 |
, 'evaluated response from call #3 as JSONP' |
| 403 |
) |
| 404 |
} |
| 405 |
}) |
| 406 |
ajax({ |
| 407 |
url: '/tests/fixtures/fixtures_jsonp_multi.jsonp?callback=reqwest_0' |
| 408 |
, type: 'jsonp' |
| 409 |
, success: function (resp) { |
| 410 |
ok(resp, 'received response from call #2') |
| 411 |
ok( |
| 412 |
resp && resp.a == 'a' |
| 413 |
, 'evaluated response from call #4 as JSONP' |
| 414 |
) |
| 415 |
} |
| 416 |
}) |
| 417 |
}) |
| 418 |
|
| 419 |
test('JSONP also supports success promises', function (complete) { |
| 420 |
ajax({ |
| 421 |
url: '/tests/none.jsonp?echo' |
| 422 |
, type: 'jsonp' |
| 423 |
, success: function (resp) { |
| 424 |
ok(resp, 'received response in constructor success callback') |
| 425 |
} |
| 426 |
}) |
| 427 |
.then(function (resp) { |
| 428 |
ok(resp, 'received response in promise success callback') |
| 429 |
return resp; |
| 430 |
}) |
| 431 |
.then(function (resp) { |
| 432 |
ok(resp, 'received response in second promise success callback') |
| 433 |
complete() |
| 434 |
}) |
| 435 |
}) |
| 436 |
|
| 437 |
test('JSONP also supports error promises', function (complete) { |
| 438 |
ajax({ |
| 439 |
url: '/tests/timeout/' |
| 440 |
, type: 'jsonp' |
| 441 |
, error: function (err) { |
| 442 |
ok(err, 'received error response in constructor error callback') |
| 443 |
} |
| 444 |
}) |
| 445 |
.fail(function (err) { |
| 446 |
ok(err, 'received error response in promise error callback') |
| 447 |
}) |
| 448 |
.fail(function (err) { |
| 449 |
ok(err, 'received error response in second promise error callback') |
| 450 |
complete() |
| 451 |
}) |
| 452 |
.abort() |
| 453 |
}) |
| 454 |
|
| 455 |
}) |
| 456 |
|
| 457 |
if (window.XMLHttpRequest |
| 458 |
&& ('withCredentials' in new window.XMLHttpRequest())) { |
| 459 |
|
| 460 |
sink('Cross-origin Resource Sharing', function (test, ok) { |
| 461 |
test('make request to another origin', 1, function () { |
| 462 |
ajax({ |
| 463 |
url: 'http://' + window.location.hostname + ':5678/get-value' |
| 464 |
, type: 'text' |
| 465 |
, method: 'get' |
| 466 |
, crossOrigin: true |
| 467 |
, complete: function (resp) { |
| 468 |
ok(resp.responseText === 'hello', 'request made successfully') |
| 469 |
} |
| 470 |
}) |
| 471 |
}) |
| 472 |
|
| 473 |
test('set cookie on other origin', 2, function () { |
| 474 |
ajax({ |
| 475 |
url: 'http://' + window.location.hostname + ':5678/set-cookie' |
| 476 |
, type: 'text' |
| 477 |
, method: 'get' |
| 478 |
, crossOrigin: true |
| 479 |
, withCredentials: true |
| 480 |
, before: function (http) { |
| 481 |
ok( |
| 482 |
http.withCredentials === true |
| 483 |
, 'has set withCredentials on connection object' |
| 484 |
) |
| 485 |
} |
| 486 |
, complete: function (resp) { |
| 487 |
ok(resp.status === 200, 'cookie set successfully') |
| 488 |
} |
| 489 |
}) |
| 490 |
}) |
| 491 |
|
| 492 |
test('get cookie from other origin', 1, function () { |
| 493 |
ajax({ |
| 494 |
url: 'http://' |
| 495 |
+ window.location.hostname |
| 496 |
+ ':5678/get-cookie-value' |
| 497 |
, type: 'text' |
| 498 |
, method: 'get' |
| 499 |
, crossOrigin: true |
| 500 |
, withCredentials: true |
| 501 |
, complete: function (resp) { |
| 502 |
ok( |
| 503 |
resp.responseText == 'hello' |
| 504 |
, 'cookie value retrieved successfully' |
| 505 |
) |
| 506 |
} |
| 507 |
}) |
| 508 |
}) |
| 509 |
|
| 510 |
}) |
| 511 |
} |
| 512 |
|
| 513 |
sink('Connection Object', function (test, ok) { |
| 514 |
|
| 515 |
test('use xhr factory provided in the options', function (complete) { |
| 516 |
var reqwest |
| 517 |
, xhr |
| 518 |
|
| 519 |
if (typeof XMLHttpRequest !== 'undefined') { |
| 520 |
xhr = new XMLHttpRequest() |
| 521 |
} else if (typeof ActiveXObject !== 'undefined') { |
| 522 |
xhr = new ActiveXObject('Microsoft.XMLHTTP') |
| 523 |
} else { |
| 524 |
ok(false, 'browser not supported') |
| 525 |
} |
| 526 |
|
| 527 |
reqwest = ajax({ |
| 528 |
url: '/tests/fixtures/fixtures.html', |
| 529 |
xhr: function () { |
| 530 |
return xhr |
| 531 |
} |
| 532 |
}) |
| 533 |
|
| 534 |
ok(reqwest.request === xhr, 'uses factory') |
| 535 |
complete() |
| 536 |
}) |
| 537 |
|
| 538 |
test('fallbacks to own xhr factory if falsy is returned', function (complete) { |
| 539 |
var reqwest |
| 540 |
|
| 541 |
FakeXHR.setup() |
| 542 |
try { |
| 543 |
reqwest = ajax({ |
| 544 |
url: '/tests/fixtures/fixtures.html', |
| 545 |
xhr: function () { |
| 546 |
return null |
| 547 |
} |
| 548 |
}) |
| 549 |
|
| 550 |
ok(reqwest.request instanceof FakeXHR, 'fallbacks correctly') |
| 551 |
complete() |
| 552 |
} finally { |
| 553 |
FakeXHR.restore() |
| 554 |
} |
| 555 |
}) |
| 556 |
|
| 557 |
test('setRequestHeaders', function (complete) { |
| 558 |
ajax({ |
| 559 |
url: '/tests/fixtures/fixtures.html' |
| 560 |
, data: 'foo=bar&baz=thunk' |
| 561 |
, method: 'post' |
| 562 |
, headers: { |
| 563 |
'Accept': 'application/x-foo' |
| 564 |
} |
| 565 |
, success: function () { |
| 566 |
ok(true, 'can post headers') |
| 567 |
complete() |
| 568 |
} |
| 569 |
}) |
| 570 |
}) |
| 571 |
|
| 572 |
test('can inspect http before send', function (complete) { |
| 573 |
var connection = ajax({ |
| 574 |
url: '/tests/fixtures/fixtures.js' |
| 575 |
, method: 'post' |
| 576 |
, type: 'js' |
| 577 |
, before: function (http) { |
| 578 |
ok(http.readyState == 1, 'received http connection object') |
| 579 |
} |
| 580 |
, success: function () { |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
setTimeout(function () { |
| 586 |
ok( |
| 587 |
connection.request.readyState == 4 |
| 588 |
, 'success callback has readyState of 4' |
| 589 |
) |
| 590 |
complete() |
| 591 |
}, 0) |
| 592 |
} |
| 593 |
}) |
| 594 |
}) |
| 595 |
|
| 596 |
test('ajax() encodes array `data`', function (complete) { |
| 597 |
FakeXHR.setup() |
| 598 |
try { |
| 599 |
ajax({ |
| 600 |
url: '/tests/fixtures/fixtures.html' |
| 601 |
, method: 'post' |
| 602 |
, data: [ |
| 603 |
{ name: 'foo', value: 'bar' } |
| 604 |
, { name: 'baz', value: 'thunk' } |
| 605 |
] |
| 606 |
}) |
| 607 |
ok(FakeXHR.last.methodCallCount('send') == 1, 'send called') |
| 608 |
ok( |
| 609 |
FakeXHR.last.methodCallArgs('send', 0).length == 1 |
| 610 |
, 'send called with 1 arg' |
| 611 |
) |
| 612 |
ok( |
| 613 |
FakeXHR.last.methodCallArgs('send', 0, 0) == 'foo=bar&baz=thunk' |
| 614 |
, 'send called with encoded array' |
| 615 |
) |
| 616 |
complete() |
| 617 |
} finally { |
| 618 |
FakeXHR.restore() |
| 619 |
} |
| 620 |
}) |
| 621 |
|
| 622 |
test('ajax() encodes hash `data`', function (complete) { |
| 623 |
FakeXHR.setup() |
| 624 |
try { |
| 625 |
ajax({ |
| 626 |
url: '/tests/fixtures/fixtures.html' |
| 627 |
, method: 'post' |
| 628 |
, data: { bar: 'foo', thunk: 'baz' } |
| 629 |
}) |
| 630 |
ok(FakeXHR.last.methodCallCount('send') == 1, 'send called') |
| 631 |
ok( |
| 632 |
FakeXHR.last.methodCallArgs('send', 0).length == 1 |
| 633 |
, 'send called with 1 arg' |
| 634 |
) |
| 635 |
ok( |
| 636 |
FakeXHR.last.methodCallArgs('send', 0, 0) == 'bar=foo&thunk=baz' |
| 637 |
, 'send called with encoded array' |
| 638 |
) |
| 639 |
complete() |
| 640 |
} finally { |
| 641 |
FakeXHR.restore() |
| 642 |
} |
| 643 |
}) |
| 644 |
|
| 645 |
test('ajax() obeys `processData`', function (complete) { |
| 646 |
FakeXHR.setup() |
| 647 |
try { |
| 648 |
var d = { bar: 'foo', thunk: 'baz' } |
| 649 |
ajax({ |
| 650 |
url: '/tests/fixtures/fixtures.html' |
| 651 |
, processData: false |
| 652 |
, method: 'post' |
| 653 |
, data: d |
| 654 |
}) |
| 655 |
ok(FakeXHR.last.methodCallCount('send') == 1, 'send called') |
| 656 |
ok( |
| 657 |
FakeXHR.last.methodCallArgs('send', 0).length == 1 |
| 658 |
, 'send called with 1 arg' |
| 659 |
) |
| 660 |
ok( |
| 661 |
FakeXHR.last.methodCallArgs('send', 0, 0) === d |
| 662 |
, 'send called with exact `data` object' |
| 663 |
) |
| 664 |
complete() |
| 665 |
} finally { |
| 666 |
FakeXHR.restore() |
| 667 |
} |
| 668 |
}) |
| 669 |
|
| 670 |
function testXhrGetUrlAdjustment(url, data, expectedUrl, complete) { |
| 671 |
FakeXHR.setup() |
| 672 |
try { |
| 673 |
ajax({ url: url, data: data }) |
| 674 |
ok(FakeXHR.last.methodCallCount('open') == 1, 'open called') |
| 675 |
ok( |
| 676 |
FakeXHR.last.methodCallArgs('open', 0).length == 3 |
| 677 |
, 'open called with 3 args' |
| 678 |
) |
| 679 |
ok( |
| 680 |
FakeXHR.last.methodCallArgs('open', 0, 0) == 'GET' |
| 681 |
, 'first arg of open() is "GET"' |
| 682 |
) |
| 683 |
ok(FakeXHR.last.methodCallArgs('open', 0, 1) == expectedUrl |
| 684 |
, 'second arg of open() is URL with query string') |
| 685 |
ok( |
| 686 |
FakeXHR.last.methodCallArgs('open', 0, 2) === true |
| 687 |
, 'third arg of open() is `true`' |
| 688 |
) |
| 689 |
ok(FakeXHR.last.methodCallCount('send') == 1, 'send called') |
| 690 |
ok( |
| 691 |
FakeXHR.last.methodCallArgs('send', 0).length == 1 |
| 692 |
, 'send called with 1 arg' |
| 693 |
) |
| 694 |
ok( |
| 695 |
FakeXHR.last.methodCallArgs('send', 0, 0) === null |
| 696 |
, 'send called with null' |
| 697 |
) |
| 698 |
complete() |
| 699 |
} finally { |
| 700 |
FakeXHR.restore() |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
test('ajax() appends GET URL with ?`data`', function (complete) { |
| 705 |
testXhrGetUrlAdjustment( |
| 706 |
'/tests/fixtures/fixtures.html' |
| 707 |
, 'bar=foo&thunk=baz' |
| 708 |
, '/tests/fixtures/fixtures.html?bar=foo&thunk=baz' |
| 709 |
, complete |
| 710 |
) |
| 711 |
}) |
| 712 |
|
| 713 |
test('ajax() appends GET URL with ?`data` (serialized object)' |
| 714 |
, function (complete) { |
| 715 |
|
| 716 |
testXhrGetUrlAdjustment( |
| 717 |
'/tests/fixtures/fixtures.html' |
| 718 |
, { bar: 'foo', thunk: 'baz' } |
| 719 |
, '/tests/fixtures/fixtures.html?bar=foo&thunk=baz' |
| 720 |
, complete |
| 721 |
) |
| 722 |
}) |
| 723 |
|
| 724 |
test('ajax() appends GET URL with &`data` (serialized array)' |
| 725 |
, function (complete) { |
| 726 |
|
| 727 |
testXhrGetUrlAdjustment( |
| 728 |
'/tests/fixtures/fixtures.html?x=y' |
| 729 |
, [ { name: 'bar', value: 'foo'}, {name: 'thunk', value: 'baz' } ] |
| 730 |
, '/tests/fixtures/fixtures.html?x=y&bar=foo&thunk=baz' |
| 731 |
, complete |
| 732 |
) |
| 733 |
}) |
| 734 |
}) |
| 735 |
|
| 736 |
sink('Standard vs compat mode', function (test, ok) { |
| 737 |
function methodMatch(resp, method) { |
| 738 |
return resp && resp.method === method |
| 739 |
} |
| 740 |
function headerMatch(resp, key, expected) { |
| 741 |
return resp && resp.headers && resp.headers[key] === expected |
| 742 |
} |
| 743 |
function queryMatch(resp, key, expected) { |
| 744 |
return resp && resp.query && resp.query[key] === expected |
| 745 |
} |
| 746 |
|
| 747 |
test('standard mode default', function (complete) { |
| 748 |
ajax({ |
| 749 |
url: '/tests/none.json?echo' |
| 750 |
, success: function (resp) { |
| 751 |
ok(methodMatch(resp, 'GET'), 'correct request method (GET)') |
| 752 |
ok( |
| 753 |
headerMatch( |
| 754 |
resp |
| 755 |
, 'content-type' |
| 756 |
, 'application/x-www-form-urlencoded' |
| 757 |
) |
| 758 |
, 'correct Content-Type request header' |
| 759 |
) |
| 760 |
ok( |
| 761 |
headerMatch(resp, 'x-requested-with', 'XMLHttpRequest') |
| 762 |
, 'correct X-Requested-With header' |
| 763 |
) |
| 764 |
ok( |
| 765 |
headerMatch( |
| 766 |
resp |
| 767 |
, 'accept' |
| 768 |
, 'text/javascript, text/html, application/xml, text/xml, */*' |
| 769 |
) |
| 770 |
, 'correct Accept header' |
| 771 |
) |
| 772 |
complete() |
| 773 |
} |
| 774 |
}) |
| 775 |
}) |
| 776 |
|
| 777 |
test('standard mode custom content-type', function (complete) { |
| 778 |
ajax({ |
| 779 |
url: '/tests/none.json?echo' |
| 780 |
, contentType: 'yapplication/foobar' |
| 781 |
, success: function (resp) { |
| 782 |
ok(methodMatch(resp, 'GET'), 'correct request method (GET)') |
| 783 |
ok( |
| 784 |
headerMatch(resp, 'content-type', 'yapplication/foobar') |
| 785 |
, 'correct Content-Type request header' |
| 786 |
) |
| 787 |
ok( |
| 788 |
headerMatch(resp, 'x-requested-with', 'XMLHttpRequest') |
| 789 |
, 'correct X-Requested-With header' |
| 790 |
) |
| 791 |
ok( |
| 792 |
headerMatch( |
| 793 |
resp |
| 794 |
, 'accept' |
| 795 |
, 'text/javascript, text/html, application/xml, text/xml, */*' |
| 796 |
) |
| 797 |
, 'correct Accept header' |
| 798 |
) |
| 799 |
complete() |
| 800 |
} |
| 801 |
}) |
| 802 |
}) |
| 803 |
|
| 804 |
test('standard mode on no content-type', function (complete) { |
| 805 |
ajax({ |
| 806 |
url: '/tests/204' |
| 807 |
, success: function (resp) { |
| 808 |
ok(true, 'Nothing blew up.') |
| 809 |
} |
| 810 |
}) |
| 811 |
}) |
| 812 |
|
| 813 |
test('compat mode "dataType=json" headers', function (complete) { |
| 814 |
ajax.compat({ |
| 815 |
url: '/tests/none.json?echo' |
| 816 |
, dataType: 'json' |
| 817 |
, success: function (resp) { |
| 818 |
ok(methodMatch(resp, 'GET'), 'correct request method (GET)') |
| 819 |
ok( |
| 820 |
headerMatch( |
| 821 |
resp |
| 822 |
, 'content-type' |
| 823 |
, 'application/x-www-form-urlencoded' |
| 824 |
) |
| 825 |
, 'correct Content-Type request header' |
| 826 |
) |
| 827 |
ok( |
| 828 |
headerMatch(resp, 'x-requested-with', 'XMLHttpRequest') |
| 829 |
, 'correct X-Requested-With header' |
| 830 |
) |
| 831 |
ok( |
| 832 |
headerMatch(resp, 'accept', 'application/json, text/javascript') |
| 833 |
, 'correct Accept header' |
| 834 |
) |
| 835 |
complete() |
| 836 |
} |
| 837 |
}) |
| 838 |
}) |
| 839 |
|
| 840 |
test('compat mode "dataType=json" with "type=post" headers' |
| 841 |
, function (complete) { |
| 842 |
ajax.compat({ |
| 843 |
url: '/tests/none.json?echo' |
| 844 |
, type: 'post' |
| 845 |
, dataType: 'json' |
| 846 |
, success: function (resp) { |
| 847 |
ok(methodMatch(resp, 'POST'), 'correct request method (POST)') |
| 848 |
ok( |
| 849 |
headerMatch( |
| 850 |
resp |
| 851 |
, 'content-type' |
| 852 |
, 'application/x-www-form-urlencoded' |
| 853 |
) |
| 854 |
, 'correct Content-Type request header' |
| 855 |
) |
| 856 |
ok( |
| 857 |
headerMatch(resp, 'x-requested-with', 'XMLHttpRequest') |
| 858 |
, 'correct X-Requested-With header' |
| 859 |
) |
| 860 |
ok( |
| 861 |
headerMatch(resp, 'accept', 'application/json, text/javascript') |
| 862 |
, 'correct Accept header' |
| 863 |
) |
| 864 |
complete() |
| 865 |
} |
| 866 |
}) |
| 867 |
}) |
| 868 |
|
| 869 |
test('compat mode "dataType=json" headers (with additional headers)' |
| 870 |
, function (complete) { |
| 871 |
|
| 872 |
ajax.compat({ |
| 873 |
url: '/tests/none.json?echo' |
| 874 |
, dataType: 'json' |
| 875 |
|
| 876 |
|
| 877 |
, headers: { one: 1, two: 2 } |
| 878 |
, success: function (resp) { |
| 879 |
ok( |
| 880 |
headerMatch( |
| 881 |
resp |
| 882 |
, 'content-type' |
| 883 |
, 'application/x-www-form-urlencoded' |
| 884 |
) |
| 885 |
, 'correct Content-Type request header' |
| 886 |
) |
| 887 |
ok( |
| 888 |
headerMatch(resp, 'x-requested-with', 'XMLHttpRequest') |
| 889 |
, 'correct X-Requested-With header' |
| 890 |
) |
| 891 |
ok( |
| 892 |
headerMatch(resp, 'accept', 'application/json, text/javascript') |
| 893 |
, 'correct Accept header' |
| 894 |
) |
| 895 |
ok( |
| 896 |
headerMatch(resp, 'one', '1') && headerMatch(resp, 'two', '2') |
| 897 |
, 'left additional headers intact' |
| 898 |
) |
| 899 |
complete() |
| 900 |
} |
| 901 |
}) |
| 902 |
}) |
| 903 |
|
| 904 |
test('compat mode "dataType=jsonp" query string', function (complete) { |
| 905 |
ajax.compat({ |
| 906 |
url: '/tests/none.jsonp?echo' |
| 907 |
, dataType: 'jsonp' |
| 908 |
, jsonp: 'testCallback' |
| 909 |
, jsonpCallback: 'foobar' |
| 910 |
, success: function (resp) { |
| 911 |
ok( |
| 912 |
queryMatch(resp, 'echo', '') |
| 913 |
, 'correct Content-Type request header' |
| 914 |
) |
| 915 |
ok( |
| 916 |
queryMatch(resp, 'testCallback', 'foobar') |
| 917 |
, 'correct X-Requested-With header' |
| 918 |
) |
| 919 |
complete() |
| 920 |
} |
| 921 |
}) |
| 922 |
}) |
| 923 |
}) |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
function createSerializeHelper(ok) { |
| 931 |
var forms = document.forms |
| 932 |
, foo = forms[0].getElementsByTagName('input')[1] |
| 933 |
, bar = forms[0].getElementsByTagName('input')[2] |
| 934 |
, choices = forms[0].getElementsByTagName('select')[0] |
| 935 |
, BIND_ARGS = 'bind' |
| 936 |
, PASS_ARGS = 'pass' |
| 937 |
|
| 938 |
function reset() { |
| 939 |
forms[1].reset() |
| 940 |
} |
| 941 |
|
| 942 |
function formElements(formIndex, tagName, elementIndex) { |
| 943 |
return forms[formIndex].getElementsByTagName(tagName)[elementIndex] |
| 944 |
} |
| 945 |
|
| 946 |
function isArray(a) { |
| 947 |
return Object.prototype.toString.call(a) == '[object Array]' |
| 948 |
} |
| 949 |
|
| 950 |
function sameValue(value, expected) { |
| 951 |
if (expected == null) { |
| 952 |
return value === null |
| 953 |
} else if (isArray(expected)) { |
| 954 |
if (value.length !== expected.length) return false |
| 955 |
for (var i = 0; i < expected.length; i++) { |
| 956 |
if (value[i] != expected[i]) return false |
| 957 |
} |
| 958 |
return true |
| 959 |
} else return value == expected |
| 960 |
} |
| 961 |
|
| 962 |
function testInput(input, name, value, str) { |
| 963 |
var sa = ajax.serialize(input, { type: 'array' }) |
| 964 |
, sh = ajax.serialize(input, { type: 'map' }) |
| 965 |
, av, i |
| 966 |
|
| 967 |
if (value != null) { |
| 968 |
av = isArray(value) ? value : [ value ] |
| 969 |
|
| 970 |
ok( |
| 971 |
sa.length == av.length |
| 972 |
, 'serialize(' + str + ', {type:\'array\'}) returns array ' |
| 973 |
+ '[{name,value}]' |
| 974 |
) |
| 975 |
|
| 976 |
for (i = 0; i < av.length; i++) { |
| 977 |
ok( |
| 978 |
name == sa[i].name |
| 979 |
, 'serialize(' + str + ', {type:\'array\'})[' + i + '].name' |
| 980 |
) |
| 981 |
ok( |
| 982 |
av[i] == sa[i].value |
| 983 |
, 'serialize(' + str + ', {type:\'array\'})[' + i + '].value' |
| 984 |
) |
| 985 |
} |
| 986 |
|
| 987 |
ok(sameValue(sh[name], value), 'serialize(' + str + ', {type:\'map\'})') |
| 988 |
} else { |
| 989 |
|
| 990 |
|
| 991 |
ok(sa.length === 0, 'serialize(' + str + ', {type:\'array\'}) is []') |
| 992 |
ok( |
| 993 |
v.keys(sh).length === 0 |
| 994 |
, 'serialize(' + str + ', {type:\'map\'}) is {}' |
| 995 |
) |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
function testFormSerialize(method, type) { |
| 1000 |
var expected = |
| 1001 |
'foo=bar&bar=baz&wha=1&wha=3&who=tawoo&%24escapable+name' |
| 1002 |
+ '%24=escapeme&choices=two&opinions=world+peace+is+not+real' |
| 1003 |
|
| 1004 |
ok(method, 'serialize() bound to context') |
| 1005 |
ok( |
| 1006 |
(method ? method(forms[0]) : null) == expected |
| 1007 |
, 'serialized form (' + type + ')' |
| 1008 |
) |
| 1009 |
} |
| 1010 |
|
| 1011 |
function executeMultiArgumentMethod(method, argType, options) { |
| 1012 |
var els = [ foo, bar, choices ] |
| 1013 |
, ths = argType === BIND_ARGS ? ender(els) : null |
| 1014 |
, args = argType === PASS_ARGS ? els : [] |
| 1015 |
|
| 1016 |
if (!!options) args.push(options) |
| 1017 |
|
| 1018 |
return method.apply(ths, args) |
| 1019 |
} |
| 1020 |
|
| 1021 |
function testMultiArgumentSerialize(method, type, argType) { |
| 1022 |
ok(method, 'serialize() bound in context') |
| 1023 |
var result = method ? executeMultiArgumentMethod(method, argType) : null |
| 1024 |
ok( |
| 1025 |
result == 'foo=bar&bar=baz&choices=two' |
| 1026 |
, 'serialized all 3 arguments together' |
| 1027 |
) |
| 1028 |
} |
| 1029 |
|
| 1030 |
function verifyFormSerializeArray(result, type) { |
| 1031 |
var expected = [ |
| 1032 |
{ name: 'foo', value: 'bar' } |
| 1033 |
, { name: 'bar', value: 'baz' } |
| 1034 |
, { name: 'wha', value: 1 } |
| 1035 |
, { name: 'wha', value: 3 } |
| 1036 |
, { name: 'who', value: 'tawoo' } |
| 1037 |
, { name: '$escapable name$', value: 'escapeme' } |
| 1038 |
, { name: 'choices', value: 'two' } |
| 1039 |
, { name: 'opinions', value: 'world peace is not real' } |
| 1040 |
] |
| 1041 |
, i |
| 1042 |
|
| 1043 |
for (i = 0; i < expected.length; i++) { |
| 1044 |
ok(v.some(result, function (v) { |
| 1045 |
return v.name == expected[i].name && v.value == expected[i].value |
| 1046 |
}), 'serialized ' + expected[i].name + ' (' + type + ')') |
| 1047 |
} |
| 1048 |
} |
| 1049 |
|
| 1050 |
function testFormSerializeArray(method, type) { |
| 1051 |
ok(method, 'serialize(..., {type:\'array\'}) bound to context') |
| 1052 |
|
| 1053 |
var result = method ? method(forms[0], { type: 'array' }) : [] |
| 1054 |
if (!result) result = [] |
| 1055 |
|
| 1056 |
verifyFormSerializeArray(result, type) |
| 1057 |
} |
| 1058 |
|
| 1059 |
function testMultiArgumentSerializeArray(method, type, argType) { |
| 1060 |
ok(method, 'serialize(..., {type:\'array\'}) bound to context') |
| 1061 |
var result = method |
| 1062 |
? executeMultiArgumentMethod(method, argType, { type: 'array' }) |
| 1063 |
: [] |
| 1064 |
|
| 1065 |
if (!result) result = [] |
| 1066 |
|
| 1067 |
ok(result.length == 3, 'serialized as array of 3') |
| 1068 |
ok( |
| 1069 |
result.length == 3 |
| 1070 |
&& result[0].name == 'foo' |
| 1071 |
&& result[0].value == 'bar' |
| 1072 |
, 'serialized first element (' + type + ')' |
| 1073 |
) |
| 1074 |
ok( |
| 1075 |
result.length == 3 |
| 1076 |
&& result[1].name == 'bar' |
| 1077 |
&& result[1].value == 'baz' |
| 1078 |
, 'serialized second element (' + type + ')' |
| 1079 |
) |
| 1080 |
ok( |
| 1081 |
result.length == 3 |
| 1082 |
&& result[2].name == 'choices' |
| 1083 |
&& result[2].value == 'two' |
| 1084 |
, 'serialized third element (' + type + ')' |
| 1085 |
) |
| 1086 |
} |
| 1087 |
|
| 1088 |
function testFormSerializeHash(method, type) { |
| 1089 |
var expected = { |
| 1090 |
foo: 'bar' |
| 1091 |
, bar: 'baz' |
| 1092 |
, wha: [ '1', '3' ] |
| 1093 |
, who: 'tawoo' |
| 1094 |
, '$escapable name$': 'escapeme' |
| 1095 |
, choices: 'two' |
| 1096 |
, opinions: 'world peace is not real' |
| 1097 |
} |
| 1098 |
, result |
| 1099 |
|
| 1100 |
ok(method, 'serialize({type:\'map\'}) bound to context') |
| 1101 |
|
| 1102 |
result = method ? method(forms[0], { type: 'map' }) : {} |
| 1103 |
if (!result) result = {} |
| 1104 |
|
| 1105 |
ok( |
| 1106 |
v.keys(expected).length === v.keys(result).length |
| 1107 |
, 'same number of keys (' + type + ')' |
| 1108 |
) |
| 1109 |
|
| 1110 |
v.each(v.keys(expected), function (k) { |
| 1111 |
ok( |
| 1112 |
sameValue(expected[k], result[k]) |
| 1113 |
, 'same value for ' + k + ' (' + type + ')' |
| 1114 |
) |
| 1115 |
}) |
| 1116 |
} |
| 1117 |
|
| 1118 |
function testMultiArgumentSerializeHash(method, type, argType) { |
| 1119 |
ok(method, 'serialize({type:\'map\'}) bound to context') |
| 1120 |
var result = method |
| 1121 |
? executeMultiArgumentMethod(method, argType, { type: 'map' }) |
| 1122 |
: {} |
| 1123 |
if (!result) result = {} |
| 1124 |
ok(result.foo == 'bar', 'serialized first element (' + type + ')') |
| 1125 |
ok(result.bar == 'baz', 'serialized second element (' + type + ')') |
| 1126 |
ok(result.choices == 'two', 'serialized third element (' + type + ')') |
| 1127 |
} |
| 1128 |
|
| 1129 |
return { |
| 1130 |
reset: reset |
| 1131 |
, formElements: formElements |
| 1132 |
, testInput: testInput |
| 1133 |
, testFormSerialize: testFormSerialize |
| 1134 |
, testMultiArgumentSerialize: testMultiArgumentSerialize |
| 1135 |
, testFormSerializeArray: testFormSerializeArray |
| 1136 |
, verifyFormSerializeArray: verifyFormSerializeArray |
| 1137 |
, testMultiArgumentSerializeArray: testMultiArgumentSerializeArray |
| 1138 |
, testFormSerializeHash: testFormSerializeHash |
| 1139 |
, testMultiArgumentSerializeHash: testMultiArgumentSerializeHash |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
sink('Serializing', function (test, ok) { |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
var sHelper = createSerializeHelper(ok) |
| 1160 |
sHelper.reset() |
| 1161 |
|
| 1162 |
test('correctly serialize textarea', function (complete) { |
| 1163 |
var textarea = sHelper.formElements(1, 'textarea', 0) |
| 1164 |
, sa |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
ok( |
| 1169 |
'T3=%3F%0D%0AA+B%0D%0AZ' == ajax.serialize(textarea) |
| 1170 |
, 'serialize(textarea)' |
| 1171 |
) |
| 1172 |
sa = ajax.serialize(textarea, { type: 'array' }) |
| 1173 |
ok(sa.length == 1, 'serialize(textarea, {type:\'array\'}) returns array') |
| 1174 |
sa = sa[0] |
| 1175 |
ok('T3' == sa.name, 'serialize(textarea, {type:\'array\'}).name') |
| 1176 |
ok( |
| 1177 |
'?\r\nA B\r\nZ' == sa.value |
| 1178 |
, 'serialize(textarea, {type:\'array\'}).value' |
| 1179 |
) |
| 1180 |
ok( |
| 1181 |
'?\r\nA B\r\nZ' == ajax.serialize(textarea, { type: 'map' }).T3 |
| 1182 |
, 'serialize(textarea, {type:\'map\'})' |
| 1183 |
) |
| 1184 |
complete() |
| 1185 |
}) |
| 1186 |
|
| 1187 |
test('correctly serialize input[type=hidden]', function (complete) { |
| 1188 |
sHelper.testInput( |
| 1189 |
sHelper.formElements(1, 'input', 0) |
| 1190 |
, 'H1' |
| 1191 |
, 'x' |
| 1192 |
, 'hidden' |
| 1193 |
) |
| 1194 |
sHelper.testInput( |
| 1195 |
sHelper.formElements(1, 'input', 1) |
| 1196 |
, 'H2' |
| 1197 |
, '' |
| 1198 |
, 'hidden[no value]' |
| 1199 |
) |
| 1200 |
complete() |
| 1201 |
}) |
| 1202 |
|
| 1203 |
test('correctly serialize input[type=password]', function (complete) { |
| 1204 |
sHelper.testInput( |
| 1205 |
sHelper.formElements(1, 'input', 2) |
| 1206 |
, 'PWD1' |
| 1207 |
, 'xyz' |
| 1208 |
, 'password' |
| 1209 |
) |
| 1210 |
sHelper.testInput( |
| 1211 |
sHelper.formElements(1, 'input', 3) |
| 1212 |
, 'PWD2' |
| 1213 |
, '' |
| 1214 |
, 'password[no value]' |
| 1215 |
) |
| 1216 |
complete() |
| 1217 |
}) |
| 1218 |
|
| 1219 |
test('correctly serialize input[type=text]', function (complete) { |
| 1220 |
sHelper.testInput( |
| 1221 |
sHelper.formElements(1, 'input', 4) |
| 1222 |
, 'T1' |
| 1223 |
, '' |
| 1224 |
, 'text[no value]' |
| 1225 |
) |
| 1226 |
sHelper.testInput( |
| 1227 |
sHelper.formElements(1, 'input', 5) |
| 1228 |
, 'T2' |
| 1229 |
, 'YES' |
| 1230 |
, 'text[readonly]' |
| 1231 |
) |
| 1232 |
sHelper.testInput( |
| 1233 |
sHelper.formElements(1, 'input', 10) |
| 1234 |
, 'My Name' |
| 1235 |
, 'me' |
| 1236 |
, 'text[space name]' |
| 1237 |
) |
| 1238 |
complete() |
| 1239 |
}) |
| 1240 |
|
| 1241 |
test('correctly serialize input[type=checkbox]', function (complete) { |
| 1242 |
var cb1 = sHelper.formElements(1, 'input', 6) |
| 1243 |
, cb2 = sHelper.formElements(1, 'input', 7) |
| 1244 |
sHelper.testInput(cb1, 'C1', null, 'checkbox[not checked]') |
| 1245 |
cb1.checked = true |
| 1246 |
sHelper.testInput(cb1, 'C1', '1', 'checkbox[checked]') |
| 1247 |
|
| 1248 |
|
| 1249 |
sHelper.testInput(cb2, 'C2', null, 'checkbox[no value, not checked]') |
| 1250 |
cb2.checked = true |
| 1251 |
sHelper.testInput(cb2, 'C2', 'on', 'checkbox[no value, checked]') |
| 1252 |
complete() |
| 1253 |
}) |
| 1254 |
|
| 1255 |
test('correctly serialize input[type=radio]', function (complete) { |
| 1256 |
var r1 = sHelper.formElements(1, 'input', 8) |
| 1257 |
, r2 = sHelper.formElements(1, 'input', 9) |
| 1258 |
sHelper.testInput(r1, 'R1', null, 'radio[not checked]') |
| 1259 |
r1.checked = true |
| 1260 |
sHelper.testInput(r1, 'R1', '1', 'radio[not checked]') |
| 1261 |
sHelper.testInput(r2, 'R1', null, 'radio[no value, not checked]') |
| 1262 |
r2.checked = true |
| 1263 |
sHelper.testInput(r2, 'R1', '', 'radio[no value, checked]') |
| 1264 |
complete() |
| 1265 |
}) |
| 1266 |
|
| 1267 |
test('correctly serialize input[type=reset]', function (complete) { |
| 1268 |
sHelper.testInput( |
| 1269 |
sHelper.formElements(1, 'input', 11) |
| 1270 |
, 'rst' |
| 1271 |
, null |
| 1272 |
, 'reset' |
| 1273 |
) |
| 1274 |
complete() |
| 1275 |
}) |
| 1276 |
|
| 1277 |
test('correctly serialize input[type=file]', function (complete) { |
| 1278 |
sHelper.testInput( |
| 1279 |
sHelper.formElements(1, 'input', 12) |
| 1280 |
, 'file' |
| 1281 |
, null |
| 1282 |
, 'file' |
| 1283 |
) |
| 1284 |
complete() |
| 1285 |
}) |
| 1286 |
|
| 1287 |
test('correctly serialize input[type=submit]', function (complete) { |
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
sHelper.testInput( |
| 1293 |
sHelper.formElements(1, 'input', 13) |
| 1294 |
, 'sub' |
| 1295 |
, 'NO' |
| 1296 |
, 'submit' |
| 1297 |
) |
| 1298 |
complete() |
| 1299 |
}) |
| 1300 |
|
| 1301 |
test('correctly serialize select with no options', function (complete) { |
| 1302 |
var select = sHelper.formElements(1, 'select', 0) |
| 1303 |
sHelper.testInput(select, 'S1', null, 'select, no options') |
| 1304 |
complete() |
| 1305 |
}) |
| 1306 |
|
| 1307 |
test('correctly serialize select with values', function (complete) { |
| 1308 |
var select = sHelper.formElements(1, 'select', 1) |
| 1309 |
sHelper.testInput(select, 'S2', 'abc', 'select option 1 (default)') |
| 1310 |
select.selectedIndex = 1 |
| 1311 |
sHelper.testInput(select, 'S2', 'def', 'select option 2') |
| 1312 |
select.selectedIndex = 6 |
| 1313 |
sHelper.testInput(select, 'S2', 'disco stu', 'select option 7') |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
select.selectedIndex = 9 |
| 1318 |
sHelper.testInput( |
| 1319 |
select |
| 1320 |
, 'S2' |
| 1321 |
, '' |
| 1322 |
, 'select option 9, value="" should yield ""' |
| 1323 |
) |
| 1324 |
select.selectedIndex = -1 |
| 1325 |
sHelper.testInput(select, 'S2', null, 'select, unselected') |
| 1326 |
complete() |
| 1327 |
}) |
| 1328 |
|
| 1329 |
test('correctly serialize select without explicit values' |
| 1330 |
, function (complete) { |
| 1331 |
|
| 1332 |
var select = sHelper.formElements(1, 'select', 2) |
| 1333 |
sHelper.testInput(select, 'S3', 'ABC', 'select option 1 (default)') |
| 1334 |
select.selectedIndex = 1 |
| 1335 |
sHelper.testInput(select, 'S3', 'DEF', 'select option 2') |
| 1336 |
select.selectedIndex = 6 |
| 1337 |
sHelper.testInput(select, 'S3', 'DISCO STU!', 'select option 7') |
| 1338 |
select.selectedIndex = -1 |
| 1339 |
sHelper.testInput(select, 'S3', null, 'select, unselected') |
| 1340 |
complete() |
| 1341 |
}) |
| 1342 |
|
| 1343 |
test('correctly serialize select multiple', function (complete) { |
| 1344 |
var select = sHelper.formElements(1, 'select', 3) |
| 1345 |
sHelper.testInput(select, 'S4', null, 'select, unselected (default)') |
| 1346 |
select.options[1].selected = true |
| 1347 |
sHelper.testInput(select, 'S4', '2', 'select option 2') |
| 1348 |
select.options[3].selected = true |
| 1349 |
sHelper.testInput(select, 'S4', [ '2', '4' ], 'select options 2 & 4') |
| 1350 |
select.options[8].selected = true |
| 1351 |
sHelper.testInput( |
| 1352 |
select |
| 1353 |
, 'S4' |
| 1354 |
, [ '2', '4', 'Disco Stu!' ] |
| 1355 |
, 'select option 2 & 4 & 9' |
| 1356 |
) |
| 1357 |
select.options[3].selected = false |
| 1358 |
sHelper.testInput( |
| 1359 |
select |
| 1360 |
, 'S4' |
| 1361 |
, [ '2', 'Disco Stu!' ] |
| 1362 |
, 'select option 2 & 9' |
| 1363 |
) |
| 1364 |
select.options[1].selected = false |
| 1365 |
select.options[8].selected = false |
| 1366 |
sHelper.testInput(select, 'S4', null, 'select, all unselected') |
| 1367 |
complete() |
| 1368 |
}) |
| 1369 |
|
| 1370 |
test('correctly serialize options', function (complete) { |
| 1371 |
var option = sHelper.formElements(1, 'select', 1).options[6] |
| 1372 |
sHelper.testInput( |
| 1373 |
option |
| 1374 |
, '-' |
| 1375 |
, null |
| 1376 |
, 'just option (with value), shouldn\'t serialize' |
| 1377 |
) |
| 1378 |
|
| 1379 |
option = sHelper.formElements(1, 'select', 2).options[6] |
| 1380 |
sHelper.testInput( |
| 1381 |
option |
| 1382 |
, '-' |
| 1383 |
, null |
| 1384 |
, 'option (without value), shouldn\'t serialize' |
| 1385 |
) |
| 1386 |
|
| 1387 |
complete() |
| 1388 |
}) |
| 1389 |
|
| 1390 |
test('correctly serialize disabled', function (complete) { |
| 1391 |
var input = sHelper.formElements(1, 'input', 14) |
| 1392 |
, select |
| 1393 |
|
| 1394 |
sHelper.testInput(input, 'D1', null, 'disabled text input') |
| 1395 |
input = sHelper.formElements(1, 'input', 15) |
| 1396 |
sHelper.testInput(input, 'D2', null, 'disabled checkbox') |
| 1397 |
input = sHelper.formElements(1, 'input', 16) |
| 1398 |
sHelper.testInput(input, 'D3', null, 'disabled radio') |
| 1399 |
|
| 1400 |
select = sHelper.formElements(1, 'select', 4) |
| 1401 |
sHelper.testInput(select, 'D4', null, 'disabled select') |
| 1402 |
select = sHelper.formElements(1, 'select', 3) |
| 1403 |
sHelper.testInput(select, 'D5', null, 'disabled select option') |
| 1404 |
select = sHelper.formElements(1, 'select', 6) |
| 1405 |
sHelper.testInput(select, 'D6', null, 'disabled multi select') |
| 1406 |
select = sHelper.formElements(1, 'select', 7) |
| 1407 |
sHelper.testInput(select, 'D7', null, 'disabled multi select option') |
| 1408 |
complete() |
| 1409 |
}) |
| 1410 |
|
| 1411 |
test('serialize(form)', function (complete) { |
| 1412 |
sHelper.testFormSerialize(ajax.serialize, 'direct') |
| 1413 |
complete() |
| 1414 |
}) |
| 1415 |
|
| 1416 |
test('serialize(form, {type:\'array\'})', function (complete) { |
| 1417 |
sHelper.testFormSerializeArray(ajax.serialize, 'direct') |
| 1418 |
complete() |
| 1419 |
}) |
| 1420 |
|
| 1421 |
test('serialize(form, {type:\'map\'})', function (complete) { |
| 1422 |
sHelper.testFormSerializeHash(ajax.serialize, 'direct') |
| 1423 |
complete() |
| 1424 |
}) |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
test('serialize(element, element, element...)', function (complete) { |
| 1429 |
sHelper.testMultiArgumentSerialize(ajax.serialize, 'direct', PASS_ARGS) |
| 1430 |
complete() |
| 1431 |
}) |
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
test('serialize(element, element, element..., {type:\'array\'})' |
| 1437 |
, function (complete) { |
| 1438 |
sHelper.testMultiArgumentSerializeArray( |
| 1439 |
ajax.serialize |
| 1440 |
, 'direct' |
| 1441 |
, PASS_ARGS |
| 1442 |
) |
| 1443 |
complete() |
| 1444 |
}) |
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
test('serialize(element, element, element...)', function (complete) { |
| 1450 |
sHelper.testMultiArgumentSerializeHash( |
| 1451 |
ajax.serialize |
| 1452 |
, 'direct' |
| 1453 |
, PASS_ARGS |
| 1454 |
) |
| 1455 |
complete() |
| 1456 |
}) |
| 1457 |
|
| 1458 |
test('toQueryString([{ name: x, value: y }, ... ]) name/value array' |
| 1459 |
, function (complete) { |
| 1460 |
|
| 1461 |
var arr = [ |
| 1462 |
{ name: 'foo', value: 'bar' } |
| 1463 |
, { name: 'baz', value: '' } |
| 1464 |
, { name: 'x', value: -20 } |
| 1465 |
, { name: 'x', value: 20 } |
| 1466 |
] |
| 1467 |
|
| 1468 |
ok(ajax.toQueryString(arr) == 'foo=bar&baz=&x=-20&x=20', 'simple') |
| 1469 |
|
| 1470 |
arr = [ |
| 1471 |
{ name: 'dotted.name.intact', value: '$@%' } |
| 1472 |
, { name: '$ $', value: 20 } |
| 1473 |
, { name: 'leave britney alone', value: 'waa haa haa' } |
| 1474 |
] |
| 1475 |
|
| 1476 |
ok( |
| 1477 |
ajax.toQueryString(arr) == |
| 1478 |
'dotted.name.intact=%24%40%25&%24+%24=20' |
| 1479 |
+ '&leave+britney+alone=waa+haa+haa' |
| 1480 |
, 'escaping required' |
| 1481 |
) |
| 1482 |
|
| 1483 |
complete() |
| 1484 |
}) |
| 1485 |
|
| 1486 |
test('toQueryString({name: value,...} complex object', function (complete) { |
| 1487 |
var obj = { 'foo': 'bar', 'baz': '', 'x': -20 } |
| 1488 |
|
| 1489 |
ok(ajax.toQueryString(obj) == 'foo=bar&baz=&x=-20', 'simple') |
| 1490 |
|
| 1491 |
obj = { |
| 1492 |
'dotted.name.intact': '$@%' |
| 1493 |
, '$ $': 20 |
| 1494 |
, 'leave britney alone': 'waa haa haa' |
| 1495 |
} |
| 1496 |
ok( |
| 1497 |
ajax.toQueryString(obj) == |
| 1498 |
'dotted.name.intact=%24%40%25&%24+%24=20' |
| 1499 |
+ '&leave+britney+alone=waa+haa+haa' |
| 1500 |
, 'escaping required' |
| 1501 |
) |
| 1502 |
|
| 1503 |
complete() |
| 1504 |
}) |
| 1505 |
|
| 1506 |
test('toQueryString({name: [ value1, value2 ...],...} object with arrays', function (complete) { |
| 1507 |
var obj = { 'foo': 'bar', 'baz': [ '', '', 'boo!' ], 'x': [ -20, 2.2, 20 ] } |
| 1508 |
ok(ajax.toQueryString(obj, true) == "foo=bar&baz=&baz=&baz=boo!&x=-20&x=2.2&x=20", "object with arrays") |
| 1509 |
ok(ajax.toQueryString(obj) == "foo=bar&baz%5B%5D=&baz%5B%5D=&baz%5B%5D=boo!&x%5B%5D=-20&x%5B%5D=2.2&x%5B%5D=20") |
| 1510 |
complete() |
| 1511 |
}) |
| 1512 |
|
| 1513 |
test('toQueryString({name: { nestedName: value },...} object with objects', function(complete) { |
| 1514 |
var obj = { 'foo': { 'bar': 'baz' }, 'x': [ { 'bar': 'baz' }, { 'boo': 'hiss' } ] } |
| 1515 |
ok(ajax.toQueryString(obj) == "foo%5Bbar%5D=baz&x%5B0%5D%5Bbar%5D=baz&x%5B1%5D%5Bboo%5D=hiss", "object with objects") |
| 1516 |
complete() |
| 1517 |
}) |
| 1518 |
|
| 1519 |
}) |
| 1520 |
|
| 1521 |
sink('Ender Integration', function (test, ok) { |
| 1522 |
var sHelper = createSerializeHelper(ok) |
| 1523 |
sHelper.reset() |
| 1524 |
|
| 1525 |
test('$.ajax alias for reqwest, not bound to boosh', 1, function () { |
| 1526 |
ok(ender.ajax === ajax, '$.ajax is reqwest') |
| 1527 |
}) |
| 1528 |
|
| 1529 |
|
| 1530 |
test('$.serialize(form)', function (complete) { |
| 1531 |
sHelper.testFormSerialize(ender.serialize, 'ender') |
| 1532 |
complete() |
| 1533 |
}) |
| 1534 |
|
| 1535 |
|
| 1536 |
test('$.serialize(form, {type:\'array\'})', function (complete) { |
| 1537 |
sHelper.testFormSerializeArray(ender.serialize, 'ender') |
| 1538 |
complete() |
| 1539 |
}) |
| 1540 |
|
| 1541 |
|
| 1542 |
test('$.serialize(form, {type:\'map\'})', function (complete) { |
| 1543 |
sHelper.testFormSerializeHash(ender.serialize, 'ender') |
| 1544 |
complete() |
| 1545 |
}) |
| 1546 |
|
| 1547 |
|
| 1548 |
test('$.serializeArray(...) alias for serialize(..., {type:\'map\'}' |
| 1549 |
, function (complete) { |
| 1550 |
sHelper.verifyFormSerializeArray( |
| 1551 |
ender.serializeArray(document.forms[0]) |
| 1552 |
, 'ender' |
| 1553 |
) |
| 1554 |
complete() |
| 1555 |
}) |
| 1556 |
|
| 1557 |
test('$.serialize(element, element, element...)', function (complete) { |
| 1558 |
sHelper.testMultiArgumentSerialize(ender.serialize, 'ender', PASS_ARGS) |
| 1559 |
complete() |
| 1560 |
}) |
| 1561 |
|
| 1562 |
test('$.serialize(element, element, element..., {type:\'array\'})' |
| 1563 |
, function (complete) { |
| 1564 |
sHelper.testMultiArgumentSerializeArray( |
| 1565 |
ender.serialize |
| 1566 |
, 'ender' |
| 1567 |
, PASS_ARGS |
| 1568 |
) |
| 1569 |
complete() |
| 1570 |
}) |
| 1571 |
|
| 1572 |
test('$.serialize(element, element, element..., {type:\'map\'})' |
| 1573 |
, function (complete) { |
| 1574 |
sHelper.testMultiArgumentSerializeHash( |
| 1575 |
ender.serialize |
| 1576 |
, 'ender' |
| 1577 |
, PASS_ARGS |
| 1578 |
) |
| 1579 |
complete() |
| 1580 |
}) |
| 1581 |
|
| 1582 |
test('$(element, element, element...).serialize()', function (complete) { |
| 1583 |
sHelper.testMultiArgumentSerialize(ender.fn.serialize, 'ender', BIND_ARGS) |
| 1584 |
complete() |
| 1585 |
}) |
| 1586 |
|
| 1587 |
test('$(element, element, element...).serialize({type:\'array\'})' |
| 1588 |
, function (complete) { |
| 1589 |
sHelper.testMultiArgumentSerializeArray( |
| 1590 |
ender.fn.serialize |
| 1591 |
, 'ender' |
| 1592 |
, BIND_ARGS |
| 1593 |
) |
| 1594 |
complete() |
| 1595 |
}) |
| 1596 |
|
| 1597 |
test('$(element, element, element...).serialize({type:\'map\'})' |
| 1598 |
, function (complete) { |
| 1599 |
sHelper.testMultiArgumentSerializeHash( |
| 1600 |
ender.fn.serialize |
| 1601 |
, 'ender' |
| 1602 |
, BIND_ARGS |
| 1603 |
) |
| 1604 |
complete() |
| 1605 |
}) |
| 1606 |
|
| 1607 |
test('$.toQueryString alias for reqwest.toQueryString, not bound to boosh' |
| 1608 |
, function (complete) { |
| 1609 |
ok( |
| 1610 |
ender.toQueryString === ajax.toQueryString |
| 1611 |
, '$.toQueryString is reqwest.toQueryString' |
| 1612 |
) |
| 1613 |
complete() |
| 1614 |
}) |
| 1615 |
}) |
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
sink('Promises', function (test, ok) { |
| 1622 |
|
| 1623 |
test('always callback is called', function (complete) { |
| 1624 |
ajax({ |
| 1625 |
url: '/tests/fixtures/fixtures.js' |
| 1626 |
}) |
| 1627 |
.always(function () { |
| 1628 |
ok(true, 'called complete') |
| 1629 |
complete() |
| 1630 |
}) |
| 1631 |
}) |
| 1632 |
|
| 1633 |
test('success and error handlers are called', 3, function () { |
| 1634 |
ajax({ |
| 1635 |
url: '/tests/fixtures/invalidJSON.json' |
| 1636 |
, type: 'json' |
| 1637 |
}) |
| 1638 |
.then( |
| 1639 |
function () { |
| 1640 |
ok(false, 'success callback fired') |
| 1641 |
} |
| 1642 |
, function (resp, msg) { |
| 1643 |
ok( |
| 1644 |
msg == 'Could not parse JSON in response' |
| 1645 |
, 'error callback fired' |
| 1646 |
) |
| 1647 |
} |
| 1648 |
) |
| 1649 |
|
| 1650 |
ajax({ |
| 1651 |
url: '/tests/fixtures/invalidJSON.json' |
| 1652 |
, type: 'json' |
| 1653 |
}) |
| 1654 |
.fail(function (resp, msg) { |
| 1655 |
ok(msg == 'Could not parse JSON in response', 'fail callback fired') |
| 1656 |
}) |
| 1657 |
|
| 1658 |
ajax({ |
| 1659 |
url: '/tests/fixtures/fixtures.json' |
| 1660 |
, type: 'json' |
| 1661 |
}) |
| 1662 |
.then( |
| 1663 |
function () { |
| 1664 |
ok(true, 'success callback fired') |
| 1665 |
} |
| 1666 |
, function () { |
| 1667 |
ok(false, 'error callback fired') |
| 1668 |
} |
| 1669 |
) |
| 1670 |
}) |
| 1671 |
|
| 1672 |
test('then is chainable', 2, function () { |
| 1673 |
ajax({ |
| 1674 |
url: '/tests/fixtures/fixtures.json' |
| 1675 |
, type: 'json' |
| 1676 |
}) |
| 1677 |
.then( |
| 1678 |
function (resp) { |
| 1679 |
ok(true, 'first success callback fired') |
| 1680 |
return 'new value'; |
| 1681 |
} |
| 1682 |
) |
| 1683 |
.then( |
| 1684 |
function (resp) { |
| 1685 |
ok(resp === 'new value', 'second success callback fired') |
| 1686 |
} |
| 1687 |
) |
| 1688 |
}) |
| 1689 |
|
| 1690 |
test('success does not chain with then', 2, function () { |
| 1691 |
ajax({ |
| 1692 |
url: '/tests/fixtures/fixtures.json' |
| 1693 |
, type: 'json' |
| 1694 |
, success: function() { |
| 1695 |
ok(true, 'success callback fired') |
| 1696 |
return 'some independent value'; |
| 1697 |
} |
| 1698 |
}) |
| 1699 |
.then( |
| 1700 |
function (resp) { |
| 1701 |
ok( |
| 1702 |
resp && resp !== 'some independent value' |
| 1703 |
, 'then callback fired' |
| 1704 |
) |
| 1705 |
} |
| 1706 |
) |
| 1707 |
}) |
| 1708 |
|
| 1709 |
test('then & always handlers can be added after a response is received' |
| 1710 |
, 2 |
| 1711 |
, function () { |
| 1712 |
|
| 1713 |
var a = ajax({ |
| 1714 |
url: '/tests/fixtures/fixtures.json' |
| 1715 |
, type: 'json' |
| 1716 |
}) |
| 1717 |
.always(function () { |
| 1718 |
setTimeout(function () { |
| 1719 |
a.then( |
| 1720 |
function () { |
| 1721 |
ok(true, 'success callback called') |
| 1722 |
} |
| 1723 |
, function () { |
| 1724 |
ok(false, 'error callback called') |
| 1725 |
} |
| 1726 |
).always(function () { |
| 1727 |
ok(true, 'complete callback called') |
| 1728 |
}) |
| 1729 |
}, 1) |
| 1730 |
}) |
| 1731 |
}) |
| 1732 |
|
| 1733 |
test('then is chainable after a response is received' |
| 1734 |
, 2 |
| 1735 |
, function () { |
| 1736 |
|
| 1737 |
var a = ajax({ |
| 1738 |
url: '/tests/fixtures/fixtures.json' |
| 1739 |
, type: 'json' |
| 1740 |
}) |
| 1741 |
.always(function () { |
| 1742 |
setTimeout(function () { |
| 1743 |
a.then(function () { |
| 1744 |
ok(true, 'first success callback called') |
| 1745 |
return 'new value'; |
| 1746 |
}).then(function (resp) { |
| 1747 |
ok(resp === 'new value', 'second success callback called') |
| 1748 |
}) |
| 1749 |
}, 1) |
| 1750 |
}) |
| 1751 |
}) |
| 1752 |
|
| 1753 |
test('failure handlers can be added after a response is received' |
| 1754 |
, function (complete) { |
| 1755 |
|
| 1756 |
var a = ajax({ |
| 1757 |
url: '/tests/fixtures/invalidJSON.json' |
| 1758 |
, type: 'json' |
| 1759 |
}) |
| 1760 |
.always(function () { |
| 1761 |
setTimeout(function () { |
| 1762 |
a |
| 1763 |
.fail(function () { |
| 1764 |
ok(true, 'fail callback called') |
| 1765 |
complete() |
| 1766 |
}) |
| 1767 |
}, 1) |
| 1768 |
}) |
| 1769 |
}) |
| 1770 |
|
| 1771 |
test('.then success and fail are optional parameters', 1, function () { |
| 1772 |
try { |
| 1773 |
ajax({ |
| 1774 |
url: '/tests/fixtures/invalidJSON.json' |
| 1775 |
, type: 'json' |
| 1776 |
}) |
| 1777 |
.then() |
| 1778 |
} catch (ex) { |
| 1779 |
ok(false, '.then() parameters should be optional') |
| 1780 |
} finally { |
| 1781 |
ok(true, 'passed .then() optional parameters') |
| 1782 |
} |
| 1783 |
}) |
| 1784 |
|
| 1785 |
}) |
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
sink('Timeout', function (test, ok) { |
| 1790 |
test('xmlHttpRequest', function (complete) { |
| 1791 |
var ts = +new Date() |
| 1792 |
ajax({ |
| 1793 |
url: '/tests/timeout' |
| 1794 |
, type: 'json' |
| 1795 |
, timeout: 250 |
| 1796 |
, error: function (err, msg) { |
| 1797 |
ok(err, 'received error response') |
| 1798 |
try { |
| 1799 |
ok(err && err.status === 0, 'correctly caught timeout') |
| 1800 |
ok(msg && msg === 'Request is aborted: timeout', 'timeout message received') |
| 1801 |
} catch (e) { |
| 1802 |
ok(true, 'IE is a troll') |
| 1803 |
} |
| 1804 |
var tt = Math.abs(+new Date() - ts) |
| 1805 |
ok( |
| 1806 |
tt > 200 && tt < 300 |
| 1807 |
, 'timeout close enough to 250 (' + tt + ')' |
| 1808 |
) |
| 1809 |
complete() |
| 1810 |
} |
| 1811 |
}) |
| 1812 |
}) |
| 1813 |
|
| 1814 |
test('jsonpRequest', function (complete) { |
| 1815 |
var ts = +new Date() |
| 1816 |
ajax({ |
| 1817 |
url: '/tests/timeout' |
| 1818 |
, type: 'jsonp' |
| 1819 |
, timeout: 250 |
| 1820 |
, error: function (err) { |
| 1821 |
ok(err, 'received error response') |
| 1822 |
var tt = Math.abs(+new Date() - ts) |
| 1823 |
ok( |
| 1824 |
tt > 200 && tt < 300 |
| 1825 |
, 'timeout close enough to 250 (' + tt + ')' |
| 1826 |
) |
| 1827 |
complete() |
| 1828 |
} |
| 1829 |
}) |
| 1830 |
}) |
| 1831 |
}) |
| 1832 |
|
| 1833 |
start() |
| 1834 |
|
| 1835 |
}(reqwest)) |