blob: 6f7743286ecdd06a52ce545d56dfb1589242996e [file] [log] [blame]
Hallvord R. M. Steenfe69bb42014-11-10 22:35:191<!doctype html>
2<meta charset=utf-8>
3<title>XMLHttpRequest: passing objects to send()</title>
4<script src="/resources/testharness.js"></script>
5<script src="/resources/testharnessreport.js"></script>
Hallvord R. M. Steen7b400412014-11-11 12:00:546<link rel="help" href="https://xhr.spec.whatwg.org/#the-send()-method" data-tested-assertations="following::ol/li[4]" />
7<link rel="help" href="https://heycam.github.io/webidl/#es-union" data-tested-assertations="following::ol/li[16]" />
Hallvord R. M. Steenfe69bb42014-11-10 22:35:198
9<div id="log"></div>
10
11<script>
12 function do_test(obj, expected, name) {
13 var test = async_test(name)
14 test.step(function() {
15 var client = new XMLHttpRequest()
16 client.onload = test.step_func(function () {
17 assert_equals(client.responseText, expected)
18 test.done()
19 });
20 client.open('POST', 'resources/content.py')
Hallvord R. M. Steenc7653ce2014-11-11 12:02:0221 if (expected.exception) {
Hallvord R. M. Steenfe69bb42014-11-10 22:35:1922 assert_throws(expected.exception, function(){client.send(obj)})
23 test.done()
Hallvord R. M. Steenc7653ce2014-11-11 12:02:0224 } else {
Hallvord R. M. Steenfe69bb42014-11-10 22:35:1925 client.send(obj)
26 }
27 });
28 }
29
30 do_test({}, '[object Object]', 'sending a plain empty object')
31 do_test(Math, '[object Math]', 'sending the ES Math object')
32 do_test(new XMLHttpRequest, '[object XMLHttpRequest]', 'sending a new XHR instance')
Hallvord R. M. Steen49ee52e2014-11-10 22:46:5633 do_test({toString:function(){}}, 'undefined', 'sending object that stringifies to undefined')
34 do_test({toString:function(){return null}}, 'null', 'sending object that stringifies to null')
Hallvord R. M. Steenfe69bb42014-11-10 22:35:1935 var ancestor = {toString: function(){
36 var ar=[]
Hallvord R. M. Steenc7653ce2014-11-11 12:02:0237 for (var prop in this) {
38 if (this.hasOwnProperty(prop)) {
Hallvord R. M. Steenfe69bb42014-11-10 22:35:1939 ar.push(prop+'='+this[prop])
40 }
41 };
42 return ar.join('&')
43 }};
44
45 var myObj = Object.create(ancestor, {foo:{value:1, enumerable: true}, bar:{value:'foo', enumerable:true}})
46 do_test(myObj, 'foo=1&bar=foo', 'object that stringifies to query string')
47
48 var myFakeJSON = {a:'a', b:'b', toString:function(){ return JSON.stringify(this, function(key, val){ return key ==='toString'?undefined:val; }) }}
49 do_test(myFakeJSON, '{"a":"a","b":"b"}', 'object that stringifies to JSON string')
50
51 var myFakeDoc1 = {valueOf:function(){return document}}
52 do_test(myFakeDoc1, '[object Object]', 'object whose valueOf() returns a document - ignore valueOf(), stringify')
53
54 var myFakeDoc2 = {toString:function(){return document}}
55 do_test(myFakeDoc2, {exception:new TypeError()}, 'object whose toString() returns a document, expected to throw')
56
Hallvord R. M. Steen49ee52e2014-11-10 22:46:5657 var myThrower = {toString:function(){throw {name:'FooError', message:'bar'}}}
58 do_test(myThrower, {exception:{name:'FooError'}}, 'object whose toString() throws, expected to throw')
59
Hallvord R. M. Steenfe69bb42014-11-10 22:35:1960
61</script>