1 <script src="../../resources/testharness.js"></script>
2 <script src="test-helpers.js?pipe=sub"></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') {
10 var regResult = reg.exec(headers[i][1]);
19 function create_file_system_file(file_name, data) {
20 return new Promise(function(resolve, reject) {
21 webkitRequestFileSystem(TEMPORARY, 1024, function(fs) {
23 file_name, {create: true, exclusive: true},
25 fileEntry.createWriter(function(fileWriter) {
26 fileWriter.onwriteend = function(e) {
27 fileEntry.file(function(file) { resolve(file); });
29 var blob = new Blob([data], {type: 'text/plain'});
30 fileWriter.write(blob);
32 }, function(e) { reject(e); });
33 }, function(e) { reject(e); });
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));
43 xhr.onerror = function() {
44 reject('XHR should succeed.');
46 xhr.responseType = 'text';
47 xhr.open(method, './dummy?test', true);
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');
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');
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');
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);
86 .then(function(response) {
87 assert_equals(response.method, 'POST');
88 var boundary = get_boundary(response.headers);
90 '--' + boundary + '\r\n' +
91 'Content-Disposition: form-data; name="sample string"\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' +
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' +
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' +
111 'fs file content\r\n' +
112 '--' + boundary + '--\r\n';
113 assert_equals(response.body, expected_body);
117 window.addEventListener('message', function(evt) {
118 var port = evt.ports[0];
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}); });