1 <script src=
"../../resources/testharness.js"></script>
2 <script src=
"../../resources/get-host-info.js"></script>
3 <script src=
"test-helpers.js?pipe=sub"></script>
5 var host_info
= get_host_info();
7 function get_boundary(headers
) {
8 var reg
= new RegExp('multipart\/form-data; boundary=(.*)');
9 for (var i
= 0; i
< headers
.length
; ++i
) {
10 if (headers
[i
][0] != 'content-type') {
13 var regResult
= reg
.exec(headers
[i
][1]);
22 function create_file_system_file(file_name
, data
) {
23 return new Promise(function(resolve
, reject
) {
24 webkitRequestFileSystem(TEMPORARY
, 1024, function(fs
) {
26 file_name
, {create
: true, exclusive
: true},
28 fileEntry
.createWriter(function(fileWriter
) {
29 fileWriter
.onwriteend = function(e
) {
30 fileEntry
.file(function(file
) { resolve(file
); });
32 var blob
= new Blob([data
], {type
: 'text/plain'});
33 fileWriter
.write(blob
);
35 }, function(e
) { reject(e
); });
36 }, function(e
) { reject(e
); });
40 function xhr_send(url_base
, method
, data
, with_credentials
) {
41 return new Promise(function(resolve
, reject
) {
42 var xhr
= new XMLHttpRequest();
43 xhr
.onload = function() {
44 resolve(JSON
.parse(xhr
.response
));
46 xhr
.onerror = function() {
47 reject('XHR should succeed.');
49 xhr
.responseType
= 'text';
50 if (with_credentials
) {
51 xhr
.withCredentials
= true;
53 xhr
.open(method
, url_base
+ '/dummy?test', true);
58 function string_test() {
59 return xhr_send(host_info
['HTTP_ORIGIN'], 'POST', 'test string', false)
60 .then(function(response
) {
61 assert_equals(response
.method
, 'POST');
62 assert_equals(response
.body
, 'test string');
66 function blob_test() {
67 return xhr_send(host_info
['HTTP_ORIGIN'], 'POST', new Blob(['test blob']),
69 .then(function(response
) {
70 assert_equals(response
.method
, 'POST');
71 assert_equals(response
.body
, 'test blob');
75 function custom_method_test() {
76 return xhr_send(host_info
['HTTP_ORIGIN'], 'XXX', 'test string xxx', false)
77 .then(function(response
) {
78 assert_equals(response
.method
, 'XXX');
79 assert_equals(response
.body
, 'test string xxx');
83 function options_method_test() {
84 return xhr_send(host_info
['HTTP_ORIGIN'], 'OPTIONS', 'test string xxx', false)
85 .then(function(response
) {
86 assert_equals(response
.method
, 'OPTIONS');
87 assert_equals(response
.body
, 'test string xxx');
91 function form_data_test() {
92 return create_file_system_file('fsfile.txt', 'fs file content')
93 .then(function(file_system_file
) {
94 var formData
= new FormData();
95 formData
.append('sample string', '1234567890');
96 formData
.append('sample blob', new Blob(['blob content']));
97 formData
.append('sample file', new File(['file content'], 'file.dat'));
98 formData
.append('sample fs file', file_system_file
);
99 return xhr_send(host_info
['HTTP_ORIGIN'], 'POST', formData
, false);
101 .then(function(response
) {
102 assert_equals(response
.method
, 'POST');
103 var boundary
= get_boundary(response
.headers
);
105 '--' + boundary
+ '\r\n' +
106 'Content-Disposition: form-data; name="sample string"\r\n' +
109 '--' + boundary
+ '\r\n' +
110 'Content-Disposition: form-data; name="sample blob"; ' +
111 'filename="blob"\r\n' +
112 'Content-Type: application/octet-stream\r\n' +
115 '--' + boundary
+ '\r\n' +
116 'Content-Disposition: form-data; name="sample file"; ' +
117 'filename="file.dat"\r\n' +
118 'Content-Type: application/octet-stream\r\n' +
121 '--' + boundary
+ '\r\n' +
122 'Content-Disposition: form-data; name="sample fs file"; ' +
123 'filename="fsfile.txt"\r\n' +
124 'Content-Type: text/plain\r\n' +
126 'fs file content\r\n' +
127 '--' + boundary
+ '--\r\n';
128 assert_equals(response
.body
, expected_body
);
132 function mode_credentials_test() {
133 return xhr_send(host_info
['HTTP_ORIGIN'], 'GET', '', false)
134 .then(function(response
){
135 assert_equals(response
.mode
, 'cors');
136 assert_equals(response
.credentials
, 'same-origin');
137 return xhr_send(host_info
['HTTP_ORIGIN'], 'GET', '', true);
139 .then(function(response
){
140 assert_equals(response
.mode
, 'cors');
141 assert_equals(response
.credentials
, 'include');
142 return xhr_send(host_info
['HTTP_REMOTE_ORIGIN'], 'GET', '', false);
144 .then(function(response
){
145 assert_equals(response
.mode
, 'cors');
146 assert_equals(response
.credentials
, 'same-origin');
147 return xhr_send(host_info
['HTTP_REMOTE_ORIGIN'], 'GET', '', true);
149 .then(function(response
){
150 assert_equals(response
.mode
, 'cors');
151 assert_equals(response
.credentials
, 'include');
155 function data_url_test() {
156 return new Promise(function(resolve
, reject
) {
157 var xhr
= new XMLHttpRequest();
158 xhr
.onload = function() {
159 resolve(xhr
.response
);
161 xhr
.onerror = function() {
162 reject('XHR should succeed.');
164 xhr
.responseType
= 'text';
165 xhr
.open('GET', 'data:text/html,Foobar', true);
168 .then(function(data
) {
169 assert_equals(data
, 'Foobar');
173 window
.addEventListener('message', function(evt
) {
174 var port
= evt
.ports
[0];
177 .then(custom_method_test
)
178 .then(options_method_test
)
179 .then(form_data_test
)
180 .then(mode_credentials_test
)
182 .then(function() { port
.postMessage({results
: 'finish'}); })
183 .catch(function(e
) { port
.postMessage({results
: 'failure:' + e
}); });