OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <body> |
| 3 <p>Test XMLHttpRequest with responseType set to 'json' for various input</p> |
| 4 <pre id="console"></pre> |
| 5 <script> |
| 6 if (window.testRunner) { |
| 7 testRunner.dumpAsText(); |
| 8 testRunner.waitUntilDone(); |
| 9 } |
| 10 |
| 11 function log(text) |
| 12 { |
| 13 var console = document.getElementById('console'); |
| 14 console.appendChild(document.createTextNode(text + '\n')); |
| 15 } |
| 16 |
| 17 function test(expect, actual) |
| 18 { |
| 19 log((expect == actual ? 'PASS' : 'FAIL') + ': "' + expect + '" == "' + actua
l + '"'); |
| 20 } |
| 21 |
| 22 var failureCases = [ |
| 23 '', |
| 24 '00', |
| 25 'a' |
| 26 ]; |
| 27 |
| 28 function testFailureCases() { |
| 29 if (failureCases.length == 0) { |
| 30 if (window.testRunner) |
| 31 testRunner.notifyDone(); |
| 32 return; |
| 33 } |
| 34 |
| 35 var json = failureCases.shift(); |
| 36 |
| 37 var xhr = new XMLHttpRequest; |
| 38 xhr.responseType = 'json'; |
| 39 xhr.open('POST', 'resources/post-echo.cgi', true); |
| 40 xhr.onreadystatechange = function() { |
| 41 if (this.readyState != 4) |
| 42 return; |
| 43 |
| 44 test(200, this.status); |
| 45 // When parsing fails, null must be returned. |
| 46 test(null, this.response); |
| 47 |
| 48 setTimeout(testFailureCases, 0); |
| 49 }; |
| 50 xhr.send(json); |
| 51 } |
| 52 |
| 53 var successfulCases = [ |
| 54 '1', |
| 55 '-1', |
| 56 'null', |
| 57 '{}', |
| 58 '[]', |
| 59 '{"a":5,"b":10,"c":[{},5,"\\n"]}' |
| 60 ]; |
| 61 |
| 62 function testSuccessfulCases() { |
| 63 if (successfulCases.length == 0) { |
| 64 setTimeout(testFailureCases, 0); |
| 65 return; |
| 66 } |
| 67 |
| 68 var json = successfulCases.shift(); |
| 69 |
| 70 var xhr = new XMLHttpRequest; |
| 71 xhr.responseType = 'json'; |
| 72 xhr.open('POST', 'resources/post-echo.cgi', true); |
| 73 xhr.onreadystatechange = function() { |
| 74 if (this.readyState != 4) |
| 75 return; |
| 76 |
| 77 test(200, this.status); |
| 78 test(json, JSON.stringify(this.response)); |
| 79 |
| 80 setTimeout(testSuccessfulCases, 0); |
| 81 }; |
| 82 xhr.send(json); |
| 83 } |
| 84 |
| 85 function testStaticFile() { |
| 86 var xhr = new XMLHttpRequest; |
| 87 xhr.responseType = 'json'; |
| 88 xhr.open('GET', 'resources/test.json', true); |
| 89 xhr.onreadystatechange = function() { |
| 90 if (this.readyState != 4) |
| 91 return; |
| 92 |
| 93 test(200, this.status); |
| 94 |
| 95 test(4, this.response.length) |
| 96 test('a', this.response[0]) |
| 97 test('b', this.response[1]) |
| 98 test(2, this.response[2]) |
| 99 test(3, this.response[3][3]) |
| 100 |
| 101 setTimeout(testSuccessfulCases, 0); |
| 102 }; |
| 103 xhr.send(); |
| 104 } |
| 105 |
| 106 testStaticFile(); |
| 107 |
| 108 </script> |
| 109 </body> |
OLD | NEW |