Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / xmlhttprequest / response-json.html
blob4316be898b97f3144700c9da8a9da86035cb449e
1 <html>
2 <body>
3 <script src="../resources/testharness.js"></script>
4 <script src="../resources/testharnessreport.js"></script>
5 <script>
7 var failureCases = [
8 '',
9 '00',
10 'a'
13 var i;
15 for (i = 0; i < failureCases.length; ++i) {
16 var json = failureCases[i];
17 var test = async_test("Test XMLHttpRequest with responseType set to 'json' for '" + json + "' expecting failure.");
19 test.step(function() {
20 var xhr = new XMLHttpRequest;
21 xhr.responseType = 'json';
22 xhr.open('POST', 'resources/post-echo.cgi', true);
23 var handler = function(test) {
24 if (this.readyState != 4)
25 return;
27 assert_equals(this.status, 200, 'xhr.status');
28 // When parsing fails, null must be returned.
29 assert_equals(this.response, null, 'xhr.response');
31 test.done();
33 xhr.onreadystatechange = test.step_func(handler.bind(xhr, test));
34 xhr.send(json);
35 });
38 var successfulCases = [
39 '1',
40 '-1',
41 'null',
42 '{}',
43 '[]',
44 '{"a":5,"b":10,"c":[{},5,"\\n"]}'
47 for (i = 0; i < successfulCases.length; ++i) {
48 var json = successfulCases[i];
49 var test = async_test("Test XMLHttpRequest with responseType set to 'json' for '" + json + "' expecting success.");
51 test.step(function() {
52 var xhr = new XMLHttpRequest;
53 xhr.responseType = 'json';
54 xhr.open('POST', 'resources/post-echo.cgi', true);
55 var handler = function(test, json) {
56 if (this.readyState != 4)
57 return;
59 assert_equals(this.status, 200, 'xhr.status');
60 assert_equals(JSON.stringify(this.response), json, 'JSON.stringify(xhr.response)');
62 test.done();
64 xhr.onreadystatechange = test.step_func(handler.bind(xhr, test, json));
65 xhr.send(json);
66 });
69 var staticTest = async_test("Test XMLHttpRequest with responseType set to 'json' for test.json.");
70 staticTest.step(function() {
71 var xhr = new XMLHttpRequest;
72 xhr.responseType = 'json';
73 xhr.open('GET', 'resources/test.json', true);
74 xhr.onreadystatechange = staticTest.step_func(function() {
75 if (xhr.readyState != 4)
76 return;
78 assert_equals(xhr.status, 200, 'xhr.status');
80 assert_equals(xhr.response.length, 4, 'xhr.response.length')
81 assert_equals(xhr.response[0], 'a', 'xhr.response[0]');
82 assert_equals(xhr.response[1], 'b', 'xhr.response[1]');
83 assert_equals(xhr.response[2], 2, 'xhr.response[2]');
84 assert_equals(xhr.response[3][3], 3, 'xhr.response[3][3]');
86 staticTest.done();
87 });
88 xhr.send();
89 });
91 </script>
92 </body>