Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / xmlhttprequest / resources / post-formdata.js
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>");
3
4 var xhrFormDataTestUrl = '/xmlhttprequest/resources/multipart-post-echo.php';
5 var xhrFormDataTestCases = [{
6     data: { string: 'string value' },
7     result: "string=string value"
8 }, {
9     data: { bareBlob: new Blob(['blob-value']) },
10     result: 'bareBlob=blob:application/octet-stream:blob-value'
11 }, {
12     data: { bareBlob: new Blob(['blob-value']) },
13     beforeConstruct: function (t) { t.data.bareBlob.close(); },
14     result: 'bareBlob=blob:application/octet-stream:'
15 }, {
16     data: { bareBlob: new Blob(['blob-value']) },
17     beforeSend: function (t) { t.data.bareBlob.close(); },
18     result: 'bareBlob=blob:application/octet-stream:'
19 }, {
20     data: { mimeBlob: new Blob(['blob-value'], { type: 'text/html' }) },
21     result: 'mimeBlob=blob:text/html:blob-value'
22 }, {
23     data: {
24         namedBlob: {
25             value: new Blob(['blob-value']),
26             fileName: 'blob-file.txt'
27         }
28     },
29     result: 'namedBlob=blob-file.txt:application/octet-stream:blob-value'
30 }, {
31     data: { bareFile: new File(['file-value'], 'file-name.txt') },
32     result: 'bareFile=file-name.txt:application/octet-stream:file-value'
33 }, {
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:'
37 }, {
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:'
41 }, {
42     data: {
43         mimeFile: new File(['file-value'], 'file-name.html', { type: 'text/html' })
44     },
45     result: 'mimeFile=file-name.html:text/html:file-value'
46 }, {
47     data: {
48         renamedFile: {
49             value: new File(['file-value'], 'file-name.html', { type: 'text/html' }),
50             fileName: 'file-name-override.html'
51         }
52     },
53     result: 'renamedFile=file-name-override.html:text/html:file-value'
54 }];
55
56 var xhr;
57 var expectedMimeType;
58
59 self.jsTestIsAsync = true;
60 var asyncTestCase = 0;
61
62 function runNextAsyncTest() {
63     asyncTestCase++;
64     runAsyncTests();
65 }
66
67 function reportResult(e) {
68     var testCase = xhrFormDataTestCases[asyncTestCase];
69     if (xhr.status === 200) {
70         echoResult = xhr.response;
71         shouldBeEqualToString("echoResult", testCase.result);
72     } else {
73         testFailed("Unknown error");
74     }
75
76     runNextAsyncTest();
77 }
78
79 function runAsyncTests() {
80     if (asyncTestCase >= xhrFormDataTestCases.length) {
81         finishJSTest();
82         return;
83     }
84
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);
93         else
94             formData.append(fieldName, fieldValue);
95     }
96
97     xhr = new XMLHttpRequest();
98     xhr.onloadend = reportResult;
99     xhr.open("POST", xhrFormDataTestUrl, true);
100     if (testCase.beforeSend)
101         testCase.beforeSend(testCase);
102     xhr.send(formData);
103 }
104
105 runAsyncTests();