OLD | NEW |
1 importScripts('worker-testharness.js'); | 1 importScripts('worker-testharness.js'); |
2 | 2 |
3 function size(headers) { | 3 function size(headers) { |
4 var count = 0; | 4 var count = 0; |
5 for (var header of headers) { | 5 for (var header of headers) { |
6 ++count; | 6 ++count; |
7 } | 7 } |
8 return count; | 8 return count; |
9 } | 9 } |
10 | 10 |
11 test(function() { | 11 test(function() { |
12 var response = new Response(new Blob()); | 12 var response = new Response(new Blob()); |
13 assert_equals(response.type, 'default', 'Default Response.type should be \'d
efault\''); | 13 assert_equals(response.type, 'default', |
| 14 'Default Response.type should be \'default\''); |
14 assert_equals(response.url, '', 'Response.url should be the empty string'); | 15 assert_equals(response.url, '', 'Response.url should be the empty string'); |
15 assert_equals(response.status, 200, 'Default Response.status should be 200')
; | 16 assert_equals(response.status, 200, |
16 assert_equals(response.statusText, 'OK', 'Default Response.statusText should
be \'OK\''); | 17 'Default Response.status should be 200'); |
17 assert_equals(size(response.headers), 0, 'Default Response should not have a
ny header.'); | 18 assert_equals(response.statusText, 'OK', |
| 19 'Default Response.statusText should be \'OK\''); |
| 20 assert_equals(size(response.headers), 0, |
| 21 'Default Response should not have any header.'); |
18 | 22 |
19 response.status = 394; | 23 response.status = 394; |
20 response.statusText = 'Sesame Street'; | 24 response.statusText = 'Sesame Street'; |
21 assert_equals(response.status, 200, 'Response.status should be readonly'); | 25 assert_equals(response.status, 200, 'Response.status should be readonly'); |
22 assert_equals(response.statusText, 'OK', 'Response.statusText should be read
only'); | 26 assert_equals(response.statusText, 'OK', |
23 }, 'Response default value test in ServiceWorkerGlobalScope'); | 27 'Response.statusText should be readonly'); |
| 28 }, 'Response default value test in ServiceWorkerGlobalScope'); |
24 | 29 |
25 test(function() { | 30 test(function() { |
26 var headers = new Headers; | 31 var headers = new Headers; |
27 headers.set('Content-Language', 'ja'); | 32 headers.set('Content-Language', 'ja'); |
28 headers.set('Content-Type', 'text/html; charset=UTF-8'); | 33 headers.set('Content-Type', 'text/html; charset=UTF-8'); |
29 headers.set('X-ServiceWorker-Test', 'response test field'); | 34 headers.set('X-ServiceWorker-Test', 'response test field'); |
30 headers.set('Set-Cookie', 'response test set-cookie'); | 35 headers.set('Set-Cookie', 'response test set-cookie'); |
31 headers.set('Set-Cookie2', 'response test set-cookie2'); | 36 headers.set('Set-Cookie2', 'response test set-cookie2'); |
32 | 37 |
33 var responses = | 38 var responses = |
34 [new Response(new Blob(), | 39 [new Response(new Blob(), |
35 {status: 303, statusText: 'See Other', headers: headers}), | 40 {status: 303, statusText: 'See Other', headers: headers}), |
36 new Response(new Blob(), | 41 new Response(new Blob(), |
37 { | 42 { |
38 status: 303, | 43 status: 303, |
39 statusText: 'See Other', | 44 statusText: 'See Other', |
40 headers: {'Content-Language': 'ja', | 45 headers: {'Content-Language': 'ja', |
41 'Content-Type': 'text/html; charset=UTF-8', | 46 'Content-Type': 'text/html; charset=UTF-8', |
42 'X-ServiceWorker-Test': 'response test field
', | 47 'X-ServiceWorker-Test': 'response test field', |
43 'Set-Cookie': 'response test set-cookie', | 48 'Set-Cookie': 'response test set-cookie', |
44 'Set-Cookie2': 'response test set-cookie2'} | 49 'Set-Cookie2': 'response test set-cookie2'} |
45 }), | 50 }), |
46 new Response(new Blob(), | 51 new Response(new Blob(), |
47 { | 52 { |
48 status: 303, | 53 status: 303, |
49 statusText: 'See Other', | 54 statusText: 'See Other', |
50 headers: [['Content-Language', 'ja'], | 55 headers: [['Content-Language', 'ja'], |
51 ['Content-Type', 'text/html; charset=UTF-8']
, | 56 ['Content-Type', 'text/html; charset=UTF-8'], |
52 ['X-ServiceWorker-Test', 'response test fiel
d'], | 57 ['X-ServiceWorker-Test', |
53 ['Set-Cookie', 'response test set-cookie'], | 58 'response test field'], |
54 ['Set-Cookie2', 'response test set-cookie2']
] | 59 ['Set-Cookie', 'response test set-cookie'], |
55 })]; | 60 ['Set-Cookie2', 'response test set-cookie2']] |
| 61 })]; |
56 responses.forEach(function(response) { | 62 responses.forEach(function(response) { |
57 assert_equals(response.status, 303, 'Response.status should match'); | 63 assert_equals(response.status, 303, 'Response.status should match'); |
58 assert_equals(response.statusText, 'See Other', 'Response.statusText sho
uld match'); | 64 assert_equals(response.statusText, 'See Other', |
59 assert_true(response.headers instanceof Headers, 'Response.headers shoul
d be Headers'); | 65 'Response.statusText should match'); |
60 assert_equals(size(response.headers), 3, 'Response.headers size should m
atch'); | 66 assert_true(response.headers instanceof Headers, |
| 67 'Response.headers should be Headers'); |
| 68 assert_equals(size(response.headers), 3, |
| 69 'Response.headers size should match'); |
61 assert_equals(response.headers.get('Content-Language'), 'ja', | 70 assert_equals(response.headers.get('Content-Language'), 'ja', |
62 'Content-Language of Response.headers should match'); | 71 'Content-Language of Response.headers should match'); |
63 assert_equals(response.headers.get('Content-Type'), 'text/html; charset=
UTF-8', | 72 assert_equals(response.headers.get('Content-Type'), |
| 73 'text/html; charset=UTF-8', |
64 'Content-Type of Response.headers should match'); | 74 'Content-Type of Response.headers should match'); |
65 assert_equals(response.headers.get('X-ServiceWorker-Test'), 'response te
st field', | 75 assert_equals(response.headers.get('X-ServiceWorker-Test'), |
| 76 'response test field', |
66 'X-ServiceWorker-Test of Response.headers should match'); | 77 'X-ServiceWorker-Test of Response.headers should match'); |
67 response.headers.set('X-ServiceWorker-Test2', 'response test field2'); | 78 response.headers.set('X-ServiceWorker-Test2', 'response test field2'); |
68 assert_equals(size(response.headers), 4, 'Response.headers size should i
ncrease by 1.'); | 79 assert_equals(size(response.headers), 4, |
69 assert_equals(response.headers.get('X-ServiceWorker-Test2'), 'response t
est field2', | 80 'Response.headers size should increase by 1.'); |
| 81 assert_equals(response.headers.get('X-ServiceWorker-Test2'), |
| 82 'response test field2', |
70 'Response.headers should be added'); | 83 'Response.headers should be added'); |
71 response.headers.set('set-cookie', 'dummy'); | 84 response.headers.set('set-cookie', 'dummy'); |
72 response.headers.set('sEt-cookie', 'dummy'); | 85 response.headers.set('sEt-cookie', 'dummy'); |
73 response.headers.set('set-cookie2', 'dummy'); | 86 response.headers.set('set-cookie2', 'dummy'); |
74 response.headers.set('set-cOokie2', 'dummy'); | 87 response.headers.set('set-cOokie2', 'dummy'); |
75 response.headers.append('set-cookie', 'dummy'); | 88 response.headers.append('set-cookie', 'dummy'); |
76 response.headers.append('sEt-cookie', 'dummy'); | 89 response.headers.append('sEt-cookie', 'dummy'); |
77 response.headers.append('set-cookie2', 'dummy'); | 90 response.headers.append('set-cookie2', 'dummy'); |
78 response.headers.append('set-cOokie2', 'dummy'); | 91 response.headers.append('set-cOokie2', 'dummy'); |
79 assert_equals(size(response.headers), 4, | 92 assert_equals(size(response.headers), 4, |
80 'Response.headers should not accept Set-Cookie nor Set-Coo
kie2'); | 93 'Response.headers should not accept Set-Cookie nor ' + |
| 94 'Set-Cookie2'); |
81 response.headers.delete('X-ServiceWorker-Test'); | 95 response.headers.delete('X-ServiceWorker-Test'); |
82 assert_equals(size(response.headers), 3, 'Response.headers size should d
ecrease by 1.'); | 96 assert_equals(size(response.headers), 3, |
83 }); | 97 'Response.headers size should decrease by 1.'); |
| 98 }); |
84 // Note: detailed behavioral tests for Headers are in another test, | 99 // Note: detailed behavioral tests for Headers are in another test, |
85 // http/tests/serviceworker/headers.html. | 100 // http/tests/serviceworker/headers.html. |
86 }, 'Response constructor test in ServiceWorkerGlobalScope'); | 101 }, 'Response constructor test in ServiceWorkerGlobalScope'); |
87 | 102 |
88 test(function() { | 103 test(function() { |
89 var response = new Response(new Blob(['dummy'], {type :'audio/wav'})); | 104 var response = new Response(new Blob(['dummy'], {type: 'audio/wav'})); |
90 assert_equals(size(response.headers), 1, 'Response.headers should have Conte
nt-Type'); | 105 assert_equals(size(response.headers), 1, |
| 106 'Response.headers should have Content-Type'); |
91 assert_equals(response.headers.get('Content-Type'), 'audio/wav', | 107 assert_equals(response.headers.get('Content-Type'), 'audio/wav', |
92 'Content-Type of Response.headers should be set'); | 108 'Content-Type of Response.headers should be set'); |
93 | 109 |
94 response = new Response(new Blob(['dummy'], {type :'audio/wav'}), | 110 response = new Response(new Blob(['dummy'], {type: 'audio/wav'}), |
95 {headers:{'Content-Type': 'text/html; charset=UTF-8'
}}); | 111 { |
96 assert_equals(size(response.headers), 1, 'Response.headers should have Conte
nt-Type'); | 112 headers: { |
97 assert_equals(response.headers.get('Content-Type'), 'text/html; charset=UTF-
8', | 113 'Content-Type': 'text/html; charset=UTF-8' |
| 114 } |
| 115 }); |
| 116 assert_equals(size(response.headers), 1, |
| 117 'Response.headers should have Content-Type'); |
| 118 assert_equals(response.headers.get('Content-Type'), |
| 119 'text/html; charset=UTF-8', |
98 'Content-Type of Response.headers should be overridden'); | 120 'Content-Type of Response.headers should be overridden'); |
99 }, 'Response content type test in ServiceWorkerGlobalScope'); | 121 }, 'Response content type test in ServiceWorkerGlobalScope'); |
100 | 122 |
101 test(function() { | 123 test(function() { |
102 [0, 1, 100, 199, 600, 700].forEach(function(status) { | 124 [0, 1, 100, 199, 600, 700].forEach(function(status) { |
103 assert_throws({name:'RangeError'}, | 125 assert_throws({name: 'RangeError'}, |
104 function() { new Response(new Blob(), {status: status}); }
, | 126 function() { |
| 127 new Response(new Blob(), {status: status}); |
| 128 }, |
105 'new Response with status = ' + status + ' should throw'); | 129 'new Response with status = ' + status + ' should throw'); |
106 }); | 130 }); |
107 [200, 300, 400, 500, 599].forEach(function(status) { | 131 [200, 300, 400, 500, 599].forEach(function(status) { |
108 var response = new Response(new Blob(), {status: status}); | 132 var response = new Response(new Blob(), {status: status}); |
109 assert_equals(response.status, status, 'Response.status should match'); | 133 assert_equals(response.status, status, 'Response.status should match'); |
110 }); | 134 }); |
111 | 135 |
112 var invalidNames = ['', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', | 136 var invalidNames = ['', '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', |
113 '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b']; | 137 '/', '[', ']', '?', '=', '{', '}', '\u3042', 'a(b']; |
114 invalidNames.forEach(function(name) { | 138 invalidNames.forEach(function(name) { |
115 assert_throws({name:'TypeError'}, | 139 assert_throws( |
116 function() { | 140 {name: 'TypeError'}, |
117 var obj = {}; | 141 function() { |
118 obj[name] = 'a'; | 142 var obj = {}; |
119 new Response(new Blob(), {headers: obj}); | 143 obj[name] = 'a'; |
120 }, | 144 new Response(new Blob(), {headers: obj}); |
121 'new Response with headers with an invalid name (' + name
+') should throw'); | 145 }, |
122 assert_throws({name:'TypeError'}, | 146 'new Response with headers with an invalid name (' + name + |
123 function() { | 147 ') should throw'); |
124 new Response(new Blob(), {headers: [[name, 'a']]}); | 148 assert_throws( |
125 }, | 149 {name: 'TypeError'}, |
126 'new Response with headers with an invalid name (' + name
+') should throw'); | 150 function() { |
127 }); | 151 new Response(new Blob(), {headers: [[name, 'a']]}); |
| 152 }, |
| 153 'new Response with headers with an invalid name (' + name + |
| 154 ') should throw'); |
| 155 }); |
128 var invalidValues = ['test \r data', 'test \n data']; | 156 var invalidValues = ['test \r data', 'test \n data']; |
129 invalidValues.forEach(function(value) { | 157 invalidValues.forEach(function(value) { |
130 assert_throws({name:'TypeError'}, | 158 assert_throws( |
131 function() { | 159 {name: 'TypeError'}, |
132 new Response(new Blob(), | 160 function() { |
133 {headers: {'X-ServiceWorker-Test': value}
}); | 161 new Response(new Blob(), |
134 }, | 162 {headers: {'X-ServiceWorker-Test': value}}); |
135 'new Response with headers with an invalid value should th
row'); | 163 }, |
136 assert_throws({name:'TypeError'}, | 164 'new Response with headers with an invalid value should throw'); |
137 function() { | 165 assert_throws( |
138 new Response(new Blob(), | 166 {name: 'TypeError'}, |
139 {headers: [['X-ServiceWorker-Test', value
]]}); | 167 function() { |
140 }, | 168 new Response(new Blob(), |
141 'new Response with headers with an invalid value should th
row'); | 169 {headers: [['X-ServiceWorker-Test', value]]}); |
142 }); | 170 }, |
143 }, 'Response throw error test in ServiceWorkerGlobalScope'); | 171 'new Response with headers with an invalid value should throw'); |
| 172 }); |
| 173 }, 'Response throw error test in ServiceWorkerGlobalScope'); |
OLD | NEW |