Chromium Code Reviews| 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 function serializeFormData(formData) | |
| 8 { | |
| 9 return new Promise(function(resolve, reject) { | |
| 10 var xhr = new XMLHttpRequest(), async = true; | |
| 11 xhr.open('POST', 'http://127.0.0.1:8000/xmlhttprequest/resources/multipa rt-post-echo.php', async); | |
| 12 xhr.send(formData); | |
| 13 xhr.onreadystatechange = function() { | |
| 14 if (xhr.readyState !== XMLHttpRequest.DONE) | |
| 15 return; | |
| 16 if (xhr.status === 200) { | |
| 17 resolve(xhr.responseText); | |
| 18 } else { | |
| 19 reject(Error(xhr.statusText)); | |
| 20 } | |
| 21 }; | |
| 22 }); | |
| 23 } | |
| 24 | |
| 25 function checkFormData(test, formData, expected, message) | |
| 26 { | |
| 27 message = message || 'Serialized FormData should match'; | |
| 28 return serializeFormData(formData) | |
| 29 .then(test.step_func(function(value) { | |
| 30 assert_equals(value, expected, message); | |
| 31 test.done(); | |
| 32 })) | |
| 33 .catch(test.unreached_func('Failed to fetch form data')); | |
| 34 } | |
| 35 | |
| 36 function blobAsString(blob) | |
| 37 { | |
| 38 return new Promise(function(resolve, reject) { | |
| 39 var reader = new FileReader(); | |
| 40 reader.readAsText(blob); | |
| 41 reader.onload = function(e) { resolve(reader.result); }; | |
| 42 reader.onerror = function(e) { reject(reader.error); }; | |
| 43 }); | |
| 44 } | |
| 45 | |
| 46 function checkBlobData(test, blob, expected, message) | |
| 47 { | |
| 48 message = message || 'Blob daata should match'; | |
|
tyoshino (SeeGerritForStatus)
2014/09/12 07:52:54
daata -> data
jsbell
2014/09/12 16:30:09
Done.
| |
| 49 return blobAsString(blob) | |
| 50 .then(test.step_func(function(value) { | |
| 51 assert_equals(value, expected, message); | |
| 52 test.done(); | |
| 53 })) | |
| 54 .catch(test.unreached_func('Failed to read Blob data')); | |
| 55 } | |
| 56 | |
| 57 async_test(function(t) { | |
| 58 var fd = new FormData(); | |
| 59 fd.append('n1', 'value'); | |
| 60 assert_equals(fd.has('n1'), true, 'FormData.has() should see appended entry' ); | |
| 61 assert_equals(fd.has('n2'), false, 'FormData.has() should not see absent ent ry'); | |
| 62 fd.append('n2', 'value'); | |
| 63 assert_equals(fd.has('n1'), true, 'FormData.has() should still see original entry'); | |
| 64 assert_equals(fd.has('n2'), true, 'FormData.has() should see newly appended entry'); | |
| 65 fd.append('n3', new Blob(['content'])); | |
| 66 assert_equals(fd.has('n3'), true, 'FormData.has() should see newly appended Blob entry'); | |
| 67 | |
| 68 checkFormData(t, fd, 'n1=value&n2=value&n3=blob:application/octet-stream:con tent'); | |
| 69 }, 'FormData.has()'); | |
| 70 | |
| 71 async_test(function(t) { | |
| 72 var fd = new FormData(); | |
| 73 fd.append('name', 'value'); | |
| 74 assert_equals(fd.has('name'), true, 'FormData.has() should see appended entr y'); | |
| 75 fd.delete('name'); | |
| 76 assert_equals(fd.has('name'), false, 'FormData.has() should not see deleted entry'); | |
| 77 | |
| 78 fd.append('name', new Blob(['content'])); | |
| 79 assert_equals(fd.has('name'), true, 'FormData.has() should see newly appende d Blob entry'); | |
| 80 fd.delete('name'); | |
| 81 assert_equals(fd.has('name'), false, 'FormData.has() should not see deleted Blob entry'); | |
| 82 | |
| 83 fd.append('n1', 'v1'); | |
| 84 fd.append('n2', 'v2'); | |
| 85 fd.append('n1', 'v3'); | |
| 86 fd.delete('n1'); | |
| 87 assert_equals(fd.has('n1'), false, 'FormData.delete() should delete all matc hing entries'); | |
| 88 | |
| 89 checkFormData(t, fd, 'n2=v2'); | |
| 90 }, 'FormData.delete()'); | |
| 91 | |
| 92 async_test(function(t) { | |
| 93 var fd = new FormData(); | |
| 94 fd.set('n1', 'v1'); | |
| 95 assert_equals(fd.has('n1'), true, 'FormData.has() should see set() entry'); | |
| 96 checkFormData(t, fd, 'n1=v1'); | |
| 97 }, 'FormData.set() - DOMString'); | |
| 98 | |
| 99 async_test(function(t) { | |
| 100 var fd = new FormData(); | |
| 101 fd.set('n1', new Blob(['content'])); | |
| 102 | |
| 103 checkFormData(t, fd, 'n1=blob:application/octet-stream:content'); | |
| 104 }, 'FormData.set() - Blob'); | |
| 105 | |
| 106 async_test(function(t) { | |
| 107 var fd = new FormData(); | |
| 108 fd.set('n1', new Blob(['content']), 'name1'); | |
| 109 | |
| 110 checkFormData(t, fd, 'n1=name1:application/octet-stream:content'); | |
| 111 }, 'FormData.set() - Blob and filename'); | |
| 112 | |
| 113 async_test(function(t) { | |
| 114 var fd = new FormData(); | |
| 115 fd.append('n1', 'v1'); | |
| 116 fd.append('n1', 'v2'); | |
| 117 fd.set('n1', 'v3'); | |
| 118 assert_equals(fd.has('n1'), true, 'FormData.has() should see set() entry'); | |
| 119 fd.set('n2', 'v4'); | |
| 120 fd.set('n2', new Blob(['content']), 'name1'); | |
| 121 | |
| 122 checkFormData(t, fd, 'n1=v3&n2=name1:application/octet-stream:content', | |
| 123 'FormData.set() should replace all matching entries'); | |
| 124 }, 'FormData.set() replaces all entries'); | |
| 125 | |
| 126 test(function() { | |
| 127 var fd = new FormData(); | |
| 128 fd.append('n1', 'v1'); | |
| 129 var result = fd.get('n1'); | |
| 130 assert_equals(result, 'v1', 'FormData.get() should return matching DOMString entry'); | |
| 131 }, 'FormData.get() - DOMString'); | |
| 132 | |
| 133 async_test(function(t) { | |
| 134 var fd = new FormData(); | |
| 135 fd.append('n1', new Blob(['content'])); | |
| 136 var result = fd.get('n1'); | |
| 137 assert_true(result instanceof File, 'FormData.get() on an appended Blob shou ld return a File'); | |
| 138 assert_equals(result.name, 'blob', 'Default name on a Blob is "blob"'); | |
| 139 checkBlobData(t, result, 'content'); | |
| 140 }, 'FormData.get() - Blob'); | |
| 141 | |
| 142 async_test(function(t) { | |
| 143 var fd = new FormData(); | |
| 144 fd.append('n1', new Blob(['content']), 'name1'); | |
| 145 var result = fd.get('n1'); | |
| 146 assert_true(result instanceof File, 'FormData.get() on an appended Blob shou ld return a File'); | |
| 147 assert_equals(result.name, 'name1', 'Returned File should have specified nam e'); | |
| 148 checkBlobData(t, result, 'content'); | |
| 149 }, 'FormData.get() - Blob and filename'); | |
| 150 | |
| 151 test(function() { | |
| 152 var fd = new FormData(); | |
| 153 fd.append('n1', 'v1'); | |
| 154 fd.append('n1', 'v2'); | |
| 155 var result = fd.get('n1'); | |
| 156 assert_equals(result, 'v1', 'FormData.get() should return first matching ent ry'); | |
| 157 }, 'FormData.get() - multiple matches'); | |
| 158 | |
| 159 test(function() { | |
| 160 var fd = new FormData(); | |
| 161 fd.append('n1', 'v1'); | |
| 162 fd.append('n2', 'v2'); | |
| 163 var result = fd.get('n3'); | |
| 164 assert_equals(result, null, 'FormData.get() should return null if no matches '); | |
| 165 }, 'FormData.get() - no matches'); | |
| 166 | |
| 167 test(function() { | |
| 168 var fd = new FormData(); | |
| 169 fd.append('n1', 'v1'); | |
| 170 fd.append('n2', 'v2'); | |
| 171 fd.append('n1', new Blob(['content'])); | |
| 172 fd.append('n3', 'v3'); | |
| 173 fd.append('n1', new Blob(['content2']), 'name2'); | |
| 174 fd.append('n4', 'v4'); | |
| 175 var results = fd.getAll('n1'); | |
| 176 | |
| 177 assert_equals(results.length, 3, 'FormData.getAll() should return all matchi ng entries'); | |
| 178 | |
| 179 assert_equals(results[0], 'v1', 'First entry should match'); | |
| 180 | |
| 181 assert_true(results[1] instanceof File, 'Second entry should be a File'); | |
| 182 assert_equals(results[1].name, 'blob', 'Second entry name should be the defa ult'); | |
| 183 | |
| 184 assert_true(results[2] instanceof File, 'Third entry should be a File'); | |
| 185 assert_equals(results[2].name, 'name2', 'Third entry name should be as speci fied'); | |
| 186 }, 'FormData.getAll()'); | |
| 187 | |
| 188 test(function() { | |
| 189 var fd = new FormData(); | |
| 190 fd.append('n1', 'v1'); | |
| 191 fd.append('n2', 'v2'); | |
| 192 var results = fd.getAll('n3'); | |
| 193 | |
| 194 assert_equals(results.length, 0, 'FormData.getAll() should return empty sequ ence if no matches'); | |
| 195 }, 'FormData.getAll() - no matches'); | |
| 196 | |
| 197 // FIXME: Add test for get() for appended File without filename | |
| 198 // FIXME: Add test for get() for appended File with filename | |
| 199 // FIXME: Add test for getAll() for appended File without filename | |
| 200 // FIXME: Add test for getAll() for appended File with filename | |
| 201 | |
| 202 </script> | |
| OLD | NEW |