<html>
<body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
var failureCases = [
'',
'00',
'a'
];
var i;
for (i = 0; i < failureCases.length; ++i) {
var json = failureCases[i];
var test = async_test("Test XMLHttpRequest with responseType set to 'json' for '" + json + "' expecting failure.");
test.step(function() {
var xhr = new XMLHttpRequest;
xhr.responseType = 'json';
xhr.open('POST', 'resources/post-echo.cgi', true);
var handler = function(test) {
if (this.readyState != 4)
return;
assert_equals(this.status, 200, 'xhr.status');
// When parsing fails, null must be returned.
assert_equals(this.response, null, 'xhr.response');
test.done();
};
xhr.onreadystatechange = test.step_func(handler.bind(xhr, test));
xhr.send(json);
});
}
var successfulCases = [
'1',
'-1',
'null',
'{}',
'[]',
'{"a":5,"b":10,"c":[{},5,"\\n"]}'
];
for (i = 0; i < successfulCases.length; ++i) {
var json = successfulCases[i];
var test = async_test("Test XMLHttpRequest with responseType set to 'json' for '" + json + "' expecting success.");
test.step(function() {
var xhr = new XMLHttpRequest;
xhr.responseType = 'json';
xhr.open('POST', 'resources/post-echo.cgi', true);
var handler = function(test, json) {
if (this.readyState != 4)
return;
assert_equals(this.status, 200, 'xhr.status');
assert_equals(JSON.stringify(this.response), json, 'JSON.stringify(xhr.response)');
test.done();
};
xhr.onreadystatechange = test.step_func(handler.bind(xhr, test, json));
xhr.send(json);
});
}
var staticTest = async_test("Test XMLHttpRequest with responseType set to 'json' for test.json.");
staticTest.step(function() {
var xhr = new XMLHttpRequest;
xhr.responseType = 'json';
xhr.open('GET', 'resources/test.json', true);
xhr.onreadystatechange = staticTest.step_func(function() {
if (xhr.readyState != 4)
return;
assert_equals(xhr.status, 200, 'xhr.status');
assert_equals(xhr.response.length, 4, 'xhr.response.length')
assert_equals(xhr.response[0], 'a', 'xhr.response[0]');
assert_equals(xhr.response[1], 'b', 'xhr.response[1]');
assert_equals(xhr.response[2], 2, 'xhr.response[2]');
assert_equals(xhr.response[3][3], 3, 'xhr.response[3][3]');
staticTest.done();
});
xhr.send();
});
</script>
</body>