LCOV - code coverage report
Current view: top level - test/wpt/xhr - formdata.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 137 137 100.0 %
Date: 2025-01-24 02:02:48 Functions: 4 4 100.0 %
Branches: 7 7 100.0 %

           Branch data     Line data    Source code
       1            [ + ]:          1 : import assert from 'node:assert/strict';
       2                 :          1 : import { XMLHttpRequest, FormData } from '../../../lib/whatwg-xhr.js';
       3                 :          1 : 
       4            [ + ]:          1 : export default (activeURL) => {
       5                 :          1 : 
       6                 :          1 :         do_test('empty formdata', new FormData(), '\n');
       7                 :          1 :         do_test('formdata with string', create_formdata(['key', 'value']), 'key=value,\n');
       8                 :          1 :         do_test('formdata with named string', create_formdata(['key', new Blob(['value'], {type: 'text/plain'}), 'kv.txt']), '\nkey=kv.txt:text/plain:5,');
       9                 :          1 :         // do_test('formdata from form', new FormData(document.getElementById('form')), 'key=value,\n');
      10                 :          1 :         do_test('formdata with blob', create_formdata(['key', new Blob(['value'], {type: 'text/x-value'})]), '\nkey=blob:text/x-value:5,');
      11                 :          1 :         do_test('formdata with named blob', create_formdata(['key', new Blob(['value'], {type: 'text/x-value'}), 'blob.txt']), '\nkey=blob.txt:text/x-value:5,');
      12                 :          1 : 
      13            [ + ]:          1 :         function create_formdata(){
      14                 :          4 :                 const fd = new FormData();
      15                 :          4 : 
      16                 :          4 :                 for (let i = 0; i < arguments.length; i++){
      17                 :          4 :                         fd.append.apply(fd, arguments[i]);
      18                 :          4 :                 };
      19                 :          4 : 
      20                 :          4 :                 return fd;
      21                 :          4 :         }
      22                 :          1 : 
      23            [ + ]:          1 :         function do_test(name, fd, expected){
      24                 :          5 : 
      25                 :          5 :                 const xhr = new XMLHttpRequest();
      26                 :          5 : 
      27            [ + ]:          5 :                 xhr.onreadystatechange = () => {
      28                 :         20 :                         if(xhr.readyState !== 4)
      29       [ + ][ + ]:         20 :                                 return;
      30                 :          5 : 
      31                 :          5 :                         assert.strictEqual(xhr.responseText, expected);
      32                 :          5 :                         xhr.onreadystatechange = null;
      33                 :          5 :                 };
      34                 :          5 : 
      35                 :          5 :                 xhr.open('POST', `${activeURL}/upload.py`);
      36                 :          5 :                 xhr.send(fd);
      37                 :          5 :         }
      38                 :          1 : }
      39                 :          1 : 
      40                 :          1 : /*
      41                 :          1 :  * formdata.html
      42                 :          1 :  *
      43                 :          1 : 
      44                 :          1 : <!doctype html>
      45                 :          1 : <html lang=en>
      46                 :          1 : <meta charset=utf-8>
      47                 :          1 : <title>XMLHttpRequest: Construct and upload FormData</title>
      48                 :          1 : <script src="/resources/testharness.js"></script>
      49                 :          1 : <script src="/resources/testharnessreport.js"></script>
      50                 :          1 : <script src="/html/semantics/forms/form-submission-0/resources/targetted-form.js"></script>
      51                 :          1 :     <link rel="help" href="https://xhr.spec.whatwg.org/#interface-formdata" data-tested-assertations="following::P[1]" />
      52                 :          1 :     <link rel="help" href="https://xhr.spec.whatwg.org/#dom-formdata" data-tested-assertations=".. following::P[1]" />
      53                 :          1 :     <link rel="help" href="https://xhr.spec.whatwg.org/#dom-formdata-append" data-tested-assertations=".. following::UL[1]/LI[1] following::UL[1]/LI[2] following::UL[1]/LI[3]" />
      54                 :          1 :     <link rel="help" href="https://xhr.spec.whatwg.org/#dom-XMLHttpRequest-send-FormData" data-tested-assertations="following::DD[1]" />
      55                 :          1 : <link rel="help" href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set">
      56                 :          1 : 
      57                 :          1 : <div id="log"></div>
      58                 :          1 : <form id="form">
      59                 :          1 :   <input type="hidden" name="key" value="value">
      60                 :          1 : </form>
      61                 :          1 : <script>
      62                 :          1 :   function do_test (name, fd, expected) {
      63                 :          1 :     var test = async_test(name);
      64                 :          1 :     test.step(function() {
      65                 :          1 :       var client = new XMLHttpRequest();
      66                 :          1 :       client.onreadystatechange = test.step_func(function () {
      67                 :          1 :         if (client.readyState !== 4) return;
      68                 :          1 :         assert_equals(client.responseText, expected);
      69                 :          1 :         test.done();
      70                 :          1 :       });
      71                 :          1 :       client.open("POST", "resources/upload.py");
      72                 :          1 :       client.send(fd);
      73                 :          1 :     });
      74                 :          1 :   }
      75                 :          1 : 
      76                 :          1 :   function create_formdata () {
      77                 :          1 :     var fd = new FormData();
      78                 :          1 :     for (var i = 0; i < arguments.length; i++) {
      79                 :          1 :       fd.append.apply(fd, arguments[i]);
      80                 :          1 :     };
      81                 :          1 :     return fd;
      82                 :          1 :   }
      83                 :          1 : 
      84                 :          1 :   do_test("empty formdata", new FormData(), '\n');
      85                 :          1 :   do_test("formdata with string", create_formdata(['key', 'value']), 'key=value,\n');
      86                 :          1 :   do_test("formdata with named string", create_formdata(['key', new Blob(['value'], {type: 'text/plain'}), 'kv.txt']), '\nkey=kv.txt:text/plain:5,');
      87                 :          1 :   do_test("formdata from form", new FormData(document.getElementById('form')), 'key=value,\n');
      88                 :          1 : 
      89                 :          1 :   do_test("formdata with blob", create_formdata(['key', new Blob(['value'], {type: 'text/x-value'})]), '\nkey=blob:text/x-value:5,');
      90                 :          1 :   do_test("formdata with named blob", create_formdata(['key', new Blob(['value'], {type: 'text/x-value'}), 'blob.txt']), '\nkey=blob.txt:text/x-value:5,');
      91                 :          1 : 
      92                 :          1 :   // If 3rd argument is given and 2nd is not a Blob, formdata.append() should throw
      93                 :          1 :   const append_test = async_test('formdata.append() should throw if value is string and file name is given'); // needs to be async just because the others above are
      94                 :          1 :   append_test.step(function(){
      95                 :          1 :     assert_throws_js(TypeError, function(){
      96                 :          1 :       create_formdata('a', 'b', 'c');
      97                 :          1 :     });
      98                 :          1 :   });
      99                 :          1 :   append_test.done();
     100                 :          1 : 
     101                 :          1 :   test(() => {
     102                 :          1 :     let form = populateForm('<input name=n1 value=v1>');
     103                 :          1 :     let formDataInEvent = null;
     104                 :          1 :     form.addEventListener('formdata', e => {
     105                 :          1 :       e.formData.append('h1', 'vh1');
     106                 :          1 :       formDataInEvent = e.formData;
     107                 :          1 :     });
     108                 :          1 :     let formData = new FormData(form);
     109                 :          1 :     assert_equals(formData.get('h1'), 'vh1');
     110                 :          1 :     assert_equals(formData.get('n1'), 'v1');
     111                 :          1 :     assert_not_equals(formData, formDataInEvent,
     112                 :          1 :                       '"formData" attribute should be different from the ' +
     113                 :          1 :                       'FromData object created by "new"');
     114                 :          1 : 
     115                 :          1 :     formDataInEvent.append('later-key', 'later-value');
     116                 :          1 :     assert_false(formData.has('later-key'));
     117                 :          1 :   }, 'Newly created FormData contains entries added to "formData" IDL ' +
     118                 :          1 :      'attribute of FormDataEvent.');
     119                 :          1 : 
     120                 :          1 :   test(() => {
     121                 :          1 :     let form = populateForm('<input name=n11 value=v11>');
     122                 :          1 :     let counter = 0;
     123                 :          1 :     form.addEventListener('formdata', e => {
     124                 :          1 :       ++counter;
     125                 :          1 :       assert_throws_dom('InvalidStateError', () => { new FormData(e.target) });
     126                 :          1 :     });
     127                 :          1 :     new FormData(form);
     128                 :          1 :     assert_equals(counter, 1);
     129                 :          1 : 
     130                 :          1 :     form.submit();
     131                 :          1 :     assert_equals(counter, 2);
     132                 :          1 :   }, '|new FormData()| in formdata event handler should throw');
     133                 :          1 : </script>
     134                 :          1 : 
     135                 :          1 :  *
     136                 :          1 :  *
     137                 :          1 :  */

Generated by: LCOV version 1.14