1 description("Test verifies that FormData is sent correctly when using " +
2 "<a href='http://www.w3.org/TR/XMLHttpRequest/#the-send-method'>XMLHttpRequest asynchronously.</a>");
4 var xhrFormDataTestUrl = '/xmlhttprequest/resources/multipart-post-echo.php';
5 var xhrFormDataTestCases = [{
6 data: { string: 'string value' },
7 result: "string=string value"
9 data: { bareBlob: new Blob(['blob-value']) },
10 result: 'bareBlob=blob:application/octet-stream:blob-value'
12 data: { bareBlob: new Blob(['blob-value']) },
13 beforeConstruct: function (t) { t.data.bareBlob.close(); },
14 result: 'bareBlob=blob:application/octet-stream:'
16 data: { bareBlob: new Blob(['blob-value']) },
17 beforeSend: function (t) { t.data.bareBlob.close(); },
18 result: 'bareBlob=blob:application/octet-stream:'
20 data: { mimeBlob: new Blob(['blob-value'], { type: 'text/html' }) },
21 result: 'mimeBlob=blob:text/html:blob-value'
25 value: new Blob(['blob-value']),
26 fileName: 'blob-file.txt'
29 result: 'namedBlob=blob-file.txt:application/octet-stream:blob-value'
31 data: { bareFile: new File(['file-value'], 'file-name.txt') },
32 result: 'bareFile=file-name.txt:application/octet-stream:file-value'
34 data: { bareFile: new File(['file-value'], 'file-name.txt') },
35 beforeConstruct: function (t) { t.data.bareFile.close(); },
36 result: 'bareFile=file-name.txt:application/octet-stream:'
38 data: { bareFile: new File(['file-value'], 'file-name.txt') },
39 beforeSend: function (t) { t.data.bareFile.close(); },
40 result: 'bareFile=file-name.txt:application/octet-stream:'
43 mimeFile: new File(['file-value'], 'file-name.html', { type: 'text/html' })
45 result: 'mimeFile=file-name.html:text/html:file-value'
49 value: new File(['file-value'], 'file-name.html', { type: 'text/html' }),
50 fileName: 'file-name-override.html'
53 result: 'renamedFile=file-name-override.html:text/html:file-value'
59 self.jsTestIsAsync = true;
60 var asyncTestCase = 0;
62 function runNextAsyncTest() {
67 function reportResult(e) {
68 var testCase = xhrFormDataTestCases[asyncTestCase];
69 if (xhr.status === 200) {
70 echoResult = xhr.response;
71 shouldBeEqualToString("echoResult", testCase.result);
73 testFailed("Unknown error");
79 function runAsyncTests() {
80 if (asyncTestCase >= xhrFormDataTestCases.length) {
85 var testCase = xhrFormDataTestCases[asyncTestCase];
86 var formData = new FormData();
87 if (testCase.beforeConstruct)
88 testCase.beforeConstruct(testCase);
89 for (var fieldName in testCase.data) {
90 fieldValue = testCase.data[fieldName];
91 if (fieldValue.constructor === Object)
92 formData.append(fieldName, fieldValue.value, fieldValue.fileName);
94 formData.append(fieldName, fieldValue);
97 xhr = new XMLHttpRequest();
98 xhr.onloadend = reportResult;
99 xhr.open("POST", xhrFormDataTestUrl, true);
100 if (testCase.beforeSend)
101 testCase.beforeSend(testCase);