Index: LayoutTests/http/tests/xmlhttprequest/response-json.html |
diff --git a/LayoutTests/http/tests/xmlhttprequest/response-json.html b/LayoutTests/http/tests/xmlhttprequest/response-json.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3252ef9fa52cd9398af80d64cdb46af88e2c624c |
--- /dev/null |
+++ b/LayoutTests/http/tests/xmlhttprequest/response-json.html |
@@ -0,0 +1,109 @@ |
+<html> |
+<body> |
+<p>Test XMLHttpRequest with responseType set to 'json' for various input</p> |
+<pre id="console"></pre> |
+<script> |
+if (window.testRunner) { |
+ testRunner.dumpAsText(); |
+ testRunner.waitUntilDone(); |
+} |
+ |
+function log(text) |
+{ |
+ var console = document.getElementById('console'); |
+ console.appendChild(document.createTextNode(text + '\n')); |
+} |
+ |
+function test(expect, actual) |
+{ |
+ log((expect == actual ? 'PASS' : 'FAIL') + ': "' + expect + '" == "' + actual + '"'); |
+} |
+ |
+var failureCases = [ |
+ '', |
+ '00', |
+ 'a' |
+]; |
+ |
+function testFailureCases() { |
+ if (failureCases.length == 0) { |
+ if (window.testRunner) |
+ testRunner.notifyDone(); |
+ return; |
+ } |
+ |
+ var json = failureCases.shift(); |
+ |
+ var xhr = new XMLHttpRequest; |
+ xhr.responseType = 'json'; |
+ xhr.open('POST', 'resources/post-echo.cgi', true); |
+ xhr.onreadystatechange = function() { |
+ if (this.readyState != 4) |
+ return; |
+ |
+ test(200, this.status); |
+ // When parsing fails, null must be returned. |
+ test(null, this.response); |
+ |
+ setTimeout(testFailureCases, 0); |
+ }; |
+ xhr.send(json); |
+} |
+ |
+var successfulCases = [ |
+ '1', |
+ '-1', |
+ 'null', |
+ '{}', |
+ '[]', |
+ '{"a":5,"b":10,"c":[{},5,"\\n"]}' |
+]; |
+ |
+function testSuccessfulCases() { |
+ if (successfulCases.length == 0) { |
+ setTimeout(testFailureCases, 0); |
+ return; |
+ } |
+ |
+ var json = successfulCases.shift(); |
+ |
+ var xhr = new XMLHttpRequest; |
+ xhr.responseType = 'json'; |
+ xhr.open('POST', 'resources/post-echo.cgi', true); |
+ xhr.onreadystatechange = function() { |
+ if (this.readyState != 4) |
+ return; |
+ |
+ test(200, this.status); |
+ test(json, JSON.stringify(this.response)); |
+ |
+ setTimeout(testSuccessfulCases, 0); |
+ }; |
+ xhr.send(json); |
+} |
+ |
+function testStaticFile() { |
+ var xhr = new XMLHttpRequest; |
+ xhr.responseType = 'json'; |
+ xhr.open('GET', 'resources/test.json', true); |
+ xhr.onreadystatechange = function() { |
+ if (this.readyState != 4) |
+ return; |
+ |
+ test(200, this.status); |
+ |
+ test(4, this.response.length) |
+ test('a', this.response[0]) |
+ test('b', this.response[1]) |
+ test(2, this.response[2]) |
+ test(3, this.response[3][3]) |
+ |
+ setTimeout(testSuccessfulCases, 0); |
+ }; |
+ xhr.send(); |
+} |
+ |
+testStaticFile(); |
+ |
+</script> |
+</body> |