test_formSubmission.html (6832B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test for Directory form submission</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 7 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> 8 </head> 9 <body onload="return next();"> 10 11 <iframe name="target_iframe" id="target_iframe"></iframe> 12 13 <form action="../../../html/test/form_submit_server.sjs" target="target_iframe" id="form" 14 method="POST" enctype="multipart/form-data"> 15 </form> 16 17 <script class="testbody" type="text/javascript"> 18 var form; 19 var iframe; 20 var input; 21 var script; 22 var xhr; 23 24 function setup_tests() { 25 form = document.getElementById("form"); 26 27 iframe = document.getElementById("target_iframe"); 28 iframe.onload = function() { 29 info("Frame loaded!"); 30 next(); 31 }; 32 33 SpecialPowers.pushPrefEnv({"set": [["dom.webkitBlink.dirPicker.enabled", true], 34 ["dom.filesystem.pathcheck.disabled", true], 35 ["dom.webkitBlink.filesystem.enabled", true]]}, next); 36 } 37 38 function populate_entries(webkitDirectory) { 39 var url = SimpleTest.getTestFileURL("script_entries.js"); 40 script = SpecialPowers.loadChromeScript(url); 41 42 if (input) { 43 form.removeChild(input); 44 } 45 46 input = document.createElement("input"); 47 input.setAttribute("id", "input"); 48 input.setAttribute("type", "file"); 49 input.setAttribute("name", "input"); 50 51 if (webkitDirectory) { 52 input.setAttribute("webkitdirectory", "true"); 53 } 54 55 form.appendChild(input); 56 57 function onOpened(message) { 58 input.addEventListener("change", function() { 59 next(); 60 }, {once: true}); 61 62 SpecialPowers.wrap(input).mozSetDndFilesAndDirectories([message.data[0]]); 63 } 64 65 script.addMessageListener("entries.opened", onOpened); 66 script.sendAsyncMessage("entries.open"); 67 } 68 69 function delete_entries() { 70 script.sendAsyncMessage("entries.delete"); 71 script.addMessageListener("entries.deleted", function() { 72 script.destroy(); 73 next(); 74 }); 75 } 76 77 function setup_plain() { 78 info("Preparing for a plain text submission..."); 79 form.action = "../../../html/test/form_submit_server.sjs?plain"; 80 form.method = "POST"; 81 form.enctype = "text/plain"; 82 form.submit(); 83 } 84 85 function test_plain() { 86 var content = iframe.contentDocument.documentElement.textContent; 87 var submission = JSON.parse(content); 88 info(submission); 89 is(submission, input.webkitEntries.map(function(v) { 90 return "input=" + v.name + "\r\n"; 91 }).join(""), "Data match"); 92 93 next(); 94 } 95 96 function setup_urlencoded() { 97 info("Preparing for a urlencoded submission..."); 98 form.action = "../../../html/test/form_submit_server.sjs?url"; 99 form.method = "POST"; 100 form.enctype = "application/x-www-form-urlencoded"; 101 form.submit(); 102 } 103 104 function setup_urlencoded_get() { 105 info("Preparing for a urlencoded+GET submission..."); 106 form.action = "../../../html/test/form_submit_server.sjs?xxyy"; 107 form.method = "GET"; 108 form.enctype = ""; 109 form.submit(); 110 } 111 112 function setup_urlencoded_empty() { 113 info("Preparing for a urlencoded+default values submission..."); 114 form.action = "../../../html/test/form_submit_server.sjs"; 115 form.method = ""; 116 form.enctype = ""; 117 form.submit(); 118 } 119 120 function test_urlencoded() { 121 var content = iframe.contentDocument.documentElement.textContent; 122 var submission = JSON.parse(content); 123 info(submission); 124 is(submission, input.webkitEntries.map(function(v) { 125 return "input=" + v.name; 126 }).join("&"), "Data match"); 127 128 next(); 129 } 130 131 function setup_formData() { 132 info("Preparing for a fromData submission..."); 133 134 xhr = new XMLHttpRequest(); 135 xhr.onload = next; 136 xhr.open("POST", "../../../html/test/form_submit_server.sjs"); 137 xhr.send(new FormData(form)); 138 } 139 140 function test_multipart() { 141 var submission = JSON.parse(xhr.responseText); 142 143 var array = input.webkitEntries; 144 is(submission.length, array.length, "Same length"); 145 info(submission); 146 147 for (var i = 0; i < array.length; ++i) { 148 if (array[i].isDirectory) { 149 is(submission[i].headers["Content-Disposition"], 150 "form-data; name=\"input\"; filename=\"/" + array[i].name + "\"", 151 "Correct Content-Disposition"); 152 is(submission[i].headers["Content-Type"], "application/octet-stream", 153 "Correct Content-Type"); 154 is(submission[i].body, "", "Correct body"); 155 } else { 156 ok(array[i].isFile); 157 is(submission[i].headers["Content-Disposition"], 158 "form-data; name=\"input\"; filename=\"" + array[i].name + "\"", 159 "Correct Content-Disposition"); 160 is(submission[i].headers["Content-Type"], array[i].type, 161 "Correct Content-Type"); 162 is(submission[i].body, "", "Correct body"); 163 } 164 } 165 166 next(); 167 } 168 169 function getInputFiles(inputElement) { 170 var array = []; 171 for (var i = 0; i < inputElement.files.length; ++i) { 172 array.push(inputElement.files[i]); 173 } 174 return array; 175 } 176 177 function test_webkit_plain() { 178 var content = iframe.contentDocument.documentElement.textContent; 179 var submission = JSON.parse(content); 180 181 is(submission, getInputFiles(input).map(function(v) { 182 return "input=" + v.name + "\r\n"; 183 }).join(""), "Data match"); 184 185 next(); 186 } 187 188 function test_webkit_urlencoded() { 189 var content = iframe.contentDocument.documentElement.textContent; 190 var submission = JSON.parse(content); 191 is(submission, getInputFiles(input).map(function(v) { 192 return "input=" + v.name; 193 }).join("&"), "Data match"); 194 195 next(); 196 } 197 198 function test_webkit_multipart() { 199 var submission = JSON.parse(xhr.responseText); 200 var array = getInputFiles(input); 201 is(submission.length, array.length, "Same length"); 202 203 for (var i = 0; i < array.length; ++i) { 204 ok(array[i] instanceof File); 205 is(submission[i].headers["Content-Disposition"], 206 "form-data; name=\"input\"; filename=\"" + array[i].webkitRelativePath + "\"", 207 "Correct Content-Disposition"); 208 is(submission[i].headers["Content-Type"], array[i].type, 209 "Correct Content-Type"); 210 is(submission[i].body, "", "Correct body"); 211 } 212 next(); 213 } 214 215 var tests = [ 216 setup_tests, 217 218 function() { populate_entries(false); }, 219 220 setup_plain, 221 test_plain, 222 223 setup_urlencoded, 224 test_urlencoded, 225 226 setup_urlencoded_get, 227 test_urlencoded, 228 229 setup_urlencoded_empty, 230 test_urlencoded, 231 232 setup_formData, 233 test_multipart, 234 235 delete_entries, 236 237 function() { populate_entries(true); }, 238 239 setup_plain, 240 test_webkit_plain, 241 242 setup_urlencoded, 243 test_webkit_urlencoded, 244 245 setup_urlencoded_get, 246 test_webkit_urlencoded, 247 248 setup_urlencoded_empty, 249 test_webkit_urlencoded, 250 251 setup_formData, 252 test_webkit_multipart, 253 254 delete_entries, 255 ]; 256 257 function next() { 258 if (!tests.length) { 259 SimpleTest.finish(); 260 return; 261 } 262 263 var test = tests.shift(); 264 test(); 265 } 266 267 SimpleTest.waitForExplicitFinish(); 268 269 </script> 270 </body> 271 </html>