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

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
« no previous file with comments | « LayoutTests/http/tests/serviceworker/headers.html ('k') | Source/bindings/v8/Dictionary.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 importScripts('worker-test-helpers.js');
2
3 test(function() {
4 var expectedMap = {
5 'Content-Language': 'ja',
6 'Content-Type': 'text/html; charset=UTF-8',
7 'X-ServiceWorker-Test': 'response test field'
8 };
9
10 var headers = new Headers;
11 Object.keys(expectedMap).forEach(function(key) {
12 headers.set(key, expectedMap[key]);
13 });
14
15 // 'size'
16 assert_equals(headers.size, 3, 'headers.size should match');
17
18 // 'has()', 'get()'
19 var key = 'Content-Type';
20 assert_true(headers.has(key));
21 assert_equals(headers.get(key), expectedMap[key]);
22 assert_equals(headers.get('dummy'), null);
23 assert_false(headers.has('dummy'));
24
25 // 'delete()'
26 var deleteKey = 'Content-Type';
27 headers.delete(deleteKey);
28 assert_equals(headers.size, 2, 'headers.size should have -1 size');
29 Object.keys(expectedMap).forEach(function(key) {
30 if (key == deleteKey)
31 assert_false(headers.has(key));
32 else
33 assert_true(headers.has(key));
34 });
35
36 // 'set()'
37 var testCasesForSet = [
38 // For a new key/value pair.
39 { key: 'Cache-Control',
40 value: 'max-age=3600',
41 isNewEntry: true },
42
43 // For an existing key.
44 { key: 'X-ServiceWorker-Test',
45 value: 'response test field - updated',
46 isUpdate: true },
47
48 // For setting a numeric value, expecting to see DOMString on getting.
49 { key: 'X-Numeric-Value',
50 value: 12345,
51 expectedValue: '12345',
52 isNewEntry: true },
53
54 // For case-sensitivity test. (FIXME: if we want HeaderMap to
55 // work in an case-insensitive way (as we do for headers in XHR)
56 // update the test and code)
57 { key: 'content-language',
58 value: 'fi',
59 isNewEntry: true }
60 ];
61
62 var expectedHeaderSize = headers.size;
63 testCasesForSet.forEach(function(testCase) {
64 var key = testCase.key;
65 var value = testCase.value;
66 var expectedValue = ('expectedValue' in testCase) ? testCase.expectedVal ue : testCase.value;
67 expectedHeaderSize = testCase.isNewEntry ? (expectedHeaderSize + 1) : ex pectedHeaderSize;
68
69 headers.set(key, value);
70 assert_true(headers.has(key));
71 assert_equals(headers.get(key), expectedValue);
72 if (testCase.isUpdate)
73 assert_true(headers.get(key) != expectedMap[key]);
74 assert_equals(headers.size, expectedHeaderSize);
75
76 // Update expectedMap too for forEach() test below.
77 expectedMap[key] = expectedValue;
78 });
79
80 // 'forEach()'
81 headers.forEach(function(value, key) {
82 assert_true(key != deleteKey);
83 assert_true(key in expectedMap);
84 assert_equals(headers.get(key), expectedMap[key]);
85 });
86
87 // 'append()', 'getAll()'
88 var allValues = headers.getAll('X-ServiceWorker-Test');
89 assert_equals(allValues.length, 1);
90 assert_equals(headers.size, 5);
91 headers.append('X-ServiceWorker-Test', 'response test field - append');
92 assert_equals(headers.size, 6, 'headers.size should increase by 1.');
93 assert_equals(headers.get('X-ServiceWorker-Test'),
94 'response test field - updated',
95 'the value of the first header added should be returned.');
96 allValues = headers.getAll('X-ServiceWorker-Test');
97 assert_equals(allValues.length, 2);
98 assert_equals(allValues[0], 'response test field - updated');
99 assert_equals(allValues[1], 'response test field - append');
100 headers.set('X-ServiceWorker-Test', 'response test field - set');
101 assert_equals(headers.size, 5, 'the second header should be deleted');
102 allValues = headers.getAll('X-ServiceWorker-Test');
103 assert_equals(allValues.length, 1, 'the second header should be deleted');
104 assert_equals(allValues[0], 'response test field - set');
105 headers.append('X-ServiceWorker-Test', 'response test field - append');
106 assert_equals(headers.size, 6, 'headers.size should increase by 1.')
107 headers.delete('X-ServiceWorker-Test');
108 assert_equals(headers.size, 4, 'two headers should be deleted.')
109
110 // new Headers with sequence<sequence<ByteString>>
111 headers = new Headers([['a', 'b'], ['c', 'd'], ['c', 'e']]);
112 assert_equals(headers.size, 3, 'headers.size should match');
113 assert_equals(headers.get('a'), 'b');
114 assert_equals(headers.get('c'), 'd');
115 assert_equals(headers.getAll('c')[0], 'd');
116 assert_equals(headers.getAll('c')[1], 'e');
117
118 // new Headers with Headers
119 var headers2 = new Headers(headers);
120 assert_equals(headers2.size, 3, 'headers.size should match');
121 assert_equals(headers2.get('a'), 'b');
122 assert_equals(headers2.get('c'), 'd');
123 assert_equals(headers2.getAll('c')[0], 'd');
124 assert_equals(headers2.getAll('c')[1], 'e');
125 headers.set('a', 'x');
126 assert_equals(headers.get('a'), 'x');
127 assert_equals(headers2.get('a'), 'b');
128
129 // new Headers with Dictionary
130 headers = new Headers({'a': 'b', 'c': 'd'});
131 assert_equals(headers.size, 2, 'headers.size should match');
132 assert_equals(headers.get('a'), 'b');
133 assert_equals(headers.get('c'), 'd');
134
135 // Throw errors
136 var invalidNames = ['', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
137 '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b'];
138 invalidNames.forEach(function(name) {
139 assert_throws({name:'TypeError'},
140 function() { headers.append(name, 'a'); },
141 'Headers.append with an invalid name (' + name +') should throw');
142 assert_throws({name:'TypeError'},
143 function() { headers.delete(name); },
144 'Headers.delete with an invalid name (' + name +') should throw');
145 assert_throws({name:'TypeError'},
146 function() { headers.get(name); },
147 'Headers.get with an invalid name (' + name +') should thr ow');
148 assert_throws({name:'TypeError'},
149 function() { headers.getAll(name); },
150 'Headers.getAll with an invalid name (' + name +') should throw');
151 assert_throws({name:'TypeError'},
152 function() { headers.has(name); },
153 'Headers.has with an invalid name (' + name +') should thr ow');
154 assert_throws({name:'TypeError'},
155 function() { headers.set(name, 'a'); },
156 'Headers.set with an invalid name (' + name +') should thr ow');
157 assert_throws({name:'TypeError'},
158 function() {
159 var obj = {};
160 obj[name] = 'a';
161 var headers = new Headers(obj);
162 },
163 'new Headers with an invalid name (' + name +') should thr ow');
164 assert_throws({name:'TypeError'},
165 function() { var headers = new Headers([[name, 'a']]); },
166 'new Headers with an invalid name (' + name +') should thr ow');
167 });
168
169 var invalidValues = ['test \r data', 'test \n data'];
170 invalidValues.forEach(function(value) {
171 assert_throws({name:'TypeError'},
172 function() { headers.append('a', value); },
173 'Headers.append with an invalid value should throw');
174 assert_throws({name:'TypeError'},
175 function() { headers.set('a', value); },
176 'Headers.set with an invalid value should throw');
177 assert_throws({name:'TypeError'},
178 function() { var headers = new Headers({'a': value}); },
179 'new Headers with an invalid value should throw');
180 assert_throws({name:'TypeError'},
181 function() { var headers = new Headers([['a', value]]); },
182 'new Headers with an invalid value should throw');
183 });
184
185 assert_throws({name:'TypeError'},
186 function() { var headers = new Headers([[]]); },
187 'new Headers with an invalid value should throw');
188 assert_throws({name:'TypeError'},
189 function() { var headers = new Headers([['a']]); },
190 'new Headers with an invalid value should throw');
191 assert_throws({name:'TypeError'},
192 function() { var headers = new Headers([['a', 'b'], []]); },
193 'new Headers with an invalid value should throw');
194
195 // 'forEach()' with thisArg
196 var that = {}, saw_that;
197 headers = new Headers;
198 headers.set('a', 'b');
199 headers.forEach(function() { saw_that = this; }, that);
200 assert_equals(saw_that, that, 'Passed thisArg should match');
201
202 headers.forEach(function() { 'use strict'; saw_that = this; }, 'abc');
203 assert_equals(saw_that, 'abc', 'Passed non-object thisArg should match');
204
205 headers.forEach(function() { saw_that = this; }, null);
206 assert_equals(saw_that, self, 'Passed null thisArg should be replaced with g lobal object');
207 }, 'Headers in ServiceWorkerGlobalScope');
OLDNEW
« no previous file with comments | « LayoutTests/http/tests/serviceworker/headers.html ('k') | Source/bindings/v8/Dictionary.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698