| OLD | NEW |
| 1 importScripts('worker-testharness.js'); | 1 importScripts('worker-testharness.js'); |
| 2 importScripts('../../resources/testharness-helpers.js'); | 2 importScripts('../../resources/testharness-helpers.js'); |
| 3 importScripts('test-helpers.js'); | 3 importScripts('test-helpers.js'); |
| 4 | 4 |
| 5 var URL = 'https://www.example.com/test.html'; | 5 var URL = 'https://www.example.com/test.html'; |
| 6 | 6 |
| 7 function size(headers) { | 7 function size(headers) { |
| 8 var count = 0; | 8 var count = 0; |
| 9 for (var header of headers) { | 9 for (var header of headers) { |
| 10 ++count; | 10 ++count; |
| 11 } | 11 } |
| 12 return count; | 12 return count; |
| 13 } | 13 } |
| 14 | 14 |
| 15 test(function() { | 15 test(function() { |
| 16 var headers = new Headers; | 16 var headers = new Headers; |
| 17 headers.set('User-Agent', 'Mozilla/5.0'); | 17 headers.set('User-Agent', 'Mozilla/5.0'); |
| 18 headers.set('Accept', 'text/html'); | 18 headers.set('Accept', 'text/html'); |
| 19 headers.set('X-ServiceWorker-Test', 'request test field'); | 19 headers.set('X-ServiceWorker-Test', 'request test field'); |
| 20 | 20 |
| 21 var request = new Request(URL, {method: 'GET', headers: headers}); | 21 var request = new Request(URL, {method: 'GET', headers: headers}); |
| 22 | 22 |
| 23 assert_equals(request.url, URL, 'Request.url should match'); | 23 assert_equals(request.url, URL, 'Request.url should match'); |
| 24 assert_equals(request.method, 'GET', 'Request.method should match'); | 24 assert_equals(request.method, 'GET', 'Request.method should match'); |
| 25 assert_equals(request.referrer, location.href, 'Request.referrer should matc
h'); | 25 assert_equals(request.referrer, location.href, |
| 26 assert_true(request.headers instanceof Headers, 'Request.headers should be H
eaders'); | 26 'Request.referrer should match'); |
| 27 assert_true(request.headers instanceof Headers, |
| 28 'Request.headers should be Headers'); |
| 27 | 29 |
| 28 // 'User-Agent' is a forbidden header. | 30 // 'User-Agent' is a forbidden header. |
| 29 assert_equals(size(request.headers), 2, 'Request.headers size should match')
; | 31 assert_equals(size(request.headers), 2, |
| 32 'Request.headers size should match'); |
| 30 // Note: detailed behavioral tests for Headers are in another test, | 33 // Note: detailed behavioral tests for Headers are in another test, |
| 31 // http/tests/serviceworker/headers.html. | 34 // http/tests/serviceworker/headers.html. |
| 32 | 35 |
| 33 request.url = 'http://localhost/'; | 36 request.url = 'http://localhost/'; |
| 34 assert_equals(request.url, 'https://www.example.com/test.html', 'Request.url
should be readonly'); | 37 assert_equals(request.url, 'https://www.example.com/test.html', |
| 35 request = new Request('http://localhost/\uD800'); // Unmatched lead surrogat
e. | 38 'Request.url should be readonly'); |
| 39 |
| 40 // Unmatched lead surrogate. |
| 41 request = new Request('http://localhost/\uD800'); |
| 36 assert_equals(request.url, | 42 assert_equals(request.url, |
| 37 'http://localhost/' + encodeURIComponent('\uFFFD'), | 43 'http://localhost/' + encodeURIComponent('\uFFFD'), |
| 38 'Request.url should have unmatched surrogates replaced.'); | 44 'Request.url should have unmatched surrogates replaced.'); |
| 45 |
| 39 request.method = 'POST'; | 46 request.method = 'POST'; |
| 40 assert_equals(request.method, 'GET', 'Request.method should be readonly'); | 47 assert_equals(request.method, 'GET', 'Request.method should be readonly'); |
| 41 }, 'Request basic test in ServiceWorkerGlobalScope'); | 48 }, 'Request basic test in ServiceWorkerGlobalScope'); |
| 42 | 49 |
| 43 test(function() { | 50 test(function() { |
| 44 [new Request(URL), | 51 [new Request(URL), |
| 45 new Request(URL, {method: ''}), | 52 new Request(URL, {method: ''}), |
| 46 new Request(URL, {mode: ''}), | 53 new Request(URL, {mode: ''}), |
| 47 new Request(URL, {mode: 'invalid mode'}), | 54 new Request(URL, {mode: 'invalid mode'}), |
| 48 new Request(URL, {credentials: ''}), | 55 new Request(URL, {credentials: ''}), |
| 49 new Request(URL, {credentials: 'invalid credentials'})].forEach(function(re
quest) { | 56 new Request(URL, {credentials: 'invalid credentials'})] |
| 50 assert_equals(request.url, URL, 'Request.url should match'); | 57 .forEach(function(request) { |
| 51 assert_equals(request.method, 'GET', 'Default Request.method should be G
ET'); | 58 assert_equals(request.url, URL, |
| 52 assert_equals(request.mode, 'cors', 'Default Request.mode should be cors
'); | 59 'Request.url should match'); |
| 53 assert_equals(request.credentials, 'omit', 'Default Request.credentials
should be omit'); | 60 assert_equals(request.method, 'GET', |
| 54 }); | 61 'Default Request.method should be GET'); |
| 55 }, 'Request default value test in ServiceWorkerGlobalScope'); | 62 assert_equals(request.mode, 'cors', |
| 63 'Default Request.mode should be cors'); |
| 64 assert_equals(request.credentials, 'omit', |
| 65 'Default Request.credentials should be omit'); |
| 66 }); |
| 67 }, 'Request default value test in ServiceWorkerGlobalScope'); |
| 56 | 68 |
| 57 test(function() { | 69 test(function() { |
| 58 var request = new Request(URL); | 70 var request = new Request(URL); |
| 59 request.headers.append('X-ServiceWorker-Foo', 'foo1'); | 71 request.headers.append('X-ServiceWorker-Foo', 'foo1'); |
| 60 request.headers.append('X-ServiceWorker-Foo', 'foo2'); | 72 request.headers.append('X-ServiceWorker-Foo', 'foo2'); |
| 61 request.headers.append('X-ServiceWorker-Bar', 'bar'); | 73 request.headers.append('X-ServiceWorker-Bar', 'bar'); |
| 62 var request2 = new Request(request); | 74 var request2 = new Request(request); |
| 63 assert_equals(request2.url, URL, 'Request.url should match'); | 75 assert_equals(request2.url, URL, 'Request.url should match'); |
| 64 assert_equals(request2.method, 'GET', 'Request.method should match'); | 76 assert_equals(request2.method, 'GET', 'Request.method should match'); |
| 65 assert_equals(request2.mode, 'cors', 'Request.mode should match'); | 77 assert_equals(request2.mode, 'cors', 'Request.mode should match'); |
| 66 assert_equals(request2.credentials, 'omit', 'Request.credentials should matc
h'); | 78 assert_equals(request2.credentials, 'omit', |
| 79 'Request.credentials should match'); |
| 67 assert_equals(request2.headers.getAll('X-ServiceWorker-Foo')[0], 'foo1', | 80 assert_equals(request2.headers.getAll('X-ServiceWorker-Foo')[0], 'foo1', |
| 68 'Request.headers should match'); | 81 'Request.headers should match'); |
| 69 assert_equals(request2.headers.getAll('X-ServiceWorker-Foo')[1], 'foo2', | 82 assert_equals(request2.headers.getAll('X-ServiceWorker-Foo')[1], 'foo2', |
| 70 'Request.headers should match'); | 83 'Request.headers should match'); |
| 71 assert_equals(request2.headers.getAll('X-ServiceWorker-Bar')[0], 'bar', | 84 assert_equals(request2.headers.getAll('X-ServiceWorker-Bar')[0], 'bar', |
| 72 'Request.headers should match'); | 85 'Request.headers should match'); |
| 73 var request3 = new Request(URL, {headers: [['X-ServiceWorker-Foo', 'foo1'], | 86 var request3 = new Request(URL, |
| 74 ['X-ServiceWorker-Foo', 'foo2'], | 87 {headers: [['X-ServiceWorker-Foo', 'foo1'], |
| 75 ['X-ServiceWorker-Bar', 'bar']]})
; | 88 ['X-ServiceWorker-Foo', 'foo2'], |
| 89 ['X-ServiceWorker-Bar', 'bar']]}); |
| 76 assert_equals(request3.headers.getAll('X-ServiceWorker-Foo')[0], 'foo1', | 90 assert_equals(request3.headers.getAll('X-ServiceWorker-Foo')[0], 'foo1', |
| 77 'Request.headers should match'); | 91 'Request.headers should match'); |
| 78 assert_equals(request3.headers.getAll('X-ServiceWorker-Foo')[1], 'foo2', | 92 assert_equals(request3.headers.getAll('X-ServiceWorker-Foo')[1], 'foo2', |
| 79 'Request.headers should match'); | 93 'Request.headers should match'); |
| 80 assert_equals(request3.headers.getAll('X-ServiceWorker-Bar')[0], 'bar', | 94 assert_equals(request3.headers.getAll('X-ServiceWorker-Bar')[0], 'bar', |
| 81 'Request.headers should match'); | 95 'Request.headers should match'); |
| 82 }, 'Request header test in ServiceWorkerGlobalScope'); | 96 }, 'Request header test in ServiceWorkerGlobalScope'); |
| 83 | 97 |
| 84 test(function() { | 98 test(function() { |
| 85 var request1 = {}; | 99 var request1 = {}; |
| 86 var request2 = {}; | 100 var request2 = {}; |
| 87 var METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS', '', undefi
ned]; | 101 var METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS', '', |
| 102 undefined]; |
| 88 var MODES = ['same-origin', 'no-cors', 'cors', '', undefined]; | 103 var MODES = ['same-origin', 'no-cors', 'cors', '', undefined]; |
| 89 function isSimpleMethod(method) { | 104 function isSimpleMethod(method) { |
| 90 return ['GET', 'HEAD', 'POST', '', undefined].indexOf(method) != -1; | 105 return ['GET', 'HEAD', 'POST', '', undefined].indexOf(method) != -1; |
| 91 }; | 106 }; |
| 92 function effectiveMethod(method1, method2) { | 107 function effectiveMethod(method1, method2) { |
| 93 return method2 ? method2 : (method1 ? method1 : 'GET'); | 108 return method2 ? method2 : (method1 ? method1 : 'GET'); |
| 94 }; | 109 }; |
| 95 function effectiveMode(mode1, mode2) { | 110 function effectiveMode(mode1, mode2) { |
| 96 return mode2 ? mode2 : (mode1 ? mode1 : 'cors'); | 111 return mode2 ? mode2 : (mode1 ? mode1 : 'cors'); |
| 97 }; | 112 }; |
| 98 METHODS.forEach(function(method1) { | 113 METHODS.forEach(function(method1) { |
| 99 MODES.forEach(function(mode1) { | 114 MODES.forEach(function(mode1) { |
| 100 var init1 = {}; | 115 var init1 = {}; |
| 101 if (method1 != undefined) { init1['method'] = method1; } | 116 if (method1 != undefined) { init1['method'] = method1; } |
| 102 if (mode1 != undefined) { init1['mode'] = mode1; } | 117 if (mode1 != undefined) { init1['mode'] = mode1; } |
| 103 if (!isSimpleMethod(method1) && mode1 == 'no-cors') { | 118 if (!isSimpleMethod(method1) && mode1 == 'no-cors') { |
| 104 assert_throws( | 119 assert_throws( |
| 105 {name:'TypeError'}, | 120 {name: 'TypeError'}, |
| 106 function() { request1 = new Request(URL, init1); }, | 121 function() { request1 = new Request(URL, init1); }, |
| 107 'new no-cors Request with non simple method (' + method1 +')
should throw'); | 122 'new no-cors Request with non simple method (' + method1 + |
| 108 return; | 123 ') should throw'); |
| 124 return; |
| 109 } | 125 } |
| 110 request1 = new Request(URL, init1); | 126 request1 = new Request(URL, init1); |
| 111 assert_equals(request1.method, method1 ? method1 : 'GET', 'Request.m
ethod should match'); | 127 assert_equals(request1.method, method1 ? method1 : 'GET', |
| 112 assert_equals(request1.mode, mode1 ? mode1 : 'cors', 'Request.mode s
hould match'); | 128 'Request.method should match'); |
| 129 assert_equals(request1.mode, mode1 ? mode1 : 'cors', |
| 130 'Request.mode should match'); |
| 113 request1 = new Request(request1); | 131 request1 = new Request(request1); |
| 114 assert_equals(request1.method, method1 ? method1 : 'GET', 'Request.m
ethod should match'); | 132 assert_equals(request1.method, method1 ? method1 : 'GET', |
| 115 assert_equals(request1.mode, mode1 ? mode1 : 'cors', 'Request.mode s
hould match'); | 133 'Request.method should match'); |
| 134 assert_equals(request1.mode, mode1 ? mode1 : 'cors', |
| 135 'Request.mode should match'); |
| 116 METHODS.forEach(function(method2) { | 136 METHODS.forEach(function(method2) { |
| 117 MODES.forEach(function(mode2) { | 137 MODES.forEach(function(mode2) { |
| 118 // We need to construct a new request1 because as soon as it | 138 // We need to construct a new request1 because as soon as it |
| 119 // is used in a constructor it will be flagged as 'used', | 139 // is used in a constructor it will be flagged as 'used', |
| 120 // and we can no longer construct objects with it. | 140 // and we can no longer construct objects with it. |
| 121 request1 = new Request(URL, init1); | 141 request1 = new Request(URL, init1); |
| 122 var init2 = {}; | 142 var init2 = {}; |
| 123 if (method2 != undefined) { init2['method'] = method2; } | 143 if (method2 != undefined) { init2['method'] = method2; } |
| 124 if (mode2 != undefined) { init2['mode'] = mode2; } | 144 if (mode2 != undefined) { init2['mode'] = mode2; } |
| 125 if (!isSimpleMethod(effectiveMethod(method1, method2)) && ef
fectiveMode(mode1, mode2) == 'no-cors') { | 145 if (!isSimpleMethod(effectiveMethod(method1, method2)) && |
| 126 assert_throws( | 146 effectiveMode(mode1, mode2) == 'no-cors') { |
| 127 {name:'TypeError'}, | 147 assert_throws( |
| 128 function() { request2 = new Request(request1, init2)
; }, | 148 {name: 'TypeError'}, |
| 129 'new no-cors Request with non simple method should t
hrow'); | 149 function() { request2 = new Request(request1, init2); }, |
| 130 return; | 150 'new no-cors Request with non simple method should ' + |
| 151 'throw'); |
| 152 return; |
| 131 } | 153 } |
| 132 request2 = new Request(request1, init2); | 154 request2 = new Request(request1, init2); |
| 133 assert_equals(request2.method, | 155 assert_equals(request2.method, |
| 134 method2 ? method2 : request1.method, | 156 method2 ? method2 : request1.method, |
| 135 'Request.method should be overridden'); | 157 'Request.method should be overridden'); |
| 136 assert_equals(request2.mode, | 158 assert_equals(request2.mode, |
| 137 mode2 ? mode2 : request1.mode, | 159 mode2 ? mode2 : request1.mode, |
| 138 'Request.mode should be overridden'); | 160 'Request.mode should be overridden'); |
| 139 }); | 161 }); |
| 140 }); | 162 }); |
| 141 }); | 163 }); |
| 142 }); | 164 }); |
| 143 }, 'Request header test in ServiceWorkerGlobalScope'); | 165 }, 'Request header test in ServiceWorkerGlobalScope'); |
| 144 | 166 |
| 145 test(function() { | 167 test(function() { |
| 146 var request1 = {}; | 168 var request1 = {}; |
| 147 var request2 = {}; | 169 var request2 = {}; |
| 148 var CREDENTIALS = ['omit', 'same-origin', 'include', '', undefined]; | 170 var CREDENTIALS = ['omit', 'same-origin', 'include', '', undefined]; |
| 149 CREDENTIALS.forEach(function(credentials1) { | 171 CREDENTIALS.forEach(function(credentials1) { |
| 150 var init1 = {}; | 172 var init1 = {}; |
| 151 if (credentials1 != undefined) { init1['credentials'] = credentials1; } | 173 if (credentials1 != undefined) { init1['credentials'] = credentials1; } |
| 152 request1 = new Request(URL, init1); | 174 request1 = new Request(URL, init1); |
| 153 assert_equals(request1.credentials, credentials1 ? credentials1 : 'omit'
, 'Request.credentials should match'); | 175 assert_equals(request1.credentials, credentials1 || 'omit', |
| 176 'Request.credentials should match'); |
| 154 request1 = new Request(request1); | 177 request1 = new Request(request1); |
| 155 assert_equals(request1.credentials, credentials1 ? credentials1 : 'omit'
, 'Request.credentials should match'); | 178 assert_equals(request1.credentials, credentials1 || 'omit', |
| 179 'Request.credentials should match'); |
| 156 CREDENTIALS.forEach(function(credentials2) { | 180 CREDENTIALS.forEach(function(credentials2) { |
| 157 request1 = new Request(URL, init1); | 181 request1 = new Request(URL, init1); |
| 158 var init2 = {}; | 182 var init2 = {}; |
| 159 if (credentials2 != undefined) { init2['credentials'] = credentials2
; } | 183 if (credentials2 != undefined) { |
| 184 init2['credentials'] = credentials2; |
| 185 } |
| 160 request2 = new Request(request1, init2); | 186 request2 = new Request(request1, init2); |
| 161 assert_equals(request2.credentials, | 187 assert_equals(request2.credentials, |
| 162 credentials2 ? credentials2 : request1.credentials, | 188 credentials2 ? credentials2 : request1.credentials, |
| 163 'Request.credentials should be overridden'); | 189 'Request.credentials should be overridden'); |
| 164 }); | 190 }); |
| 165 }); | 191 }); |
| 166 }, 'Request credentials test in ServiceWorkerGlobalScope'); | 192 }, 'Request credentials test in ServiceWorkerGlobalScope'); |
| 167 | 193 |
| 168 test(function() { | 194 test(function() { |
| 169 ['same-origin', 'cors', 'no-cors'].forEach(function(mode) { | 195 ['same-origin', 'cors', 'no-cors'].forEach(function(mode) { |
| 170 var forbiddenMethods = ['TRACE', 'TRACK', 'CONNECT']; | 196 var forbiddenMethods = ['TRACE', 'TRACK', 'CONNECT']; |
| 171 forbiddenMethods.forEach(function(method) { | 197 forbiddenMethods.forEach(function(method) { |
| 172 assert_throws( | 198 assert_throws( |
| 173 {name:'TypeError'}, | 199 {name: 'TypeError'}, |
| 174 function() { var request = new Request(URL, {mode: mode, method:
method}); }, | 200 function() { |
| 175 'new Request with a forbidden method (' + method +') should thro
w'); | 201 var request = new Request(URL, {mode: mode, method: method}); |
| 176 }); | 202 }, |
| 203 'new Request with a forbidden method (' + method + ') should ' + |
| 204 'throw'); |
| 205 }); |
| 177 var invalidNames = ['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', | 206 var invalidNames = ['(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', |
| 178 '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b', | 207 '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b', |
| 179 'invalid name']; | 208 'invalid name']; |
| 180 invalidNames.forEach(function(name) { | 209 invalidNames.forEach(function(name) { |
| 181 assert_throws( | 210 assert_throws( |
| 182 {name:'TypeError'}, | 211 {name: 'TypeError'}, |
| 183 function() { var request = new Request(URL, {mode: mode, method:
name}); }, | 212 function() { |
| 184 'new Request with an invalid method (' + name +') should throw')
; | 213 var request = new Request(URL, {mode: mode, method: name}); |
| 185 }); | 214 }, |
| 186 }); | 215 'new Request with an invalid method (' + name + ') should throw'); |
| 187 }, 'Request method name test in ServiceWorkerGlobalScope'); | 216 }); |
| 217 }); |
| 218 }, 'Request method name test in ServiceWorkerGlobalScope'); |
| 188 | 219 |
| 189 test(function() { | 220 test(function() { |
| 190 var FORBIDDEN_HEADERS = | 221 var FORBIDDEN_HEADERS = |
| 191 ['Accept-Charset', 'Accept-Encoding', 'Access-Control-Request-Headers', | 222 ['Accept-Charset', 'Accept-Encoding', 'Access-Control-Request-Headers', |
| 192 'Access-Control-Request-Method', 'Connection', 'Content-Length', 'Cooki
e', | 223 'Access-Control-Request-Method', 'Connection', 'Content-Length', |
| 193 'Cookie2', 'Date', 'DNT', 'Expect', 'Host', 'Keep-Alive', 'Origin', | 224 'Cookie', 'Cookie2', 'Date', 'DNT', 'Expect', 'Host', 'Keep-Alive', |
| 194 'Referer', 'TE', 'Trailer', 'Transfer-Encoding', 'Upgrade', 'User-Agent
', | 225 'Origin', 'Referer', 'TE', 'Trailer', 'Transfer-Encoding', 'Upgrade', |
| 195 'Via', 'Proxy-', 'Sec-', 'Proxy-FooBar', 'Sec-FooBar']; | 226 'User-Agent', 'Via', 'Proxy-', 'Sec-', 'Proxy-FooBar', 'Sec-FooBar']; |
| 196 var SIMPLE_HEADERS = | 227 var SIMPLE_HEADERS = |
| 197 [['Accept', '*'], ['Accept-Language', 'ru'], ['Content-Language', 'ru'], | 228 [['Accept', '*'], ['Accept-Language', 'ru'], ['Content-Language', 'ru'], |
| 198 ['Content-Type', 'application/x-www-form-urlencoded'], | 229 ['Content-Type', 'application/x-www-form-urlencoded'], |
| 199 ['Content-Type', 'multipart/form-data'], | 230 ['Content-Type', 'multipart/form-data'], |
| 200 ['Content-Type', 'text/plain']]; | 231 ['Content-Type', 'text/plain']]; |
| 201 var NON_SIMPLE_HEADERS = | 232 var NON_SIMPLE_HEADERS = |
| 202 [['X-ServiceWorker-Test', 'test'], | 233 [['X-ServiceWorker-Test', 'test'], |
| 203 ['X-ServiceWorker-Test2', 'test2'], | 234 ['X-ServiceWorker-Test2', 'test2'], |
| 204 ['Content-Type', 'foo/bar']]; | 235 ['Content-Type', 'foo/bar']]; |
| 205 | 236 |
| 206 ['same-origin', 'cors'].forEach(function(mode) { | 237 ['same-origin', 'cors'].forEach(function(mode) { |
| 207 var request = new Request(URL, {mode: mode}); | 238 var request = new Request(URL, {mode: mode}); |
| 208 FORBIDDEN_HEADERS.forEach(function(header) { | 239 FORBIDDEN_HEADERS.forEach(function(header) { |
| 209 request.headers.append(header, 'test'); | 240 request.headers.append(header, 'test'); |
| 210 assert_equals(size(request.headers), 0, | 241 assert_equals(size(request.headers), 0, |
| 211 'Request.headers.append should ignore the forbidden he
aders'); | 242 'Request.headers.append should ignore the ' + |
| 243 'forbidden headers'); |
| 212 request.headers.set(header, 'test'); | 244 request.headers.set(header, 'test'); |
| 213 assert_equals(size(request.headers), 0, | 245 assert_equals(size(request.headers), 0, |
| 214 'Request.headers.set should ignore the forbidden heade
rs'); | 246 'Request.headers.set should ignore the forbidden ' + |
| 215 }); | 247 'headers'); |
| 248 }); |
| 216 var request = new Request(URL, {mode: mode}); | 249 var request = new Request(URL, {mode: mode}); |
| 217 assert_equals(size(request.headers), 0); | 250 assert_equals(size(request.headers), 0); |
| 218 NON_SIMPLE_HEADERS.forEach(function(header) { | 251 NON_SIMPLE_HEADERS.forEach(function(header) { |
| 219 request.headers.append(header[0], header[1]); | 252 request.headers.append(header[0], header[1]); |
| 220 }); | 253 }); |
| 221 assert_equals(size(request.headers), NON_SIMPLE_HEADERS.length); | 254 assert_equals(size(request.headers), NON_SIMPLE_HEADERS.length); |
| 222 NON_SIMPLE_HEADERS.forEach(function(header) { | 255 NON_SIMPLE_HEADERS.forEach(function(header) { |
| 223 assert_equals(request.headers.get(header[0]), header[1]); | 256 assert_equals(request.headers.get(header[0]), header[1]); |
| 224 }); | 257 }); |
| 225 request = new Request(URL, {mode: mode}); | 258 request = new Request(URL, {mode: mode}); |
| 226 assert_equals(size(request.headers), 0); | 259 assert_equals(size(request.headers), 0); |
| 227 NON_SIMPLE_HEADERS.forEach(function(header) { | 260 NON_SIMPLE_HEADERS.forEach(function(header) { |
| 228 request.headers.set(header[0], header[1]); | 261 request.headers.set(header[0], header[1]); |
| 229 }); | 262 }); |
| 230 assert_equals(size(request.headers), NON_SIMPLE_HEADERS.length); | 263 assert_equals(size(request.headers), NON_SIMPLE_HEADERS.length); |
| 231 NON_SIMPLE_HEADERS.forEach(function(header) { | 264 NON_SIMPLE_HEADERS.forEach(function(header) { |
| 232 assert_equals(request.headers.get(header[0]), header[1]); | 265 assert_equals(request.headers.get(header[0]), header[1]); |
| 233 }); | 266 }); |
| 234 }); | 267 }); |
| 235 request = new Request(URL, {mode: 'no-cors'}); | 268 request = new Request(URL, {mode: 'no-cors'}); |
| 236 FORBIDDEN_HEADERS.forEach(function(header) { | 269 FORBIDDEN_HEADERS.forEach(function(header) { |
| 237 request.headers.set(header, 'test'); | 270 request.headers.set(header, 'test'); |
| 238 request.headers.append(header, 'test'); | 271 request.headers.append(header, 'test'); |
| 239 }); | 272 }); |
| 240 NON_SIMPLE_HEADERS.forEach(function(header) { | 273 NON_SIMPLE_HEADERS.forEach(function(header) { |
| 241 request.headers.set(header[0], header[1]); | 274 request.headers.set(header[0], header[1]); |
| 242 request.headers.append(header[0], header[1]); | 275 request.headers.append(header[0], header[1]); |
| 243 }); | 276 }); |
| 244 assert_equals(size(request.headers), 0, | 277 assert_equals(size(request.headers), 0, |
| 245 'no-cors request should only accept simple headers'); | 278 'no-cors request should only accept simple headers'); |
| 246 | 279 |
| 247 SIMPLE_HEADERS.forEach(function(header) { | 280 SIMPLE_HEADERS.forEach(function(header) { |
| 248 request = new Request(URL, {mode: 'no-cors'}); | 281 request = new Request(URL, {mode: 'no-cors'}); |
| 249 request.headers.append(header[0], header[1]); | 282 request.headers.append(header[0], header[1]); |
| 250 assert_equals(size(request.headers), 1, | 283 assert_equals(size(request.headers), 1, |
| 251 'no-cors request should accept simple headers'); | 284 'no-cors request should accept simple headers'); |
| 252 request = new Request(URL, {mode: 'no-cors'}); | 285 request = new Request(URL, {mode: 'no-cors'}); |
| 253 request.headers.set(header[0], header[1]); | 286 request.headers.set(header[0], header[1]); |
| 254 assert_equals(size(request.headers), 1, | 287 assert_equals(size(request.headers), 1, |
| 255 'no-cors request should accept simple headers'); | 288 'no-cors request should accept simple headers'); |
| 256 request.headers.delete(header[0]); | 289 request.headers.delete(header[0]); |
| 257 if (header[0] == 'Content-Type') { | 290 if (header[0] == 'Content-Type') { |
| 258 assert_equals( | 291 assert_equals( |
| 259 size(request.headers), 1, | 292 size(request.headers), 1, |
| 260 'Content-Type header of no-cors request shouldn\'t be deleted'); | 293 'Content-Type header of no-cors request shouldn\'t be deleted'); |
| 261 } else { | 294 } else { |
| 262 assert_equals(size(request.headers), 0); | 295 assert_equals(size(request.headers), 0); |
| 263 } | 296 } |
| 264 }); | 297 }); |
| 265 | 298 |
| 266 SIMPLE_HEADERS.forEach(function(header) { | 299 SIMPLE_HEADERS.forEach(function(header) { |
| 267 var headers = {}; | 300 var headers = {}; |
| 268 NON_SIMPLE_HEADERS.forEach(function(header2) { | 301 NON_SIMPLE_HEADERS.forEach(function(header2) { |
| 269 headers[header2[0]] = header2[1]; | 302 headers[header2[0]] = header2[1]; |
| 270 }); | 303 }); |
| 271 FORBIDDEN_HEADERS.forEach(function(header) { headers[header] = 'foo'; })
; | 304 FORBIDDEN_HEADERS.forEach(function(header) { |
| 305 headers[header] = 'foo'; |
| 306 }); |
| 272 headers[header[0]] = header[1]; | 307 headers[header[0]] = header[1]; |
| 273 var expectedSize = NON_SIMPLE_HEADERS.length; | 308 var expectedSize = NON_SIMPLE_HEADERS.length; |
| 274 if (header[0] != 'Content-Type') { | 309 if (header[0] != 'Content-Type') { |
| 275 ++expectedSize; | 310 ++expectedSize; |
| 276 } | 311 } |
| 277 ['same-origin', 'cors'].forEach(function(mode) { | 312 ['same-origin', 'cors'].forEach(function(mode) { |
| 278 request = new Request(URL, {mode: mode, headers: headers}); | 313 request = new Request(URL, {mode: mode, headers: headers}); |
| 279 assert_equals(size(request.headers), expectedSize, | 314 assert_equals(size(request.headers), expectedSize, |
| 280 'Request should not support the forbidden headers'); | 315 'Request should not support the forbidden headers'); |
| 281 }); | 316 }); |
| 282 request = new Request(URL, {mode: 'no-cors', headers: headers}); | 317 request = new Request(URL, {mode: 'no-cors', headers: headers}); |
| 283 assert_equals(size(request.headers), 1, | 318 assert_equals(size(request.headers), 1, |
| 284 'No-CORS Request.headers should only support simple header
s'); | 319 'No-CORS Request.headers should only support simple ' + |
| 320 'headers'); |
| 285 ['same-origin', 'cors', 'no-cors'].forEach(function(mode) { | 321 ['same-origin', 'cors', 'no-cors'].forEach(function(mode) { |
| 286 request = new Request(new Request(URL, {mode: mode, headers: headers
}), {mode: 'no-cors'}); | 322 request = new Request( |
| 323 new Request(URL, {mode: mode, headers: headers}), |
| 324 {mode: 'no-cors'}); |
| 287 assert_equals(size(request.headers), 1, | 325 assert_equals(size(request.headers), 1, |
| 288 'No-CORS Request.headers should only support simple he
aders'); | 326 'No-CORS Request.headers should only support ' + |
| 289 }); | 327 'simple headers'); |
| 290 }); | 328 }); |
| 291 }, 'Request headers test in ServiceWorkerGlobalScope'); | 329 }); |
| 330 }, 'Request headers test in ServiceWorkerGlobalScope'); |
| 292 | 331 |
| 293 test(function() { | 332 test(function() { |
| 294 var url = 'http://example.com'; | 333 var url = 'http://example.com'; |
| 295 ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'].forEach(function(method) { | 334 ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'].forEach( |
| 296 assert_equals(new Request(url, {method: method.toLowerCase()}).method, | 335 function(method) { |
| 297 method, | 336 assert_equals(new Request(url, {method: method.toLowerCase()}).method, |
| 298 'method should be normalized to uppercase: ' + method); | 337 method, |
| 299 }); | 338 'method should be normalized to uppercase: ' + method); |
| 339 }); |
| 300 | 340 |
| 301 ['PATCH', 'MKCOL', 'CUSTOM', 'X-FILES'].forEach(function(method) { | 341 ['PATCH', 'MKCOL', 'CUSTOM', 'X-FILES'].forEach( |
| 302 assert_equals(new Request(url, {method: method}).method, method, | 342 function(method) { |
| 303 'method should not be changed when normalized: ' + method); | 343 assert_equals(new Request(url, {method: method}).method, method, |
| 304 method = method.toLowerCase(); | 344 'method should not be changed when normalized: ' + |
| 305 assert_equals(new Request(url, {method: method}).method, method, | 345 method); |
| 306 'method should not be changed when normalized: ' + method); | 346 method = method.toLowerCase(); |
| 307 }); | 347 assert_equals(new Request(url, {method: method}).method, method, |
| 308 }, 'Request method names are normalized'); | 348 'method should not be changed when normalized: ' + |
| 349 method); |
| 350 }); |
| 351 }, 'Request method names are normalized'); |
| 309 | 352 |
| 310 test(function() { | 353 test(function() { |
| 311 var req = new Request(URL); | 354 var req = new Request(URL); |
| 312 assert_false(req.bodyUsed, | 355 assert_false(req.bodyUsed, |
| 313 "Request should not be flagged as used if it has not been consumed."); | 356 'Request should not be flagged as used if it has not been ' + |
| 357 'consumed.'); |
| 314 var req2 = new Request(req); | 358 var req2 = new Request(req); |
| 315 assert_true(req.bodyUsed, | 359 assert_true(req.bodyUsed, |
| 316 "Request should be flagged as used if it is used as a construction " + | 360 'Request should be flagged as used if it is used as a ' + |
| 317 "argument of another Request."); | 361 'construction argument of another Request.'); |
| 318 assert_false(req2.bodyUsed, | 362 assert_false(req2.bodyUsed, |
| 319 "Request should not be flagged as used if it has not been consumed."); | 363 'Request should not be flagged as used if it has not been ' + |
| 364 'consumed.'); |
| 320 assert_throws(new TypeError(), function() { new Request(req); }, | 365 assert_throws(new TypeError(), function() { new Request(req); }, |
| 321 "Request cannot be constructed with a request that has been flagged as use
d."); | 366 'Request cannot be constructed with a request that has ' + |
| 322 }, 'Request construction behavior regarding "used" body flag and exceptions.')
; | 367 'been flagged as used.'); |
| 368 }, |
| 369 'Request construction behavior regarding "used" body flag and exceptions.'); |
| 323 | 370 |
| 324 promise_test(function() { | 371 promise_test(function() { |
| 325 var headers = new Headers; | 372 var headers = new Headers; |
| 326 headers.set('Content-Language', 'ja'); | 373 headers.set('Content-Language', 'ja'); |
| 327 var req = new Request(URL, { | 374 var req = new Request(URL, { |
| 328 method: 'GET', | 375 method: 'GET', |
| 329 headers: headers, | 376 headers: headers, |
| 330 body: new Blob(['Test Blob'], {type: 'test/type'}) | 377 body: new Blob(['Test Blob'], {type: 'test/type'}) |
| 331 }); | 378 }); |
| 332 var req2 = req.clone(); | 379 var req2 = req.clone(); |
| 333 // Change headers and of original request. | 380 // Change headers and of original request. |
| 334 req.headers.set('Content-Language', 'en'); | 381 req.headers.set('Content-Language', 'en'); |
| 335 assert_equals( | 382 assert_equals(req2.headers.get('Content-Language'), 'ja', |
| 336 req2.headers.get('Content-Language'), 'ja', 'Headers of cloned request ' + | 383 'Headers of cloned request should not change when ' + |
| 337 'should not change when original request headers are changed.'); | 384 'original request headers are changed.'); |
| 338 | 385 |
| 339 return req.text() | 386 return req.text() |
| 340 .then(function(text) { | 387 .then(function(text) { |
| 341 assert_equals(text, 'Test Blob', 'Body of request should match.'); | 388 assert_equals(text, 'Test Blob', 'Body of request should match.'); |
| 342 return req2.text(); | 389 return req2.text(); |
| 343 }) | 390 }) |
| 344 .then(function(text) { | 391 .then(function(text) { |
| 345 assert_equals(text, 'Test Blob', 'Cloned request body should match.'); | 392 assert_equals(text, 'Test Blob', 'Cloned request body should match.'); |
| 346 }); | 393 }); |
| 347 }, 'Test clone behavior with loading content from Request.'); | 394 }, 'Test clone behavior with loading content from Request.'); |
| 348 | 395 |
| 349 async_test(function(t) { | 396 async_test(function(t) { |
| 350 var getContentType = function(headers) { | 397 var getContentType = function(headers) { |
| 351 var content_type = ''; | 398 var content_type = ''; |
| 352 for (var header of headers) { | 399 for (var header of headers) { |
| 353 if (header[0] == 'content-type') | 400 if (header[0] == 'content-type') |
| 354 content_type = header[1]; | 401 content_type = header[1]; |
| 355 } | 402 } |
| 356 return content_type; | 403 return content_type; |
| 357 }; | 404 }; |
| 358 var request = | 405 var request = |
| 359 new Request(URL, | 406 new Request(URL, |
| 360 { | 407 { |
| 361 method: 'POST', | 408 method: 'POST', |
| 362 body: new Blob(['Test Blob'], {type: 'test/type'}) | 409 body: new Blob(['Test Blob'], {type: 'test/type'}) |
| 363 }); | 410 }); |
| 364 assert_equals( | 411 assert_equals( |
| 365 getContentType(request.headers), 'test/type', | 412 getContentType(request.headers), 'test/type', |
| 366 'ContentType header of Request created with Blob body must be set.'); | 413 'ContentType header of Request created with Blob body must be set.'); |
| 367 assert_false(request.bodyUsed, | 414 assert_false(request.bodyUsed, |
| 368 'bodyUsed must be true before calling text()'); | 415 'bodyUsed must be true before calling text()'); |
| 369 request.text() | 416 request.text() |
| 370 .then(function(result) { | 417 .then(function(result) { |
| 371 assert_equals(result, 'Test Blob', | 418 assert_equals(result, 'Test Blob', |
| 372 'Creating a Request with Blob body should success.'); | 419 'Creating a Request with Blob body should success.'); |
| 373 | 420 |
| 374 request = new Request(URL, {method: 'POST', body: 'Test String'}); | 421 request = new Request(URL, {method: 'POST', body: 'Test String'}); |
| 375 assert_equals( | 422 assert_equals( |
| 376 getContentType(request.headers), 'text/plain;charset=UTF-8', | 423 getContentType(request.headers), 'text/plain;charset=UTF-8', |
| 377 'ContentType header of Request created with string must be set.'); | 424 'ContentType header of Request created with string must be set.'); |
| 378 return request.text(); | 425 return request.text(); |
| 379 }) | 426 }) |
| 380 .then(function(result) { | 427 .then(function(result) { |
| 381 assert_equals(result, 'Test String', | 428 assert_equals(result, 'Test String', |
| 382 'Creating a Request with string body should success.'); | 429 'Creating a Request with string body should success.'); |
| 383 | 430 |
| 384 var text = "Test ArrayBuffer"; | 431 var text = 'Test ArrayBuffer'; |
| 385 var array = new Uint8Array(text.length); | 432 var array = new Uint8Array(text.length); |
| 386 for (var i = 0; i < text.length; ++i) | 433 for (var i = 0; i < text.length; ++i) |
| 387 array[i] = text.charCodeAt(i); | 434 array[i] = text.charCodeAt(i); |
| 388 request = new Request(URL, {method: 'POST', body: array.buffer}); | 435 request = new Request(URL, {method: 'POST', body: array.buffer}); |
| 389 return request.text(); | 436 return request.text(); |
| 390 }) | 437 }) |
| 391 .then(function(result) { | 438 .then(function(result) { |
| 392 assert_equals( | 439 assert_equals( |
| 393 result, 'Test ArrayBuffer', | 440 result, 'Test ArrayBuffer', |
| 394 'Creating a Request with ArrayBuffer body should success.'); | 441 'Creating a Request with ArrayBuffer body should success.'); |
| 395 | 442 |
| 396 var text = "Test ArrayBufferView"; | 443 var text = 'Test ArrayBufferView'; |
| 397 var array = new Uint8Array(text.length); | 444 var array = new Uint8Array(text.length); |
| 398 for (var i = 0; i < text.length; ++i) | 445 for (var i = 0; i < text.length; ++i) |
| 399 array[i] = text.charCodeAt(i); | 446 array[i] = text.charCodeAt(i); |
| 400 request = new Request(URL, {method: 'POST', body: array}); | 447 request = new Request(URL, {method: 'POST', body: array}); |
| 401 return request.text(); | 448 return request.text(); |
| 402 }) | 449 }) |
| 403 .then(function(result) { | 450 .then(function(result) { |
| 404 assert_equals( | 451 assert_equals( |
| 405 result, 'Test ArrayBufferView', | 452 result, 'Test ArrayBufferView', |
| 406 'Creating a Request with ArrayBuffer body should success.'); | 453 'Creating a Request with ArrayBuffer body should success.'); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 result, expected_body, | 486 result, expected_body, |
| 440 'Creating a Request with FormData body should success.'); | 487 'Creating a Request with FormData body should success.'); |
| 441 }) | 488 }) |
| 442 .then(function() { | 489 .then(function() { |
| 443 t.done(); | 490 t.done(); |
| 444 }) | 491 }) |
| 445 .catch(unreached_rejection(t)); | 492 .catch(unreached_rejection(t)); |
| 446 assert_true(request.bodyUsed, | 493 assert_true(request.bodyUsed, |
| 447 'bodyUsed must be true after calling text()'); | 494 'bodyUsed must be true after calling text()'); |
| 448 }, 'Request body test in ServiceWorkerGlobalScope'); | 495 }, 'Request body test in ServiceWorkerGlobalScope'); |
| OLD | NEW |