OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>FormData interface</title> |
| 3 <script src="../../resources/testharness.js"></script> |
| 4 <script src="../../resources/testharnessreport.js"></script> |
| 5 <script> |
| 6 |
| 7 // Helpers |
| 8 |
| 9 function serializeFormData(formData) |
| 10 { |
| 11 return new Promise(function(resolve, reject) { |
| 12 var xhr = new XMLHttpRequest(), async = true; |
| 13 xhr.open( |
| 14 'POST', |
| 15 'http://127.0.0.1:8000/xmlhttprequest/resources/multipart-post-echo.
php', |
| 16 async); |
| 17 xhr.send(formData); |
| 18 xhr.onreadystatechange = function() { |
| 19 if (xhr.readyState !== XMLHttpRequest.DONE) |
| 20 return; |
| 21 if (xhr.status === 200) { |
| 22 resolve(xhr.responseText); |
| 23 } else { |
| 24 reject(Error(xhr.statusText)); |
| 25 } |
| 26 }; |
| 27 }); |
| 28 } |
| 29 |
| 30 function checkFormData(test, formData, expected, message) |
| 31 { |
| 32 message = message || 'Serialized FormData should match'; |
| 33 return serializeFormData(formData) |
| 34 .then(test.step_func(function(value) { |
| 35 assert_equals(value, expected, message); |
| 36 test.done(); |
| 37 })) |
| 38 .catch(test.unreached_func('Failed to fetch form data')); |
| 39 } |
| 40 |
| 41 function blobAsString(blob) |
| 42 { |
| 43 return new Promise(function(resolve, reject) { |
| 44 var reader = new FileReader(); |
| 45 reader.readAsText(blob); |
| 46 reader.onload = function(e) { resolve(reader.result); }; |
| 47 reader.onerror = function(e) { reject(reader.error); }; |
| 48 }); |
| 49 } |
| 50 |
| 51 function checkBlobData(test, blob, expected, message) |
| 52 { |
| 53 message = message || 'Blob data should match'; |
| 54 return blobAsString(blob) |
| 55 .then(test.step_func(function(value) { |
| 56 assert_equals(value, expected, message); |
| 57 test.done(); |
| 58 })) |
| 59 .catch(test.unreached_func('Failed to read Blob data')); |
| 60 } |
| 61 |
| 62 // Tests |
| 63 |
| 64 async_test(function(t) { |
| 65 var fd = new FormData(); |
| 66 fd.append('n1', 'value'); |
| 67 assert_equals(fd.has('n1'), true, |
| 68 'FormData.has() should see appended entry'); |
| 69 assert_equals(fd.has('n2'), false, |
| 70 'FormData.has() should not see absent entry'); |
| 71 fd.append('n2', 'value'); |
| 72 assert_equals(fd.has('n1'), true, |
| 73 'FormData.has() should still see original entry'); |
| 74 assert_equals(fd.has('n2'), true, |
| 75 'FormData.has() should see newly appended entry'); |
| 76 fd.append('n3', new Blob(['content'])); |
| 77 assert_equals(fd.has('n3'), true, |
| 78 'FormData.has() should see newly appended Blob entry'); |
| 79 |
| 80 checkFormData(t, fd, |
| 81 'n1=value&n2=value&n3=blob:application/octet-stream:content'); |
| 82 }, 'FormData.has()'); |
| 83 |
| 84 async_test(function(t) { |
| 85 var fd = new FormData(); |
| 86 fd.append('name', 'value'); |
| 87 assert_equals(fd.has('name'), true, |
| 88 'FormData.has() should see appended entry'); |
| 89 fd.delete('name'); |
| 90 assert_equals(fd.has('name'), false, |
| 91 'FormData.has() should not see deleted entry'); |
| 92 |
| 93 fd.append('name', new Blob(['content'])); |
| 94 assert_equals(fd.has('name'), true, |
| 95 'FormData.has() should see newly appended Blob entry'); |
| 96 fd.delete('name'); |
| 97 assert_equals(fd.has('name'), false, |
| 98 'FormData.has() should not see deleted Blob entry'); |
| 99 |
| 100 fd.append('n1', 'v1'); |
| 101 fd.append('n2', 'v2'); |
| 102 fd.append('n1', 'v3'); |
| 103 fd.delete('n1'); |
| 104 assert_equals(fd.has('n1'), false, |
| 105 'FormData.delete() should delete all matching entries'); |
| 106 |
| 107 checkFormData(t, fd, 'n2=v2'); |
| 108 }, 'FormData.delete()'); |
| 109 |
| 110 async_test(function(t) { |
| 111 var fd = new FormData(); |
| 112 fd.set('n1', 'v1'); |
| 113 assert_equals(fd.has('n1'), true, 'FormData.has() should see set() entry'); |
| 114 checkFormData(t, fd, 'n1=v1'); |
| 115 }, 'FormData.set() - DOMString'); |
| 116 |
| 117 async_test(function(t) { |
| 118 var fd = new FormData(); |
| 119 fd.set('n1', new Blob(['content'])); |
| 120 |
| 121 checkFormData(t, fd, 'n1=blob:application/octet-stream:content'); |
| 122 }, 'FormData.set() - Blob'); |
| 123 |
| 124 async_test(function(t) { |
| 125 var fd = new FormData(); |
| 126 fd.set('n1', new Blob(['content']), 'name1'); |
| 127 |
| 128 checkFormData(t, fd, 'n1=name1:application/octet-stream:content'); |
| 129 }, 'FormData.set() - Blob and filename'); |
| 130 |
| 131 async_test(function(t) { |
| 132 var fd = new FormData(); |
| 133 fd.append('n1', 'v1'); |
| 134 fd.append('n1', 'v2'); |
| 135 fd.set('n1', 'v3'); |
| 136 assert_equals(fd.has('n1'), true, 'FormData.has() should see set() entry'); |
| 137 fd.set('n2', 'v4'); |
| 138 fd.set('n2', new Blob(['content']), 'name1'); |
| 139 |
| 140 checkFormData(t, fd, 'n1=v3&n2=name1:application/octet-stream:content', |
| 141 'FormData.set() should replace all matching entries'); |
| 142 }, 'FormData.set() replaces all entries'); |
| 143 |
| 144 test(function() { |
| 145 var fd = new FormData(); |
| 146 fd.append('n1', 'v1'); |
| 147 var result = fd.get('n1'); |
| 148 assert_equals(result, 'v1', |
| 149 'FormData.get() should return matching DOMString entry'); |
| 150 }, 'FormData.get() - DOMString'); |
| 151 |
| 152 async_test(function(t) { |
| 153 var fd = new FormData(); |
| 154 fd.append('n1', new Blob(['content'])); |
| 155 var result = fd.get('n1'); |
| 156 assert_true(result instanceof File, |
| 157 'FormData.get() on an appended Blob should return a File'); |
| 158 assert_equals(result.name, 'blob', 'Default name on a Blob is "blob"'); |
| 159 checkBlobData(t, result, 'content'); |
| 160 }, 'FormData.get() - Blob'); |
| 161 |
| 162 async_test(function(t) { |
| 163 var fd = new FormData(); |
| 164 fd.append('n1', new Blob(['content']), 'name1'); |
| 165 var result = fd.get('n1'); |
| 166 assert_true(result instanceof File, |
| 167 'FormData.get() on an appended Blob should return a File'); |
| 168 assert_equals(result.name, 'name1', |
| 169 'Returned File should have specified name'); |
| 170 checkBlobData(t, result, 'content'); |
| 171 }, 'FormData.get() - Blob and filename'); |
| 172 |
| 173 test(function() { |
| 174 var fd = new FormData(); |
| 175 fd.append('n1', 'v1'); |
| 176 fd.append('n1', 'v2'); |
| 177 var result = fd.get('n1'); |
| 178 assert_equals(result, 'v1', |
| 179 'FormData.get() should return first matching entry'); |
| 180 }, 'FormData.get() - multiple matches'); |
| 181 |
| 182 test(function() { |
| 183 var fd = new FormData(); |
| 184 fd.append('n1', 'v1'); |
| 185 fd.append('n2', 'v2'); |
| 186 var result = fd.get('n3'); |
| 187 assert_equals(result, null, |
| 188 'FormData.get() should return null if no matches'); |
| 189 }, 'FormData.get() - no matches'); |
| 190 |
| 191 test(function() { |
| 192 var fd = new FormData(); |
| 193 fd.append('n1', 'v1'); |
| 194 fd.append('n2', 'v2'); |
| 195 fd.append('n1', new Blob(['content'])); |
| 196 fd.append('n3', 'v3'); |
| 197 fd.append('n1', new Blob(['content2']), 'name2'); |
| 198 fd.append('n4', 'v4'); |
| 199 var results = fd.getAll('n1'); |
| 200 |
| 201 assert_equals(results.length, 3, |
| 202 'FormData.getAll() should return all matching entries'); |
| 203 |
| 204 assert_equals(results[0], 'v1', 'First entry should match'); |
| 205 |
| 206 assert_true(results[1] instanceof File, 'Second entry should be a File'); |
| 207 assert_equals(results[1].name, 'blob', |
| 208 'Second entry name should be the default'); |
| 209 |
| 210 assert_true(results[2] instanceof File, 'Third entry should be a File'); |
| 211 assert_equals(results[2].name, 'name2', |
| 212 'Third entry name should be as specified'); |
| 213 }, 'FormData.getAll()'); |
| 214 |
| 215 test(function() { |
| 216 var fd = new FormData(); |
| 217 fd.append('n1', 'v1'); |
| 218 fd.append('n2', 'v2'); |
| 219 var results = fd.getAll('n3'); |
| 220 |
| 221 assert_equals(results.length, 0, |
| 222 'FormData.getAll() should return empty sequence if no ' + |
| 223 'matches'); |
| 224 }, 'FormData.getAll() - no matches'); |
| 225 |
| 226 test(function() { |
| 227 var fd = new FormData(); |
| 228 fd.set('f1', new File(['content'], 'name1')); |
| 229 fd.append('f2', new File(['content'], 'name2')); |
| 230 fd.set('f3', new File(['content'], 'name3'), 'override3'); |
| 231 fd.append('f4', new File(['content'], 'name4'), 'override4'); |
| 232 |
| 233 assert_equals(fd.get('f1').name, 'name1', |
| 234 'Original name of set file should be used'); |
| 235 assert_equals(fd.get('f2').name, 'name2', |
| 236 'Original name of appended file should be used'); |
| 237 assert_equals(fd.get('f3').name, 'override3', |
| 238 'Overridden name of set file should be used'); |
| 239 assert_equals(fd.get('f4').name, 'override4', |
| 240 'Overridden name of appended file should be used'); |
| 241 }, 'FormData.get() - Files with name overrides'); |
| 242 |
| 243 test(function() { |
| 244 var fd = new FormData(); |
| 245 fd.set('f1', new File(['content'], 'name1')); |
| 246 fd.append('f1', new File(['content'], 'name2')); |
| 247 fd.set('f2', new File(['content'], 'name3'), 'override3'); |
| 248 fd.append('f2', new File(['content'], 'name4'), 'override4'); |
| 249 |
| 250 var results1 = fd.getAll('f1'); |
| 251 var results2 = fd.getAll('f2'); |
| 252 assert_equals(results1[0].name, 'name1', |
| 253 'Original name of set file should be used'); |
| 254 assert_equals(results1[1].name, 'name2', |
| 255 'Original name of appended file should be used'); |
| 256 assert_equals(results2[0].name, 'override3', |
| 257 'Overridden name of set file should be used'); |
| 258 assert_equals(results2[1].name, 'override4', |
| 259 'Overridden name of appended file should be used'); |
| 260 }, 'FormData.getAll() - Files with name overrides'); |
| 261 |
| 262 test(function() { |
| 263 var file = new File(['content'], 'name'); |
| 264 var blob = new Blob(['content']); |
| 265 |
| 266 var now = Date.now(); |
| 267 var fd = new FormData(); |
| 268 fd.set('file1', file); |
| 269 fd.set('file2', file, 'override'); |
| 270 fd.set('blob1', blob); |
| 271 fd.set('blob2', blob, 'override'); |
| 272 |
| 273 assert_equals(Number(fd.get('file1').lastModifiedDate), |
| 274 Number(file.lastModifiedDate), |
| 275 'timestamp of file should be preserved'); |
| 276 |
| 277 assert_equals(Number(fd.get('file2').lastModifiedDate), |
| 278 Number(file.lastModifiedDate), |
| 279 'timestamp of file with name override should be preserved'); |
| 280 |
| 281 // NTP resets, different clocks (between DOM and JS), and just |
| 282 // slow machines (due to test loads) can make timestamp samples |
| 283 // vary between samples, so allow for some imprecision. We're |
| 284 // really just ensuring it's "nowish"; not the Epoch and not |
| 285 // initialized with the wrong units (s vs. ms). |
| 286 |
| 287 var epsilon_ms = 60 * 1000; // 60s - close enough. |
| 288 |
| 289 assert_approx_equals(Number(fd.get('blob1').lastModifiedDate), |
| 290 now, |
| 291 epsilon_ms, |
| 292 'timestamp of blob should be nowish'); |
| 293 assert_approx_equals(Number(fd.get('blob2').lastModifiedDate), |
| 294 now, |
| 295 epsilon_ms, |
| 296 'timestamp of blob should be nowish'); |
| 297 }, 'FormData - lastModifiedDate of File/Blob entries'); |
| 298 |
| 299 async_test(function(t) { |
| 300 var sample = |
| 301 '\xC0' + // 'LATIN CAPITAL LETTER A WITH GRAVE' (U+00C0) |
| 302 '\xC1' + // 'LATIN CAPITAL LETTER A WITH ACUTE' (U+00C1) |
| 303 '\xE0' + // 'LATIN SMALL LETTER A WITH GRAVE' (U+00E0) |
| 304 '\xE1' + // 'LATIN SMALL LETTER A WITH ACUTE' (U+00E1) |
| 305 '\xFF' + // 'LATIN SMALL LETTER Y WITH DIAERESIS' (U+00FF) |
| 306 '\u0100' + // 'LATIN CAPITAL LETTER A WITH MACRON' (U+0100) |
| 307 '\u1000' + // 'MYANMAR LETTER KA' (U+1000) |
| 308 '\uD834\uDD1E' + // 'MUSICAL SYMBOL G-CLEF' (U+1D11E) - UTF-16 surrogate
pairs |
| 309 '\uFFFD'; // 'REPLACEMENT CHARACTER' (U+FFFD) |
| 310 |
| 311 var fd = new FormData(); |
| 312 fd.append('k1' + sample, 'v1' + sample); |
| 313 fd.set('k2' + sample, 'v2' + sample); |
| 314 assert_equals(fd.get('k1' + sample), 'v1' + sample, |
| 315 'Set entry should encode/decode correctly'); |
| 316 assert_equals(fd.get('k2' + sample), 'v2' + sample, |
| 317 'Appended entry should encode/decode correctly'); |
| 318 checkFormData(t, fd, |
| 319 'k1' + sample + '=v1' + sample + '&' + |
| 320 'k2' + sample + '=v2' + sample); |
| 321 }, 'FormData - encoding round-trip'); |
| 322 |
| 323 test(function() { |
| 324 var fd = new FormData(); |
| 325 fd.append('k1\uD800', 'v1\uDC00'); |
| 326 assert_equals(fd.get('k1\uFFFD'), 'v1\uFFFD', |
| 327 'Invalid UTF-16 in append() should yield U+FFFD'); |
| 328 fd.set('k2\uD800', 'v2\uDC00'); |
| 329 assert_true(fd.has('k2\uFFFD'), |
| 330 'Invalid UTF-16 in set() should yield U+FFFD'); |
| 331 assert_true(fd.has('k2\uD800'), |
| 332 'Invalid UTF-16 in has() should yield U+FFFD'); |
| 333 assert_equals(fd.get('k2\uD800'), 'v2\uFFFD', |
| 334 'Invalid UTF-16 in get() should yield U+FFFD'); |
| 335 fd.delete('k2\uD800'); |
| 336 assert_false(fd.has('k2\uD800'), |
| 337 'Invalid UTF-16 in delete() should yield U+FFFD'); |
| 338 }, 'FormData - ScalarValueStrings'); |
| 339 |
| 340 test(function() { |
| 341 var fd = new FormData(); |
| 342 fd.append('n1', 'v1'); |
| 343 fd.append('n2', 'v2'); |
| 344 fd.append('n3', 'v3'); |
| 345 fd.append('n1', 'v4'); |
| 346 fd.append('n2', 'v5'); |
| 347 fd.append('n3', 'v6'); |
| 348 fd.delete('n2'); |
| 349 |
| 350 var expected_keys = ['n1', 'n3', 'n1', 'n3']; |
| 351 var expected_values = ['v1', 'v3', 'v4', 'v6']; |
| 352 |
| 353 (function() { |
| 354 var keys = [], values = []; |
| 355 for(var entry of fd) { |
| 356 assert_equals(entry.length, 2, |
| 357 'Default iterator should yield key/value pairs'); |
| 358 keys.push(entry[0]); |
| 359 values.push(entry[1]); |
| 360 } |
| 361 assert_array_equals(keys, expected_keys, |
| 362 'Default iterator should see duplicate keys'); |
| 363 assert_array_equals(values, expected_values, |
| 364 'Default iterator should see non-deleted values'); |
| 365 }()); |
| 366 |
| 367 (function() { |
| 368 var keys = [], values = []; |
| 369 for(var entry of fd.entries()) { |
| 370 assert_equals(entry.length, 2, |
| 371 'entries() iterator should yield key/value pairs'); |
| 372 keys.push(entry[0]); |
| 373 values.push(entry[1]); |
| 374 } |
| 375 assert_array_equals(keys, expected_keys, |
| 376 'entries() iterator should see duplicate keys'); |
| 377 assert_array_equals(values, expected_values, |
| 378 'entries() iterator should see non-deleted values'); |
| 379 }()); |
| 380 |
| 381 (function() { |
| 382 var keys = []; |
| 383 for(var entry of fd.keys()) |
| 384 keys.push(entry); |
| 385 assert_array_equals(keys, expected_keys, |
| 386 'keys() iterator should see duplicate keys'); |
| 387 }()); |
| 388 |
| 389 (function() { |
| 390 var values = []; |
| 391 for(var entry of fd.values()) |
| 392 values.push(entry); |
| 393 assert_array_equals(values, expected_values, |
| 394 'values() iterator should see non-deleted values'); |
| 395 }()); |
| 396 |
| 397 }, 'FormData - iterators'); |
| 398 |
| 399 </script> |
OLD | NEW |