142
St
startsInvasion / files
startsInvasion / libs / node_modules / reqwest / tests / tests.js
4.7 KB
1sink('Mime types', function (test, ok) { 2 test('JSON', 1, function() { 3 reqwest({ 4 url: '/tests/fixtures/fixtures.json', 5 type: 'json', 6 success: function (resp) { 7 ok(resp.boosh == 'boosh', 'evaluated response as JSON'); 8 } 9 }); 10 }); 11 12 // For some reason, using the .jsonp file extension didn't work 13 // in the testing suite. Using .js instead for now. 14 test('JSONP', 3, function() { 15 reqwest({ 16 url: '/tests/fixtures/fixtures_jsonp.js?callback=?', 17 type: 'jsonp', 18 success: function (resp) { 19 ok(resp.boosh == "boosh", "evaluated response as JSONP"); 20 } 21 }); 22 23 reqwest({ 24 url: '/tests/fixtures/fixtures_jsonp2.js?foo=bar', 25 type: 'jsonp', 26 jsonpCallback: 'foo', 27 success: function (resp) { 28 ok(resp.boosh == "boosh", "evaluated response as JSONP with custom callback"); 29 } 30 }); 31 32 reqwest({ 33 url: '/tests/fixtures/fixtures_jsonp3.js?foo=?', 34 type: 'jsonp', 35 jsonpCallback: 'foo', 36 success: function (resp) { 37 ok(resp.boosh == "boosh", "evaluated response as JSONP with custom wildcard callback"); 38 } 39 }); 40 }); 41 42 test('JS', 1, function() { 43 reqwest({ 44 url: '/tests/fixtures/fixtures.js', 45 type: 'js', 46 success: function (resp) { 47 ok(boosh == 'boosh', 'evaluated response as JavaScript'); 48 } 49 }); 50 }); 51 52 test('HTML', 1, function() { 53 reqwest({ 54 url: '/tests/fixtures/fixtures.html', 55 type: 'html', 56 success: function (resp) { 57 ok(resp == '<p>boosh</p>', 'evaluated response as HTML'); 58 } 59 }); 60 }); 61 62}); 63 64sink('Callbacks', function (test, ok) { 65 66 test('no callbacks', 1, function () { 67 var pass = true; 68 try { 69 reqwest('/tests/fixtures/fixtures.js'); 70 } catch (ex) { 71 pass = false; 72 } finally { 73 ok(pass, 'successfully doesnt fail without callback'); 74 } 75 }); 76 77 test('complete is called', 1, function () { 78 reqwest({ 79 url: '/tests/fixtures/fixtures.js', 80 complete: function () { 81 ok(true, 'called complete'); 82 } 83 }); 84 }); 85 86}); 87 88sink('Connection Object', function (test, ok) { 89 90 test('setRequestHeaders', 1, function () { 91 reqwest({ 92 url: '/tests/fixtures/fixtures.html', 93 data: 'foo=bar&baz=thunk', 94 method: 'post', 95 headers: { 96 'Accept': 'application/x-foo' 97 }, 98 success: function (resp) { 99 ok(true, 'can post headers'); 100 } 101 }); 102 }); 103 104 test('can inspect http before send', 2, function () { 105 var connection = reqwest({ 106 url: '/tests/fixtures/fixtures.js', 107 method: 'post', 108 type: 'js', 109 before: function (http) { 110 ok(http.readyState == 1, 'received http connection object'); 111 }, 112 success: function () { 113 ok(connection.request.readyState == 4, 'success callback has readyState of 4'); 114 } 115 }); 116 }); 117 118 sink('Serializing', function (test, ok) { 119 120 test('serialize', 1, function () { 121 var expected = 'foo=bar&bar=baz&wha=1&wha=3&choices=two&opinions=world%20peace%20is%20not%20real'; 122 ok(reqwest.serialize(document.forms[0]) == expected, 'serialized form'); 123 }); 124 125 test('serializeArray', 6, function () { 126 var expected = [ 127 { name: 'foo', value: 'bar' }, 128 { name: 'bar', value: 'baz' }, 129 { name: 'wha', value: 1 }, 130 { name: 'wha', value: 3 }, 131 { name: 'choices', value: 'two' }, 132 { name: 'opinions', value: 'world%20peace%20is%20not%20real' } 133 ]; 134 135 var result = reqwest.serializeArray(document.forms[0]); 136 137 for (var i = 0; i < expected.length; i++) { 138 ok(result.some(function (v) { 139 return v.name == expected[i].name && v.value == expected[i].value; 140 }), 'serialized ' + result[i].name); 141 } 142 }); 143 144 }); 145 146 147 sink('Parallel Calls', function (test, ok) { 148 149 test('multiple named callbacks', 4, function () { 150 reqwest({ 151 url: '/tests/fixtures/fixtures_jsonp_multi.js?callback=reqwest_0', 152 type: 'jsonp', 153 success: function (resp) { 154 ok(resp.a == "a", "evaluated response as JSONP"); 155 } 156 }); 157 reqwest({ 158 url: '/tests/fixtures/fixtures_jsonp_multi_b.js?callback=reqwest_0', 159 type: 'jsonp', 160 success: function (resp) { 161 ok(resp.b == "b", "evaluated response as JSONP"); 162 } 163 }); 164 reqwest({ 165 url: '/tests/fixtures/fixtures_jsonp_multi_c.js?callback=reqwest_0', 166 type: 'jsonp', 167 success: function (resp) { 168 ok(resp.c == "c", "evaluated response as JSONP"); 169 } 170 }); 171 reqwest({ 172 url: '/tests/fixtures/fixtures_jsonp_multi.js?callback=reqwest_0', 173 type: 'jsonp', 174 success: function (resp) { 175 ok(resp.a == "a", "evaluated response as JSONP"); 176 } 177 }); 178 }); 179 180 }); 181 182}); 183 184start();