send-data-es-object.any.js (2460B)
1 // META: title=XMLHttpRequest.send(ES object) 2 3 function do_test(obj, expected, name) { 4 var test = async_test(name) 5 test.step(function() { 6 var client = new XMLHttpRequest() 7 client.onload = test.step_func(function () { 8 assert_equals(client.responseText, expected) 9 test.done() 10 }); 11 client.open('POST', 'resources/content.py') 12 if (expected.exception) { 13 if (expected.exception.identity) { 14 assert_throws_exactly(expected.exception.identity, 15 function(){client.send(obj)}) 16 } else { 17 assert_throws_js(expected.exception.ctor, 18 function(){client.send(obj)}) 19 } 20 test.done() 21 } else { 22 client.send(obj) 23 } 24 }); 25 } 26 27 do_test({}, '[object Object]', 'sending a plain empty object') 28 do_test(Math, '[object Math]', 'sending the ES Math object') 29 do_test(new XMLHttpRequest, '[object XMLHttpRequest]', 'sending a new XHR instance') 30 do_test(new ReadableStream, '[object ReadableStream]', 'sending a new ReadableStream instance') 31 do_test({toString:function(){}}, 'undefined', 'sending object that stringifies to undefined') 32 do_test({toString:function(){return null}}, 'null', 'sending object that stringifies to null') 33 var ancestor = {toString: function(){ 34 var ar=[] 35 for (var prop in this) { 36 if (this.hasOwnProperty(prop)) { 37 ar.push(prop+'='+this[prop]) 38 } 39 }; 40 return ar.join('&') 41 }}; 42 43 var myObj = Object.create(ancestor, {foo:{value:1, enumerable: true}, bar:{value:'foo', enumerable:true}}) 44 do_test(myObj, 'foo=1&bar=foo', 'object that stringifies to query string') 45 46 var myFakeJSON = {a:'a', b:'b', toString:function(){ return JSON.stringify(this, function(key, val){ return key ==='toString'?undefined:val; }) }} 47 do_test(myFakeJSON, '{"a":"a","b":"b"}', 'object that stringifies to JSON string') 48 49 var myFakeDoc1 = {valueOf:function(){return document}} 50 do_test(myFakeDoc1, '[object Object]', 'object whose valueOf() returns a document - ignore valueOf(), stringify') 51 52 var myFakeDoc2 = {toString:function(){return document}} 53 var expectedError = self.GLOBAL.isWorker() ? ReferenceError : TypeError; 54 do_test(myFakeDoc2, {exception: { ctor: expectedError } }, 'object whose toString() returns a document, expected to throw') 55 56 var err = {name:'FooError', message:'bar'}; 57 var myThrower = {toString:function(){throw err;}}; 58 do_test(myThrower, {exception: { identity: err }}, 'object whose toString() throws, expected to throw')