| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 | |
| 3 <script src="/js-test-resources/js-test.js"></script> | 2 <script src="/js-test-resources/js-test.js"></script> |
| 4 <script> | 3 <script src="resources/post-formdata.js"></script> |
| 5 description("Test verifies that FormData is sent correctly when using " + | |
| 6 "<a href='http://www.w3.org/TR/XMLHttpRequest/#the-send-method'>XMLH
ttpRequest asynchronously.</a>"); | |
| 7 | |
| 8 var xhrFormDataTestUrl = '/xmlhttprequest/resources/multipart-post-echo.php'; | |
| 9 var xhrFormDataTestCases = [{ | |
| 10 data: { string: 'string value' }, | |
| 11 result: "string=string value" | |
| 12 }, { | |
| 13 data: { bareBlob: new Blob(['blob-value']) }, | |
| 14 result: 'bareBlob=blob:application/octet-stream:blob-value' | |
| 15 }, { | |
| 16 data: { bareBlob: new Blob(['blob-value']) }, | |
| 17 beforeConstruct: function (t) { t.data.bareBlob.close(); }, | |
| 18 result: 'bareBlob=blob:application/octet-stream:' | |
| 19 }, { | |
| 20 data: { bareBlob: new Blob(['blob-value']) }, | |
| 21 beforeSend: function (t) { t.data.bareBlob.close(); }, | |
| 22 result: 'bareBlob=blob:application/octet-stream:' | |
| 23 }, { | |
| 24 data: { mimeBlob: new Blob(['blob-value'], { type: 'text/html' }) }, | |
| 25 result: 'mimeBlob=blob:text/html:blob-value' | |
| 26 }, { | |
| 27 data: { | |
| 28 namedBlob: { | |
| 29 value: new Blob(['blob-value']), | |
| 30 fileName: 'blob-file.txt' | |
| 31 } | |
| 32 }, | |
| 33 result: 'namedBlob=blob-file.txt:application/octet-stream:blob-value' | |
| 34 }, { | |
| 35 data: { bareFile: new File(['file-value'], 'file-name.txt') }, | |
| 36 result: 'bareFile=file-name.txt:application/octet-stream:file-value' | |
| 37 }, { | |
| 38 data: { bareFile: new File(['file-value'], 'file-name.txt') }, | |
| 39 beforeConstruct: function (t) { t.data.bareFile.close(); }, | |
| 40 result: 'bareFile=file-name.txt:application/octet-stream:' | |
| 41 }, { | |
| 42 data: { bareFile: new File(['file-value'], 'file-name.txt') }, | |
| 43 beforeSend: function (t) { t.data.bareFile.close(); }, | |
| 44 result: 'bareFile=file-name.txt:application/octet-stream:' | |
| 45 }, { | |
| 46 data: { | |
| 47 mimeFile: new File(['file-value'], 'file-name.html', { type: 'text/html'
}) | |
| 48 }, | |
| 49 result: 'mimeFile=file-name.html:text/html:file-value' | |
| 50 }, { | |
| 51 data: { | |
| 52 renamedFile: { | |
| 53 value: new File(['file-value'], 'file-name.html', { type: 'text/html
' }), | |
| 54 fileName: 'file-name-override.html' | |
| 55 } | |
| 56 }, | |
| 57 result: 'renamedFile=file-name-override.html:text/html:file-value' | |
| 58 }]; | |
| 59 | |
| 60 var xhr; | |
| 61 var expectedMimeType; | |
| 62 window.jsTestIsAsync = true; | |
| 63 var asyncTestCase = 0; | |
| 64 | |
| 65 function runNextAsyncTest() { | |
| 66 asyncTestCase++; | |
| 67 runAsyncTests(); | |
| 68 } | |
| 69 | |
| 70 function reportResult(e) { | |
| 71 var testCase = xhrFormDataTestCases[asyncTestCase]; | |
| 72 if (xhr.status === 200) { | |
| 73 echoResult = xhr.response; | |
| 74 shouldBeEqualToString("echoResult", testCase.result); | |
| 75 } else { | |
| 76 testFailed("Unknown error"); | |
| 77 } | |
| 78 | |
| 79 runNextAsyncTest(); | |
| 80 } | |
| 81 | |
| 82 function runAsyncTests() { | |
| 83 if (asyncTestCase >= xhrFormDataTestCases.length) { | |
| 84 finishJSTest(); | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 var testCase = xhrFormDataTestCases[asyncTestCase]; | |
| 89 var formData = new FormData(); | |
| 90 if (testCase.beforeConstruct) | |
| 91 testCase.beforeConstruct(testCase); | |
| 92 for (var fieldName in testCase.data) { | |
| 93 fieldValue = testCase.data[fieldName]; | |
| 94 if (fieldValue.constructor === Object) | |
| 95 formData.append(fieldName, fieldValue.value, fieldValue.fileName); | |
| 96 else | |
| 97 formData.append(fieldName, fieldValue); | |
| 98 } | |
| 99 | |
| 100 xhr = new XMLHttpRequest(); | |
| 101 xhr.onloadend = reportResult; | |
| 102 xhr.open("POST", xhrFormDataTestUrl, true); | |
| 103 if (testCase.beforeSend) | |
| 104 testCase.beforeSend(testCase); | |
| 105 xhr.send(formData); | |
| 106 } | |
| 107 | |
| 108 runAsyncTests(); | |
| 109 </script> | |
| OLD | NEW |