LCOV - code coverage report
Current view: top level - test/wpt/xhr - setrequestheader-bogus-name.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 129 129 100.0 %
Date: 2024-12-07 00:20:21 Functions: 7 7 100.0 %
Branches: 11 11 100.0 %

           Branch data     Line data    Source code
       1            [ + ]:          2 : import assert from 'node:assert/strict';
       2                 :          2 : import { XMLHttpRequest } from '../../../lib/whatwg-xhr.js';
       3                 :          2 : 
       4            [ + ]:          2 : export default (activeURL) => {
       5                 :          1 : 
       6                 :          1 :         let invalid_headers = [
       7                 :          1 :                 "(", ")", "<", ">", "@", ",", ";", ":", "\\",
       8                 :          1 :                 "\"", "/", "[", "]", "?", "=", "{", "}", " ",
       9                 :          1 :                 "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
      10                 :          1 :                 "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
      11                 :          1 :                 "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"
      12                 :          1 :         ];
      13                 :          1 : 
      14                 :          1 :         let invalid_byte_strings = ["テスト", "X-テスト"];
      15                 :          1 : 
      16            [ + ]:          1 :         for(let i = 0; i < 32; ++i){
      17                 :         32 :                 invalid_headers.push(String.fromCharCode(i));
      18                 :         32 :         }
      19                 :          1 : 
      20            [ + ]:          1 :         for(let i = 0; i < invalid_headers.length; ++i){
      21                 :         69 :                 try_name(invalid_headers[i]);
      22                 :         69 :         }
      23                 :          1 : 
      24            [ + ]:          1 :         for(let i = 0; i < invalid_byte_strings.length; ++i){
      25                 :          2 :                 try_byte_string(invalid_byte_strings[i]);
      26                 :          2 :         }
      27                 :          1 : 
      28            [ + ]:          1 :         function try_name(name){
      29                 :         69 :                 const xhr = new XMLHttpRequest();
      30                 :         69 : 
      31                 :         69 :                 xhr.open('GET', `${activeURL}/...`);
      32                 :         69 :                 // setRequestHeader should throw with header name
      33                 :         69 :                 assert.throws(
      34            [ + ]:         69 :                         () => {
      35                 :         69 :                                 xhr.setRequestHeader(name, 'x-value');
      36                 :         69 :                         },
      37            [ + ]:         69 :                         (err) => {
      38                 :         69 :                                 assert(err instanceof DOMException, 'DOMException expected');
      39                 :         69 :                                 assert(err.name === 'SyntaxError', 'SyntaxError expected');
      40                 :         69 : 
      41                 :         69 :                                 return true;
      42                 :         69 :                         }
      43                 :         69 :                 );
      44                 :         69 :         }
      45                 :          1 : 
      46            [ + ]:          1 :         function try_byte_string(name) {
      47                 :          2 :                 const xhr = new XMLHttpRequest();
      48                 :          2 : 
      49                 :          2 :                 xhr.open('GET', `${activeURL}/...`);
      50                 :          2 :                 // setRequestHeader should throw with header name
      51                 :          2 :                 assert.throws(
      52            [ + ]:          2 :                         () => {
      53                 :          2 :                                 xhr.setRequestHeader(name, 'x-value');
      54                 :          2 :                         },
      55            [ + ]:          2 :                         (err) => {
      56                 :          2 :                                 assert(err instanceof TypeError, 'TypeError expected');
      57                 :          2 :                                 return true;
      58                 :          2 :                         }
      59                 :          2 :                 );
      60                 :          2 :         }
      61                 :          1 : }
      62                 :          2 : 
      63                 :          2 : /*
      64                 :          2 :  * setrequestheader-bogus-name.htm
      65                 :          2 :  *
      66                 :          2 : 
      67                 :          2 : <!doctype html>
      68                 :          2 : <html>
      69                 :          2 :   <head>
      70                 :          2 :     <title>XMLHttpRequest: setRequestHeader() name argument checks</title>
      71                 :          2 :     <meta charset="utf-8">
      72                 :          2 :     <script src="/resources/testharness.js"></script>
      73                 :          2 :     <script src="/resources/testharnessreport.js"></script>
      74                 :          2 :     <link rel="help" href="https://xhr.spec.whatwg.org/#the-setrequestheader()-method" data-tested-assertations="/following::ol/li[3]" />
      75                 :          2 :   </head>
      76                 :          2 :   <body>
      77                 :          2 :     <div id="log"></div>
      78                 :          2 : <!--
      79                 :          2 :        CHAR           = <any US-ASCII character (octets 0 - 127)>
      80                 :          2 :        CTL            = <any US-ASCII control character
      81                 :          2 :                         (octets 0 - 31) and DEL (127)>
      82                 :          2 :        SP             = <US-ASCII SP, space (32)>
      83                 :          2 :        HT             = <US-ASCII HT, horizontal-tab (9)>
      84                 :          2 :        token          = 1*<any CHAR except CTLs or separators>
      85                 :          2 :        separators     = "(" | ")" | "<" | ">" | "@"
      86                 :          2 :                       | "," | ";" | ":" | "\" | <">
      87                 :          2 :                       | "/" | "[" | "]" | "?" | "="
      88                 :          2 :                       | "{" | "}" | SP | HT
      89                 :          2 :        field-name     = token
      90                 :          2 : -->
      91                 :          2 :     <script>
      92                 :          2 :       function try_name(name) {
      93                 :          2 :         test(function() {
      94                 :          2 :           var client = new XMLHttpRequest()
      95                 :          2 :           client.open("GET", "...")
      96                 :          2 :           assert_throws_dom("SyntaxError", function() { client.setRequestHeader(name, 'x-value') })
      97                 :          2 :         }, "setRequestHeader should throw with header name " + format_value(invalid_headers[i]) + ".")
      98                 :          2 :       }
      99                 :          2 :       function try_byte_string(name) {
     100                 :          2 :         test(function() {
     101                 :          2 :           var client = new XMLHttpRequest()
     102                 :          2 :           client.open("GET", "...")
     103                 :          2 :           assert_throws_js(TypeError, function() { client.setRequestHeader(name, 'x-value') })
     104                 :          2 :         }, "setRequestHeader should throw with header name " + format_value(invalid_byte_strings[i]) + ".")
     105                 :          2 :       }
     106                 :          2 :       var invalid_headers = ["(", ")", "<", ">", "@", ",", ";", ":", "\\",
     107                 :          2 :                              "\"", "/", "[", "]", "?", "=", "{", "}", " ",
     108                 :          2 :                              // HT already tested in the loop below
     109                 :          2 :                              "\u007f", "", "t\rt", "t\nt", "t: t", "t:t",
     110                 :          2 :                              "t<t", "t t", " tt", ":tt", "\ttt", "\vtt", "t\0t",
     111                 :          2 :                              "t\"t", "t,t", "t;t", "()[]{}", "a?B", "a=B"]
     112                 :          2 :       var invalid_byte_strings = ["テスト", "X-テスト"]
     113                 :          2 :       for (var i = 0; i < 32; ++i) {
     114                 :          2 :         invalid_headers.push(String.fromCharCode(i))
     115                 :          2 :       }
     116                 :          2 :       for (var i = 0; i < invalid_headers.length; ++i) {
     117                 :          2 :         try_name(invalid_headers[i])
     118                 :          2 :       }
     119                 :          2 :       for (var i = 0; i < invalid_byte_strings.length; ++i) {
     120                 :          2 :         try_byte_string(invalid_byte_strings[i])
     121                 :          2 :       }
     122                 :          2 : 
     123                 :          2 :     </script>
     124                 :          2 :   </body>
     125                 :          2 : </html>
     126                 :          2 : 
     127                 :          2 :  *
     128                 :          2 :  * setrequestheader-bogus-name.htm
     129                 :          2 :  */

Generated by: LCOV version 1.14