chromium/third_party/blink/web_tests/http/tests/xmlhttprequest/xmlhttprequest-data-url.html

<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
// This test must be run over HTTP. Otherwise, content_shell runs it with file:
// scheme and then the access to data: resources are handled as same origin
// access.

async_test(t => {
  var xhr = new XMLHttpRequest;
  xhr.responseType = 'text';
  xhr.open('GET', 'data:text/html,Foobar', true);
  xhr.onreadystatechange = t.step_func(function() {
    if (xhr.readyState != xhr.DONE)
      return;

    assert_equals(xhr.status, 200, 'status');
    assert_equals(xhr.statusText, 'OK', 'statusText');
    assert_equals(xhr.getAllResponseHeaders(), 'content-type: text/html\r\n', 'getAllResponseheaders()');
    assert_equals(xhr.response, 'Foobar', 'response');

    t.done();
  });
  xhr.send();
}, "Test parsing a data URL. US-ASCII into DOMString");

async_test(t => {
  var xhr = new XMLHttpRequest;
  xhr.responseType = 'arraybuffer';
  xhr.open('GET', 'data:text/html;base64,AAEC/w%3D%3D', true);
  xhr.onreadystatechange = t.step_func(function() {
    if (xhr.readyState != xhr.DONE)
      return;

    assert_equals(xhr.status, 200, 'status');
    assert_equals(xhr.response.byteLength, 4, 'byteLength');
    var view = new Uint8Array(xhr.response);
    assert_equals(view[0], 0x00, 'view[0]')
    assert_equals(view[1], 0x01, 'view[1]')
    assert_equals(view[2], 0x02, 'view[2]')
    assert_equals(view[3], 0xff, 'view[3]')

    t.done();
  });
  xhr.send();
}, "Test parsing a data URL. Binary into ArrayBuffer");

async_test(t => {
  var xhr = new XMLHttpRequest;
  xhr.responseType = 'text';
  xhr.open('GET', 'data:text/html;charset=utf-8;base64,5paH5a2X', true);
  xhr.onreadystatechange = t.step_func(function() {
    if (xhr.readyState != xhr.DONE)
      return;

    assert_equals(xhr.status, 200, 'status');
    assert_equals(xhr.getAllResponseHeaders(), 'content-type: text/html;charset=utf-8\r\n', 'getAllResponseheaders()');
    assert_equals(xhr.response, '\u6587\u5b57', 'response');

    t.done();
  });
  xhr.send();
}, "Test parsing a data URL. UTF-8 data into DOMString.");

async_test(t => {
  var xhr = new XMLHttpRequest;
  xhr.responseType = 'blob';
  xhr.open('GET', 'data:text/html;charset=utf-8;base64,5paH5a2X', true);
  xhr.onreadystatechange = t.step_func(function() {
    if (xhr.readyState != xhr.DONE)
      return;


    assert_equals(xhr.status, 200, 'status');
    assert_equals(xhr.getAllResponseHeaders(), 'content-type: text/html;charset=utf-8\r\n', 'getAllResponseheaders()');
    assert_equals(xhr.response.size, 6);
    assert_equals(xhr.response.type, 'text/html');
    var reader = new FileReader;
    reader.readAsText(xhr.response);
    reader.onloadend = t.step_func(function() {
      assert_equals(reader.result, '\u6587\u5b57', 'result');
      t.done();
    });
  });
  xhr.send();
}, "Test parsing a data URL. UTF-8 data into Blob.");

async_test(t => {
  var xhr = new XMLHttpRequest;
  xhr.responseType = 'text';
  xhr.open('GET', 'data:text/html;base64,***', true);
  xhr.onreadystatechange = t.step_func(function() {
    if (xhr.readyState != xhr.DONE)
      return;

    assert_not_equals(xhr.status, 200, 'status');

    t.done();
  });
  xhr.send();
}, "Test parsing a data URL. Invalid Base64 data.");

</script>