OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <title>Headers structure</title> |
| 6 <meta name="help" href="https://fetch.spec.whatwg.org/#headers"> |
| 7 <meta name="author" title="Canon Research France" href="https://www.crf.cano
n.fr"> |
| 8 <script src="/resources/testharness.js"></script> |
| 9 <script src="/resources/testharnessreport.js"></script> |
| 10 </head> |
| 11 <body> |
| 12 <script> |
| 13 test(function() { |
| 14 new Headers(); |
| 15 }, "Create headers from no parameter"); |
| 16 |
| 17 test(function() { |
| 18 new Headers(undefined); |
| 19 }, "Create headers from undefined parameter"); |
| 20 |
| 21 test(function() { |
| 22 new Headers({}); |
| 23 }, "Create headers from empty object"); |
| 24 |
| 25 var parameters = [null, 1]; |
| 26 parameters.forEach(function(parameter) { |
| 27 test(function() { |
| 28 assert_throws(new TypeError(), function() { new Headers(parameter) }); |
| 29 }, "Create headers with " + parameter + " should throw"); |
| 30 }); |
| 31 |
| 32 var headerDict = {"name1": "value1", |
| 33 "name2": "value2", |
| 34 "name3": "value3", |
| 35 "name4": null, |
| 36 "name5": undefined, |
| 37 "name6": 1, |
| 38 "Content-Type": "value4" |
| 39 }; |
| 40 |
| 41 var headerSeq = []; |
| 42 for (var name in headerDict) |
| 43 headerSeq.push([name, headerDict[name]]); |
| 44 |
| 45 test(function() { |
| 46 var headers = new Headers(headerSeq); |
| 47 for (name in headerDict) { |
| 48 assert_equals(headers.get(name), String(headerDict[name]), |
| 49 "name: " + name + " has value: " + headerDict[name]); |
| 50 } |
| 51 assert_equals(headers.get("length"), null, "init should be treated as a
sequence, not as a dictionary"); |
| 52 }, "Create headers with sequence"); |
| 53 |
| 54 test(function() { |
| 55 var headers = new Headers(headerDict); |
| 56 for (name in headerDict) { |
| 57 assert_equals(headers.get(name), String(headerDict[name]), |
| 58 "name: " + name + " has value: " + headerDict[name]); |
| 59 } |
| 60 }, "Create headers with record"); |
| 61 |
| 62 test(function() { |
| 63 var headers = new Headers(headerDict); |
| 64 var headers2 = new Headers(headers); |
| 65 for (name in headerDict) { |
| 66 assert_equals(headers2.get(name), String(headerDict[name]), |
| 67 "name: " + name + " has value: " + headerDict[name]); |
| 68 } |
| 69 }, "Create headers with existing headers"); |
| 70 |
| 71 test(function() { |
| 72 var headers = new Headers() |
| 73 headers[Symbol.iterator] = function *() { |
| 74 yield ["test", "test"] |
| 75 } |
| 76 var headers2 = new Headers(headers) |
| 77 assert_equals(headers2.get("test"), "test") |
| 78 }, "Create headers with existing headers with custom iterator"); |
| 79 |
| 80 test(function() { |
| 81 var headers = new Headers(); |
| 82 for (name in headerDict) { |
| 83 headers.append(name, headerDict[name]); |
| 84 assert_equals(headers.get(name), String(headerDict[name]), |
| 85 "name: " + name + " has value: " + headerDict[name]); |
| 86 } |
| 87 }, "Check append method"); |
| 88 |
| 89 test(function() { |
| 90 var headers = new Headers(); |
| 91 for (name in headerDict) { |
| 92 headers.set(name, headerDict[name]); |
| 93 assert_equals(headers.get(name), String(headerDict[name]), |
| 94 "name: " + name + " has value: " + headerDict[name]); |
| 95 } |
| 96 }, "Check set method"); |
| 97 |
| 98 test(function() { |
| 99 var headers = new Headers(headerDict); |
| 100 for (name in headerDict) |
| 101 assert_true(headers.has(name),"headers has name " + name); |
| 102 |
| 103 assert_false(headers.has("nameNotInHeaders"),"headers do not have header
: nameNotInHeaders"); |
| 104 }, "Check has method"); |
| 105 |
| 106 test(function() { |
| 107 var headers = new Headers(headerDict); |
| 108 for (name in headerDict) { |
| 109 assert_true(headers.has(name),"headers have a header: " + name); |
| 110 headers.delete(name) |
| 111 assert_true(!headers.has(name),"headers do not have anymore a header:
" + name); |
| 112 } |
| 113 }, "Check delete method"); |
| 114 |
| 115 test(function() { |
| 116 var headers = new Headers(headerDict); |
| 117 for (name in headerDict) |
| 118 assert_equals(headers.get(name), String(headerDict[name]), |
| 119 "name: " + name + " has value: " + headerDict[name]); |
| 120 |
| 121 assert_equals(headers.get("nameNotInHeaders"), null, "header: nameNotInH
eaders has no value"); |
| 122 }, "Check get method"); |
| 123 |
| 124 var headerEntriesDict = {"name1": "value1", |
| 125 "Name2": "value2", |
| 126 "name": "value3", |
| 127 "content-Type": "value4", |
| 128 "Content-Typ": "value5", |
| 129 "Content-Types": "value6" |
| 130 }; |
| 131 var sortedHeaderDict = {}; |
| 132 var headerValues = []; |
| 133 var sortedHeaderKeys = Object.keys(headerEntriesDict).map(function(value)
{ |
| 134 sortedHeaderDict[value.toLowerCase()] = headerEntriesDict[value]; |
| 135 headerValues.push(headerEntriesDict[value]); |
| 136 return value.toLowerCase(); |
| 137 }).sort(); |
| 138 |
| 139 var iteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Sym
bol.iterator]())); |
| 140 function checkIteratorProperties(iterator) { |
| 141 var prototype = Object.getPrototypeOf(iterator); |
| 142 assert_equals(Object.getPrototypeOf(prototype), iteratorPrototype); |
| 143 |
| 144 var descriptor = Object.getOwnPropertyDescriptor(prototype, "next"); |
| 145 assert_true(descriptor.configurable, "configurable"); |
| 146 assert_true(descriptor.enumerable, "enumerable"); |
| 147 assert_true(descriptor.writable, "writable"); |
| 148 } |
| 149 |
| 150 test(function() { |
| 151 var headers = new Headers(headerEntriesDict); |
| 152 var actual = headers.keys(); |
| 153 checkIteratorProperties(actual); |
| 154 |
| 155 sortedHeaderKeys.forEach(function(key) { |
| 156 entry = actual.next(); |
| 157 assert_false(entry.done); |
| 158 assert_equals(entry.value, key); |
| 159 }); |
| 160 assert_true(actual.next().done); |
| 161 assert_true(actual.next().done); |
| 162 |
| 163 for (key of headers.keys()) |
| 164 assert_true(sortedHeaderKeys.indexOf(key) != -1); |
| 165 }, "Check keys method"); |
| 166 |
| 167 test(function() { |
| 168 var headers = new Headers(headerEntriesDict); |
| 169 var actual = headers.values(); |
| 170 checkIteratorProperties(actual); |
| 171 |
| 172 sortedHeaderKeys.forEach(function(key) { |
| 173 entry = actual.next(); |
| 174 assert_false(entry.done); |
| 175 assert_equals(entry.value, sortedHeaderDict[key]); |
| 176 }); |
| 177 assert_true(actual.next().done); |
| 178 assert_true(actual.next().done); |
| 179 |
| 180 for (value of headers.values()) |
| 181 assert_true(headerValues.indexOf(value) != -1); |
| 182 }, "Check values method"); |
| 183 |
| 184 test(function() { |
| 185 var headers = new Headers(headerEntriesDict); |
| 186 var actual = headers.entries(); |
| 187 checkIteratorProperties(actual); |
| 188 |
| 189 sortedHeaderKeys.forEach(function(key) { |
| 190 entry = actual.next(); |
| 191 assert_false(entry.done); |
| 192 assert_equals(entry.value[0], key); |
| 193 assert_equals(entry.value[1], sortedHeaderDict[key]); |
| 194 }); |
| 195 assert_true(actual.next().done); |
| 196 assert_true(actual.next().done); |
| 197 |
| 198 for (entry of headers.entries()) |
| 199 assert_equals(entry[1], sortedHeaderDict[entry[0]]); |
| 200 }, "Check entries method"); |
| 201 |
| 202 test(function() { |
| 203 var headers = new Headers(headerEntriesDict); |
| 204 var actual = headers[Symbol.iterator](); |
| 205 |
| 206 sortedHeaderKeys.forEach(function(key) { |
| 207 entry = actual.next(); |
| 208 assert_false(entry.done); |
| 209 assert_equals(entry.value[0], key); |
| 210 assert_equals(entry.value[1], sortedHeaderDict[key]); |
| 211 }); |
| 212 assert_true(actual.next().done); |
| 213 assert_true(actual.next().done); |
| 214 }, "Check Symbol.iterator method"); |
| 215 |
| 216 test(function() { |
| 217 var headers = new Headers(headerEntriesDict); |
| 218 var reference = sortedHeaderKeys[Symbol.iterator](); |
| 219 headers.forEach(function(value, key, container) { |
| 220 assert_equals(headers, container); |
| 221 entry = reference.next(); |
| 222 assert_false(entry.done); |
| 223 assert_equals(key, entry.value); |
| 224 assert_equals(value, sortedHeaderDict[entry.value]); |
| 225 }); |
| 226 assert_true(reference.next().done); |
| 227 }, "Check forEach method"); |
| 228 </script> |
| 229 </body> |
| 230 </html> |
OLD | NEW |