tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

response-json.htm (2939B)


      1 <!doctype html>
      2 <html>
      3  <head>
      4    <title>XMLHttpRequest: responseType json</title>
      5    <meta charset="utf-8">
      6    <script src="/resources/testharness.js"></script>
      7    <script src="/resources/testharnessreport.js"></script>
      8        <link rel="help" href="https://xhr.spec.whatwg.org/#the-responsetype-attribute" data-tested-assertations="following::OL[1]/LI[4]" />
      9        <link rel="help" href="https://xhr.spec.whatwg.org/#the-response-attribute" data-tested-assertations="following::dt[2]/dt[4] following::dt[2]/dt[4]/following::dd[1]" />
     10        <link rel="help" href="https://xhr.spec.whatwg.org/#json-response-entity-body" data-tested-assertations="following::ol[1]/li[1] following::ol[1]/li[2] following::ol[1]/li[3]" />
     11 
     12  </head>
     13  <body>
     14    <div id="log"></div>
     15    <script>
     16      function setupXHR () {
     17        var client = new XMLHttpRequest()
     18        client.open('POST', "resources/content.py", true)
     19        client.responseType = 'json'
     20        return client
     21      }
     22      function makeTest(data, expectedResponse, description){
     23        var test = async_test(description)
     24        var xhr = setupXHR()
     25        assert_equals(xhr.responseType, 'json')
     26        xhr.onreadystatechange = function(){
     27            if(xhr.readyState === 4){
     28                test.step(function(){
     29                    assert_equals(xhr.status, 200)
     30                    assert_equals(xhr.responseType, 'json')
     31                    assert_equals(typeof xhr.response, 'object')
     32                    if(expectedResponse){ // if the expectedResponse is not null, we iterate over properties to do a deeper comparison..
     33                        for(var prop in expectedResponse){
     34                          if (expectedResponse[prop] instanceof Array) {
     35                            assert_array_equals(expectedResponse[prop], xhr.response[prop])
     36                          }else{
     37                            assert_equals(expectedResponse[prop], xhr.response[prop])
     38                          }
     39                        }
     40                    }else{
     41                        assert_equals(xhr.response, expectedResponse) // null comparison, basically
     42                    }
     43                    assert_equals(xhr.response, xhr.response,
     44                                  "Response should be cached")
     45                    test.done()
     46                })
     47            }
     48        }
     49        xhr.send(data)
     50      }
     51      // no data
     52      makeTest("", null, 'json response with no data: response property is null')
     53      // malformed
     54      makeTest('{"test":"foo"', null, 'json response with malformed data: response property is null')
     55      // real object
     56      var obj = {alpha:'a-z', integer:15003, negated:-20, b1:true, b2:false, myAr:['a', 'b', 'c', 1, 2, 3]}
     57      makeTest(JSON.stringify(obj), obj,  'JSON object roundtrip')
     58      makeTest('{"日本語":"にほんご"}', {"日本語":"にほんご"}, 'JSON roundtrip with Japanese text')
     59    </script>
     60  </body>
     61 </html>