Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: LayoutTests/http/tests/serviceworker/resources/headers-worker.js

Issue 358573002: [ServiceWorker] Implement Headers class. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebase Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 importScripts('worker-test-helpers.js'); 1 importScripts('worker-test-helpers.js');
2 2
3 test(function() { 3 test(function() {
4 var expectedMap = { 4 var expectedMap = {
5 'Content-Language': 'ja', 5 'Content-Language': 'ja',
6 'Content-Type': 'text/html; charset=UTF-8', 6 'Content-Type': 'text/html; charset=UTF-8',
7 'X-ServiceWorker-Test': 'response test field' 7 'X-ServiceWorker-Test': 'response test field'
8 }; 8 };
9 9
10 var headers = new HeaderMap; 10 var headers = new Headers;
11 Object.keys(expectedMap).forEach(function(key) { 11 Object.keys(expectedMap).forEach(function(key) {
12 headers.set(key, expectedMap[key]); 12 headers.set(key, expectedMap[key]);
13 }); 13 });
14 14
15 // 'size' 15 // 'size'
16 assert_equals(headers.size, 3, 'headers.size should match'); 16 assert_equals(headers.size, 3, 'headers.size should match');
17 17
18 // 'has()', 'get()' 18 // 'has()', 'get()'
19 var key = 'Content-Type'; 19 var key = 'Content-Type';
20 assert_true(headers.has(key)); 20 assert_true(headers.has(key));
21 assert_equals(headers.get(key), expectedMap[key]); 21 assert_equals(headers.get(key), expectedMap[key]);
22 assert_equals(headers.get('dummy'), null);
22 23
23 // 'delete()' 24 // 'delete()'
24 var deleteKey = 'Content-Type'; 25 var deleteKey = 'Content-Type';
25 headers.delete(deleteKey); 26 headers.delete(deleteKey);
26 assert_equals(headers.size, 2, 'headers.size should have -1 size'); 27 assert_equals(headers.size, 2, 'headers.size should have -1 size');
27 Object.keys(expectedMap).forEach(function(key) { 28 Object.keys(expectedMap).forEach(function(key) {
28 if (key == deleteKey) 29 if (key == deleteKey)
29 assert_false(headers.has(key)); 30 assert_false(headers.has(key));
30 else 31 else
31 assert_true(headers.has(key)); 32 assert_true(headers.has(key));
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 expectedMap[key] = expectedValue; 76 expectedMap[key] = expectedValue;
76 }); 77 });
77 78
78 // 'forEach()' 79 // 'forEach()'
79 headers.forEach(function(value, key) { 80 headers.forEach(function(value, key) {
80 assert_true(key != deleteKey); 81 assert_true(key != deleteKey);
81 assert_true(key in expectedMap); 82 assert_true(key in expectedMap);
82 assert_equals(headers.get(key), expectedMap[key]); 83 assert_equals(headers.get(key), expectedMap[key]);
83 }); 84 });
84 85
86 // 'append()', 'getAll()'
87 var allValues = headers.getAll('X-ServiceWorker-Test');
88 assert_equals(allValues.length, 1);
89 assert_equals(headers.size, 5);
90 headers.append('X-ServiceWorker-Test', 'response test field - append');
91 assert_equals(headers.size, 6);
falken 2014/06/27 02:08:06 can you add a description indicating the size shou
horo 2014/06/27 04:19:10 Done.
92 allValues = headers.getAll('X-ServiceWorker-Test');
93 assert_equals(headers.get('X-ServiceWorker-Test'), 'response test field - up dated');
falken 2014/06/27 02:08:05 nit: seems to read better if line 92 and 93 are sw
falken 2014/06/27 02:08:06 add a description that the value of the first head
horo 2014/06/27 04:19:10 Done.
horo 2014/06/27 04:19:10 Done.
94 assert_equals(allValues.length, 2);
95 assert_equals(allValues[0], 'response test field - updated');
96 assert_equals(allValues[1], 'response test field - append');
97 headers.set('X-ServiceWorker-Test', 'response test field - set');
98 assert_equals(headers.size, 5);
falken 2014/06/27 02:08:05 seems a description would be useful here, and for
horo 2014/06/27 04:19:10 Done.
99 allValues = headers.getAll('X-ServiceWorker-Test');
100 assert_equals(allValues.length, 1);
101 assert_equals(allValues[0], 'response test field - set');
102 headers.append('X-ServiceWorker-Test', 'response test field - append');
103 assert_equals(headers.size, 6);
104 headers.delete('X-ServiceWorker-Test');
105 assert_equals(headers.size, 4);
106
107 // new Headers with sequence<sequence<ByteString>>
108 headers = new Headers([['a', 'b'], ['c', 'd'], ['c', 'e']]);
109 assert_equals(headers.size, 3, 'headers.size should match');
110 assert_equals(headers.get('a'), 'b');
111 assert_equals(headers.get('c'), 'd');
112 assert_equals(headers.getAll('c')[0], 'd');
113 assert_equals(headers.getAll('c')[1], 'e');
114
115 // new Headers with Headers
116 var headers2 = new Headers(headers);
117 assert_equals(headers2.size, 3, 'headers.size should match');
118 assert_equals(headers2.get('a'), 'b');
119 assert_equals(headers2.get('c'), 'd');
120 assert_equals(headers2.getAll('c')[0], 'd');
121 assert_equals(headers2.getAll('c')[1], 'e');
122 headers.set('a', 'x');
123 assert_equals(headers.get('a'), 'x');
124 assert_equals(headers2.get('a'), 'b');
125
126 // new Headers with Dictionary
127 headers = new Headers({'a': 'b', 'c': 'd'});
128 assert_equals(headers.size, 2, 'headers.size should match');
129 assert_equals(headers.get('a'), 'b');
130 assert_equals(headers.get('c'), 'd');
131
132 // Throw errors
133 assert_throws({name:'TypeError'}, function() { headers.append('', 'a'); },
134 'Headers.append with an invalid name should throw');
falken 2014/06/27 02:08:05 are there any invalid names other than ''? could b
horo 2014/06/27 04:19:10 Done.
135 assert_throws({name:'TypeError'}, function() { headers.delete(''); },
136 'Headers.delete with an invalid name should throw');
137 assert_throws({name:'TypeError'}, function() { headers.get(''); },
138 'Headers.get with an invalid name should throw');
139 assert_throws({name:'TypeError'}, function() { headers.getAll(''); },
140 'Headers.getAll with an invalid name should throw');
141 assert_throws({name:'TypeError'}, function() { headers.has(''); },
142 'Headers.has with an invalid name should throw');
143 assert_throws({name:'TypeError'}, function() { headers.set('', 'a'); },
144 'Headers.set with an invalid name should throw');
145
146 assert_throws({name:'TypeError'},
147 function() { headers.append('a', 'test \r data'); },
148 'Headers.append with an invalid value should throw');
149 assert_throws({name:'TypeError'},
150 function() { headers.append('a', 'test \n data'); },
151 'Headers.append with an invalid value should throw');
152 assert_throws({name:'TypeError'},
153 function() { headers.set('a', 'test \r data'); },
154 'Headers.set with an invalid value should throw');
155 assert_throws({name:'TypeError'},
156 function() { headers.set('a', 'test \n data'); },
157 'Headers.set with an invalid value should throw');
158
159 assert_throws({name:'TypeError'},
160 function() { var headers = new Headers({'': 'a'}); },
161 'new Headers with an invalid name should throw');
162 assert_throws({name:'TypeError'},
163 function() { var headers = new Headers([['', 'a']]); },
164 'new Headers with an invalid name should throw');
165 assert_throws({name:'TypeError'},
166 function() { var headers = new Headers({'a': 'a\rb'}); },
167 'new Headers with an invalid value should throw');
168 assert_throws({name:'TypeError'},
169 function() { var headers = new Headers({'a': 'a\nb'}); },
170 'new Headers with an invalid value should throw');
171 assert_throws({name:'TypeError'},
172 function() { var headers = new Headers([['a', 'a\rb']]); },
173 'new Headers with an invalid value should throw');
174 assert_throws({name:'TypeError'},
175 function() { var headers = new Headers([['a', 'a\nb']]); },
176 'new Headers with an invalid value should throw');
177 assert_throws({name:'TypeError'},
178 function() { var headers = new Headers([[]]); },
179 'new Headers with an invalid value should throw');
180 assert_throws({name:'TypeError'},
181 function() { var headers = new Headers([['a']]); },
182 'new Headers with an invalid value should throw');
183 assert_throws({name:'TypeError'},
184 function() { var headers = new Headers([['a', 'b'], []]); },
185 'new Headers with an invalid value should throw');
186
85 // 'forEach()' with thisArg 187 // 'forEach()' with thisArg
86 var that = {}, saw_that; 188 var that = {}, saw_that;
87 headers = new HeaderMap; 189 headers = new Headers;
88 headers.set('a', 'b'); 190 headers.set('a', 'b');
89 headers.forEach(function() { saw_that = this; }, that); 191 headers.forEach(function() { saw_that = this; }, that);
90 assert_equals(saw_that, that, 'Passed thisArg should match'); 192 assert_equals(saw_that, that, 'Passed thisArg should match');
91 193
92 headers.forEach(function() { 'use strict'; saw_that = this; }, 'abc'); 194 headers.forEach(function() { 'use strict'; saw_that = this; }, 'abc');
93 assert_equals(saw_that, 'abc', 'Passed non-object thisArg should match'); 195 assert_equals(saw_that, 'abc', 'Passed non-object thisArg should match');
94 196
95 headers.forEach(function() { saw_that = this; }, null); 197 headers.forEach(function() { saw_that = this; }, null);
96 assert_equals(saw_that, self, 'Passed null thisArg should be replaced with g lobal object'); 198 assert_equals(saw_that, self, 'Passed null thisArg should be replaced with g lobal object');
97 199
98 }, 'HeaderMap in ServiceWorkerGlobalScope'); 200 }, 'Headers in ServiceWorkerGlobalScope');
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698