| OLD | NEW |
| 1 <script src="../../resources/testharness.js"></script> | 1 <script src="../../resources/testharness.js"></script> |
| 2 <script src="test-helpers.js?pipe=sub"></script> | 2 <script src="test-helpers.js?pipe=sub"></script> |
| 3 <script> | 3 <script> |
| 4 var host_info = get_host_info(); |
| 5 |
| 4 function get_boundary(headers) { | 6 function get_boundary(headers) { |
| 5 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); | 7 var reg = new RegExp('multipart\/form-data; boundary=(.*)'); |
| 6 for (var i = 0; i < headers.length; ++i) { | 8 for (var i = 0; i < headers.length; ++i) { |
| 7 if (headers[i][0] != 'content-type') { | 9 if (headers[i][0] != 'content-type') { |
| 8 continue; | 10 continue; |
| 9 } | 11 } |
| 10 var regResult = reg.exec(headers[i][1]); | 12 var regResult = reg.exec(headers[i][1]); |
| 11 if (!regResult) { | 13 if (!regResult) { |
| 12 continue; | 14 continue; |
| 13 } | 15 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 27 fileEntry.file(function(file) { resolve(file); }); | 29 fileEntry.file(function(file) { resolve(file); }); |
| 28 }; | 30 }; |
| 29 var blob = new Blob([data], {type: 'text/plain'}); | 31 var blob = new Blob([data], {type: 'text/plain'}); |
| 30 fileWriter.write(blob); | 32 fileWriter.write(blob); |
| 31 }); | 33 }); |
| 32 }, function(e) { reject(e); }); | 34 }, function(e) { reject(e); }); |
| 33 }, function(e) { reject(e); }); | 35 }, function(e) { reject(e); }); |
| 34 }); | 36 }); |
| 35 } | 37 } |
| 36 | 38 |
| 37 function xhr_send(method, data) { | 39 function xhr_send(url_base, method, data, with_credentials) { |
| 38 return new Promise(function(resolve, reject) { | 40 return new Promise(function(resolve, reject) { |
| 39 var xhr = new XMLHttpRequest(); | 41 var xhr = new XMLHttpRequest(); |
| 40 xhr.onload = function() { | 42 xhr.onload = function() { |
| 41 resolve(JSON.parse(xhr.response)); | 43 resolve(JSON.parse(xhr.response)); |
| 42 }; | 44 }; |
| 43 xhr.onerror = function() { | 45 xhr.onerror = function() { |
| 44 reject('XHR should succeed.'); | 46 reject('XHR should succeed.'); |
| 45 }; | 47 }; |
| 46 xhr.responseType = 'text'; | 48 xhr.responseType = 'text'; |
| 47 xhr.open(method, './dummy?test', true); | 49 if (with_credentials) { |
| 50 xhr.withCredentials = true; |
| 51 } |
| 52 xhr.open(method, url_base + '/dummy?test', true); |
| 48 xhr.send(data); | 53 xhr.send(data); |
| 49 }); | 54 }); |
| 50 } | 55 } |
| 51 | 56 |
| 52 function string_test() { | 57 function string_test() { |
| 53 return xhr_send('POST', 'test string') | 58 return xhr_send(host_info['HTTP_ORIGIN'], 'POST', 'test string', false) |
| 54 .then(function(response) { | 59 .then(function(response) { |
| 55 assert_equals(response.method, 'POST'); | 60 assert_equals(response.method, 'POST'); |
| 56 assert_equals(response.body, 'test string'); | 61 assert_equals(response.body, 'test string'); |
| 57 }); | 62 }); |
| 58 } | 63 } |
| 59 | 64 |
| 60 function blob_test() { | 65 function blob_test() { |
| 61 return xhr_send('POST', new Blob(['test blob'])) | 66 return xhr_send(host_info['HTTP_ORIGIN'], 'POST', new Blob(['test blob']), |
| 67 false) |
| 62 .then(function(response) { | 68 .then(function(response) { |
| 63 assert_equals(response.method, 'POST'); | 69 assert_equals(response.method, 'POST'); |
| 64 assert_equals(response.body, 'test blob'); | 70 assert_equals(response.body, 'test blob'); |
| 65 }); | 71 }); |
| 66 } | 72 } |
| 67 | 73 |
| 68 function custom_method_test() { | 74 function custom_method_test() { |
| 69 return xhr_send('XXX', 'test string xxx') | 75 return xhr_send(host_info['HTTP_ORIGIN'], 'XXX', 'test string xxx', false) |
| 70 .then(function(response){ | 76 .then(function(response){ |
| 71 assert_equals(response.method, 'XXX'); | 77 assert_equals(response.method, 'XXX'); |
| 72 assert_equals(response.body, 'test string xxx'); | 78 assert_equals(response.body, 'test string xxx'); |
| 73 }); | 79 }); |
| 74 } | 80 } |
| 75 | 81 |
| 76 function form_data_test() { | 82 function form_data_test() { |
| 77 return create_file_system_file('fsfile.txt', 'fs file content') | 83 return create_file_system_file('fsfile.txt', 'fs file content') |
| 78 .then(function(file_system_file) { | 84 .then(function(file_system_file) { |
| 79 var formData = new FormData(); | 85 var formData = new FormData(); |
| 80 formData.append('sample string', '1234567890'); | 86 formData.append('sample string', '1234567890'); |
| 81 formData.append('sample blob', new Blob(['blob content'])); | 87 formData.append('sample blob', new Blob(['blob content'])); |
| 82 formData.append('sample file', new File(['file content'], 'file.dat')); | 88 formData.append('sample file', new File(['file content'], 'file.dat')); |
| 83 formData.append('sample fs file', file_system_file); | 89 formData.append('sample fs file', file_system_file); |
| 84 return xhr_send('POST', formData); | 90 return xhr_send(host_info['HTTP_ORIGIN'], 'POST', formData, false); |
| 85 }) | 91 }) |
| 86 .then(function(response) { | 92 .then(function(response) { |
| 87 assert_equals(response.method, 'POST'); | 93 assert_equals(response.method, 'POST'); |
| 88 var boundary = get_boundary(response.headers); | 94 var boundary = get_boundary(response.headers); |
| 89 var expected_body = | 95 var expected_body = |
| 90 '--' + boundary + '\r\n' + | 96 '--' + boundary + '\r\n' + |
| 91 'Content-Disposition: form-data; name="sample string"\r\n' + | 97 'Content-Disposition: form-data; name="sample string"\r\n' + |
| 92 '\r\n' + | 98 '\r\n' + |
| 93 '1234567890\r\n' + | 99 '1234567890\r\n' + |
| 94 '--' + boundary + '\r\n' + | 100 '--' + boundary + '\r\n' + |
| (...skipping 12 matching lines...) Expand all Loading... |
| 107 'Content-Disposition: form-data; name="sample fs file"; ' + | 113 'Content-Disposition: form-data; name="sample fs file"; ' + |
| 108 'filename="fsfile.txt"\r\n' + | 114 'filename="fsfile.txt"\r\n' + |
| 109 'Content-Type: text/plain\r\n' + | 115 'Content-Type: text/plain\r\n' + |
| 110 '\r\n' + | 116 '\r\n' + |
| 111 'fs file content\r\n' + | 117 'fs file content\r\n' + |
| 112 '--' + boundary + '--\r\n'; | 118 '--' + boundary + '--\r\n'; |
| 113 assert_equals(response.body, expected_body); | 119 assert_equals(response.body, expected_body); |
| 114 }); | 120 }); |
| 115 } | 121 } |
| 116 | 122 |
| 123 function mode_test() { |
| 124 return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', false) |
| 125 .then(function(response){ |
| 126 assert_equals(response.mode, 'cors'); |
| 127 return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', true); |
| 128 }) |
| 129 .then(function(response){ |
| 130 assert_equals(response.mode, 'cors'); |
| 131 return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', false); |
| 132 }) |
| 133 .then(function(response){ |
| 134 assert_equals(response.mode, 'cors'); |
| 135 return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', true); |
| 136 }) |
| 137 .then(function(response){ |
| 138 assert_equals(response.mode, 'cors'); |
| 139 }); |
| 140 } |
| 141 |
| 117 window.addEventListener('message', function(evt) { | 142 window.addEventListener('message', function(evt) { |
| 118 var port = evt.ports[0]; | 143 var port = evt.ports[0]; |
| 119 string_test() | 144 string_test() |
| 120 .then(blob_test) | 145 .then(blob_test) |
| 121 .then(custom_method_test) | 146 .then(custom_method_test) |
| 122 .then(form_data_test) | 147 .then(form_data_test) |
| 148 .then(mode_test) |
| 123 .then(function() { port.postMessage({results: 'finish'}); }) | 149 .then(function() { port.postMessage({results: 'finish'}); }) |
| 124 .catch(function(e) { port.postMessage({results: 'failure:' + e}); }); | 150 .catch(function(e) { port.postMessage({results: 'failure:' + e}); }); |
| 125 }); | 151 }); |
| 126 </script> | 152 </script> |
| OLD | NEW |