blob: 609ac6e433a88b35191f40879e3eddad4a4fc429 [file] [log] [blame]
<script src="../../resources/testharness.js"></script>
<script src="../../resources/get-host-info.js?pipe=sub"></script>
<script src="test-helpers.js"></script>
<script>
var host_info = get_host_info();
function get_boundary(headers) {
var reg = new RegExp('multipart\/form-data; boundary=(.*)');
for (var i = 0; i < headers.length; ++i) {
if (headers[i][0] != 'content-type') {
continue;
}
var regResult = reg.exec(headers[i][1]);
if (!regResult) {
continue;
}
return regResult[1];
}
return '';
}
function create_file_system_file(file_name, data) {
return new Promise(function(resolve, reject) {
webkitRequestFileSystem(TEMPORARY, 1024, function(fs) {
fs.root.getFile(
file_name, {create: true, exclusive: true},
function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
fileEntry.file(function(file) { resolve(file); });
};
var blob = new Blob([data], {type: 'text/plain'});
fileWriter.write(blob);
});
}, function(e) { reject(e); });
}, function(e) { reject(e); });
});
}
function xhr_send(url_base, method, data, with_credentials) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(JSON.parse(xhr.response));
};
xhr.onerror = function() {
reject('XHR should succeed.');
};
xhr.responseType = 'text';
if (with_credentials) {
xhr.withCredentials = true;
}
xhr.open(method, url_base + '/dummy?test', true);
xhr.send(data);
});
}
function get_sorted_header_name_list(headers) {
const header_names = [];
for (const [name, value] of headers) {
header_names.push(name);
}
header_names.sort();
return header_names;
}
function get_header_test() {
return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept", "user-agent"],
'event.request has the expected headers for same-origin GET.');
});
}
// TODO(tyoshino): Fix the stack not to include the Origin header as specified
// in the spec.
function post_header_test() {
return xhr_send(host_info['HTTP_ORIGIN'], 'POST', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept", "content-type", "origin", "user-agent"],
'event.request has the expected headers for same-origin POST.');
});
}
function cross_origin_get_header_test() {
return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept", "user-agent"],
'event.request has the expected headers for cross-origin GET.');
});
}
// TODO(tyoshino): Fix the stack not to include the Origin header as specified
// in the spec.
function cross_origin_post_header_test() {
return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'POST', '', false)
.then(function(response) {
assert_array_equals(
get_sorted_header_name_list(response.headers),
["accept", "content-type", "origin", "user-agent"],
'event.request has the expected headers for cross-origin POST.');
});
}
function string_test() {
return xhr_send(host_info['HTTP_ORIGIN'], 'POST', 'test string', false)
.then(function(response) {
assert_equals(response.method, 'POST');
assert_equals(response.body, 'test string');
});
}
function blob_test() {
return xhr_send(host_info['HTTP_ORIGIN'], 'POST', new Blob(['test blob']),
false)
.then(function(response) {
assert_equals(response.method, 'POST');
assert_equals(response.body, 'test blob');
});
}
function custom_method_test() {
return xhr_send(host_info['HTTP_ORIGIN'], 'XXX', 'test string xxx', false)
.then(function(response) {
assert_equals(response.method, 'XXX');
assert_equals(response.body, 'test string xxx');
});
}
function options_method_test() {
return xhr_send(host_info['HTTP_ORIGIN'], 'OPTIONS', 'test string xxx', false)
.then(function(response) {
assert_equals(response.method, 'OPTIONS');
assert_equals(response.body, 'test string xxx');
});
}
function form_data_test() {
return create_file_system_file('fsfile.txt', 'fs file content')
.then(function(file_system_file) {
var formData = new FormData();
formData.append('sample string', '1234567890');
formData.append('sample blob', new Blob(['blob content']));
formData.append('sample file', new File(['file content'], 'file.dat'));
formData.append('sample fs file', file_system_file);
return xhr_send(host_info['HTTP_ORIGIN'], 'POST', formData, false);
})
.then(function(response) {
assert_equals(response.method, 'POST');
var boundary = get_boundary(response.headers);
var expected_body =
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="sample string"\r\n' +
'\r\n' +
'1234567890\r\n' +
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="sample blob"; ' +
'filename="blob"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\n' +
'blob content\r\n' +
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="sample file"; ' +
'filename="file.dat"\r\n' +
'Content-Type: application/octet-stream\r\n' +
'\r\n' +
'file content\r\n' +
'--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="sample fs file"; ' +
'filename="fsfile.txt"\r\n' +
'Content-Type: text/plain\r\n' +
'\r\n' +
'fs file content\r\n' +
'--' + boundary + '--\r\n';
assert_equals(response.body, expected_body);
});
}
function mode_credentials_test() {
return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', false)
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'include');
return xhr_send(host_info['HTTP_ORIGIN'], 'GET', '', true);
})
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'include');
return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', false);
})
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'same-origin');
return xhr_send(host_info['HTTP_REMOTE_ORIGIN'], 'GET', '', true);
})
.then(function(response){
assert_equals(response.mode, 'cors');
assert_equals(response.credentials, 'include');
});
}
function data_url_test() {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
resolve(xhr.response);
};
xhr.onerror = function() {
reject('XHR should succeed.');
};
xhr.responseType = 'text';
xhr.open('GET', 'data:text/html,Foobar', true);
xhr.send();
})
.then(function(data) {
assert_equals(data, 'Foobar');
});
}
window.addEventListener('message', function(evt) {
var port = evt.ports[0];
get_header_test()
.then(post_header_test)
.then(cross_origin_get_header_test)
.then(cross_origin_post_header_test)
.then(string_test)
.then(blob_test)
.then(custom_method_test)
.then(options_method_test)
.then(form_data_test)
.then(mode_credentials_test)
.then(data_url_test)
.then(function() { port.postMessage({results: 'finish'}); })
.catch(function(e) { port.postMessage({results: 'failure:' + e}); });
});
</script>