Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / serviceworker / resources / fetch-request-xhr-iframe.html
1 <script src="../../resources/testharness.js"></script>
2 <script src="test-helpers.js?pipe=sub"></script>
3 <script>
4 function get_boundary(headers) {
5   var reg = new RegExp('multipart\/form-data; boundary=(.*)');
6   for (var i = 0; i < headers.length; ++i) {
7     if (headers[i][0] != 'content-type') {
8       continue;
9     }
10     var regResult = reg.exec(headers[i][1]);
11     if (!regResult) {
12       continue;
13     }
14     return regResult[1];
15   }
16   return '';
17 }
18
19 function create_file_system_file(file_name, data) {
20   return new Promise(function(resolve, reject) {
21       webkitRequestFileSystem(TEMPORARY, 1024, function(fs) {
22           fs.root.getFile(
23             file_name, {create: true, exclusive: true},
24             function(fileEntry) {
25               fileEntry.createWriter(function(fileWriter) {
26                   fileWriter.onwriteend = function(e) {
27                     fileEntry.file(function(file) { resolve(file); });
28                   };
29                   var blob = new Blob([data], {type: 'text/plain'});
30                   fileWriter.write(blob);
31                 });
32             }, function(e) { reject(e); });
33         }, function(e) { reject(e); });
34     });
35 }
36
37 function xhr_send(method, data) {
38   return new Promise(function(resolve, reject) {
39       var xhr = new XMLHttpRequest();
40       xhr.onload = function() {
41         resolve(JSON.parse(xhr.response));
42       };
43       xhr.onerror = function() {
44         reject('XHR should succeed.');
45       };
46       xhr.responseType = 'text';
47       xhr.open(method, './dummy?test', true);
48       xhr.send(data);
49     });
50 }
51
52 function string_test() {
53   return xhr_send('POST', 'test string')
54     .then(function(response) {
55         assert_equals(response.method, 'POST');
56         assert_equals(response.body, 'test string');
57       });
58 }
59
60 function blob_test() {
61   return xhr_send('POST', new Blob(['test blob']))
62     .then(function(response) {
63         assert_equals(response.method, 'POST');
64         assert_equals(response.body, 'test blob');
65       });
66 }
67
68 function custom_method_test() {
69   return xhr_send('XXX', 'test string xxx')
70     .then(function(response){
71         assert_equals(response.method, 'XXX');
72         assert_equals(response.body, 'test string xxx');
73       });
74 }
75
76 function form_data_test() {
77   return create_file_system_file('fsfile.txt', 'fs file content')
78     .then(function(file_system_file) {
79         var formData = new FormData();
80         formData.append('sample string', '1234567890');
81         formData.append('sample blob', new Blob(['blob content']));
82         formData.append('sample file', new File(['file content'], 'file.dat'));
83         formData.append('sample fs file', file_system_file);
84         return xhr_send('POST', formData);
85       })
86     .then(function(response) {
87         assert_equals(response.method, 'POST');
88         var boundary = get_boundary(response.headers);
89         var expected_body =
90           '--' + boundary + '\r\n' +
91           'Content-Disposition: form-data; name="sample string"\r\n' +
92           '\r\n' +
93           '1234567890\r\n' +
94           '--' + boundary + '\r\n' +
95           'Content-Disposition: form-data; name="sample blob"; ' +
96           'filename="blob"\r\n' +
97           'Content-Type: application/octet-stream\r\n' +
98           '\r\n' +
99           'blob content\r\n' +
100           '--' + boundary + '\r\n' +
101           'Content-Disposition: form-data; name="sample file"; ' +
102           'filename="file.dat"\r\n' +
103           'Content-Type: application/octet-stream\r\n' +
104           '\r\n' +
105           'file content\r\n' +
106           '--' + boundary + '\r\n' +
107           'Content-Disposition: form-data; name="sample fs file"; ' +
108           'filename="fsfile.txt"\r\n' +
109           'Content-Type: text/plain\r\n' +
110           '\r\n' +
111           'fs file content\r\n' +
112           '--' + boundary + '--\r\n';
113         assert_equals(response.body, expected_body);
114       });
115 }
116
117 window.addEventListener('message', function(evt) {
118     var port = evt.ports[0];
119     string_test()
120       .then(blob_test)
121       .then(custom_method_test)
122       .then(form_data_test)
123       .then(function() { port.postMessage({results: 'finish'}); })
124       .catch(function(e) { port.postMessage({results: 'failure:' + e}); });
125   });
126 </script>