test_headers_mainthread.html (4968B)
1 <!-- Any copyright is dedicated to the Public Domain. 2 - http://creativecommons.org/publicdomain/zero/1.0/ --> 3 <!DOCTYPE HTML> 4 <html> 5 <head> 6 <title>Test Fetch Headers - Basic</title> 7 <script src="/tests/SimpleTest/SimpleTest.js"></script> 8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 9 </head> 10 <body> 11 <script type="text/javascript" src="test_headers_common.js"> </script> 12 <script type="text/javascript"> 13 // Main thread specific tests because they need SpecialPowers. Expects 14 // test_headers_common.js to already be loaded. 15 16 function TestRequestHeaders() { 17 is(typeof Headers, "function", "Headers global constructor exists."); 18 var headers = new Headers(); 19 ok(headers, "Constructed empty Headers object"); 20 SpecialPowers.wrap(headers).guard = "request"; 21 TestCoreBehavior(headers, "foo"); 22 var forbidden = [ 23 "Accept-Charset", 24 "Accept-Encoding", 25 "Access-Control-Request-Headers", 26 "Access-Control-Request-Method", 27 "Connection", 28 "Content-Length", 29 "Cookie", 30 "Cookie2", 31 "Date", 32 "DNT", 33 "Expect", 34 "Host", 35 "Keep-Alive", 36 "Origin", 37 "Referer", 38 "TE", 39 "Trailer", 40 "Transfer-Encoding", 41 "Upgrade", 42 "Via", 43 "Proxy-Authorization", 44 "Proxy-blarg", 45 "Proxy-", 46 "Sec-foo", 47 "Sec-" 48 ]; 49 50 for (var i = 0, n = forbidden.length; i < n; ++i) { 51 var name = forbidden[i]; 52 headers.append(name, "hmm"); 53 checkNotHas(headers, name, "Should not be able to append " + name + " to request headers"); 54 headers.set(name, "hmm"); 55 checkNotHas(headers, name, "Should not be able to set " + name + " on request headers"); 56 } 57 } 58 59 function TestRequestNoCorsHeaders() { 60 is(typeof Headers, "function", "Headers global constructor exists."); 61 var headers = new Headers(); 62 ok(headers, "Constructed empty Headers object"); 63 SpecialPowers.wrap(headers).guard = "request-no-cors"; 64 65 headers.append("foo", "bar"); 66 checkNotHas(headers, "foo", "Should not be able to append arbitrary headers to request-no-cors headers."); 67 headers.set("foo", "bar"); 68 checkNotHas(headers, "foo", "Should not be able to set arbitrary headers on request-no-cors headers."); 69 70 var simpleNames = [ 71 "Accept", 72 "Accept-Language", 73 "Content-Language" 74 ]; 75 76 var simpleContentTypes = [ 77 "application/x-www-form-urlencoded", 78 "multipart/form-data", 79 "text/plain", 80 "application/x-www-form-urlencoded; charset=utf-8", 81 "multipart/form-data; charset=utf-8", 82 "text/plain; charset=utf-8" 83 ]; 84 85 for (var i = 0, n = simpleNames.length; i < n; ++i) { 86 var name = simpleNames[i]; 87 headers.append(name, "hmm"); 88 checkHas(headers, name, "Should be able to append " + name + " to request-no-cors headers"); 89 headers.set(name, "hmm"); 90 checkHas(headers, name, "Should be able to set " + name + " on request-no-cors headers"); 91 } 92 93 for (var i = 0, n = simpleContentTypes.length; i < n; ++i) { 94 var value = simpleContentTypes[i]; 95 headers.append("Content-Type", value); 96 checkHas(headers, "Content-Type", "Should be able to append " + value + " Content-Type to request-no-cors headers"); 97 headers.delete("Content-Type"); 98 headers.set("Content-Type", value); 99 checkHas(headers, "Content-Type", "Should be able to set " + value + " Content-Type on request-no-cors headers"); 100 } 101 } 102 103 function TestResponseHeaders() { 104 is(typeof Headers, "function", "Headers global constructor exists."); 105 var headers = new Headers(); 106 ok(headers, "Constructed empty Headers object"); 107 SpecialPowers.wrap(headers).guard = "response"; 108 TestCoreBehavior(headers, "foo"); 109 var forbidden = [ 110 "Set-Cookie", 111 "Set-Cookie2" 112 ]; 113 114 for (var i = 0, n = forbidden.length; i < n; ++i) { 115 var name = forbidden[i]; 116 headers.append(name, "hmm"); 117 checkNotHas(headers, name, "Should not be able to append " + name + " to response headers"); 118 headers.set(name, "hmm"); 119 checkNotHas(headers, name, "Should not be able to set " + name + " on response headers"); 120 } 121 } 122 123 function TestImmutableHeaders() { 124 is(typeof Headers, "function", "Headers global constructor exists."); 125 var headers = new Headers(); 126 ok(headers, "Constructed empty Headers object"); 127 TestCoreBehavior(headers, "foo"); 128 headers.append("foo", "atleastone"); 129 130 SpecialPowers.wrap(headers).guard = "immutable"; 131 132 shouldThrow(function() { 133 headers.append("foo", "wat"); 134 }, TypeError, "Should not be able to append to immutable headers"); 135 136 shouldThrow(function() { 137 headers.set("foo", "wat"); 138 }, TypeError, "Should not be able to set immutable headers"); 139 140 shouldThrow(function() { 141 headers.delete("foo"); 142 }, TypeError, "Should not be able to delete immutable headers"); 143 144 checkHas(headers, "foo", "Should be able to check immutable headers"); 145 ok(headers.get("foo"), "Should be able to get immutable headers"); 146 } 147 148 TestRequestHeaders(); 149 TestRequestNoCorsHeaders(); 150 TestResponseHeaders(); 151 TestImmutableHeaders(); 152 </script> 153 </body> 154 </html>