OLD | NEW |
---|---|
(Empty) | |
1 importScripts('worker-testharness.js'); | |
2 importScripts('/resources/testharness-helpers.js'); | |
3 | |
4 var test_url = 'https://example.com/foo'; | |
5 | |
6 // Construct a generic Request object. The URL is |test_url|. All other fields | |
7 // are defaults. | |
8 function new_test_request() { | |
9 return new Request(test_url); | |
10 } | |
11 | |
12 | |
13 // Construct a generic Response object. The URL is empty. If specified |body| | |
14 // will be set as the response body string. | |
15 function new_test_response(body) { | |
16 body = body || 'Hello world!'; | |
17 return new Response(body, { | |
18 status: 200, | |
19 statusText: 'OK', | |
20 headers: [['Content-Type', 'text/plain']] | |
21 }); | |
22 } | |
23 | |
24 cache_test(function(cache) { | |
25 var request = new_test_request(); | |
26 var response = new_test_response(); | |
27 return cache.put(request, response) | |
28 .then(function(result) { | |
29 assert_equals(result, undefined, | |
30 'Cache.put should resolve with undefined on success.'); | |
31 }); | |
32 }, 'Cache.put result on success'); | |
33 | |
34 cache_test(function(cache) { | |
35 var test_url = new URL('simple.txt', location.href).href; | |
36 var request = new Request(test_url); | |
37 var response; | |
38 return fetch(test_url) | |
39 .then(function(fetch_result) { | |
40 response = fetch_result.clone(); | |
41 return cache.put(request, fetch_result); | |
42 }) | |
43 .then(function() { | |
44 return cache.match(test_url); | |
45 }) | |
46 .then(function(result) { | |
47 assert_object_equals(result, response, | |
48 'Cache.put should update the cache with ' + | |
49 'new request and response.'); | |
50 return result.text(); | |
51 }) | |
52 .then(function(body) { | |
53 assert_equals(body, 'a simple text file\n', | |
54 'Cache.put should store response body.'); | |
55 }); | |
56 }, 'Cache.put with a valid request and response'); | |
57 | |
58 cache_test(function(cache) { | |
59 var request = new Request(test_url, { | |
60 method: 'GET', | |
61 body: 'Hello' | |
62 }); | |
63 var response = new_test_response(); | |
64 assert_false(request.bodyUsed, | |
65 '[https://fetch.spec.whatwg.org/#dom-body-bodyused] ' + | |
66 'Request.bodyUsed should be initially false.'); | |
67 return cache.put(request, response) | |
68 .then(function() { | |
69 assert_false(request.bodyUsed, | |
70 'Cache.put should not consume Request body.'); | |
71 }) | |
72 .then(function() { | |
73 return cache.match(request); | |
74 }) | |
75 .then(function(result) { | |
76 assert_object_equals(result, response, | |
77 'Cache.put should store response body.'); | |
78 }); | |
79 }, 'Cache.put with request containing a body'); | |
80 | |
81 cache_test(function(cache) { | |
82 var request = new_test_request(); | |
83 var response = new_test_response(); | |
84 return cache.put(request, response) | |
85 .then(function() { | |
86 return cache.match(test_url); | |
87 }) | |
88 .then(function(result) { | |
89 assert_object_equals(result, response, | |
90 'Cache.put should update the cache with ' + | |
91 'new request and response.'); | |
92 }); | |
93 }, 'Cache.put with a response containing an empty URL'); | |
94 | |
95 // TODO: This test currently causes a browser crash. Enable test again once the | |
96 // crash is resolved. http://crbug.com/426150 | |
jsbell
2014/10/23 00:30:32
Testing locally: crash resolved by https://coderev
asanka
2014/10/23 06:12:39
Enabling test and updating expectations.
| |
97 if (false) { | |
98 cache_test(function(cache) { | |
99 var request = new_test_request(); | |
100 var response = new Response('', { | |
101 status: 200, | |
102 headers: [['Content-Type', 'text/plain']] | |
103 }); | |
104 return cache.put(request, response) | |
105 .then(function() { | |
106 return cache.match(test_url); | |
107 }) | |
108 .then(function(result) { | |
109 assert_object_equals(result, response, | |
110 'Cache.put should update the cache with ' + | |
111 'new request and response.'); | |
112 return result.text(); | |
113 }) | |
114 .then(function(body) { | |
115 assert_equals(body, '', | |
116 'Cache.put should store response body.'); | |
117 }); | |
118 }, 'Cache.put with an empty response body'); | |
119 } | |
120 | |
121 // TODO: This test currently causes a renderer crash. Enable test again once the | |
122 // crash is resolved. http://crbug.com/426153 | |
jsbell
2014/10/23 00:15:31
Fix posted - thanks for catching this!
jsbell
2014/10/23 00:23:51
Well, fix posted crbug.com/426153 (i.e. put(..., s
jsbell
2014/10/23 00:30:32
Testing locally: resolved by https://codereview.ch
asanka
2014/10/23 06:12:39
Whoops. Bug annotations was wrong. This should be
| |
123 if (false) { | |
124 cache_test(function(cache) { | |
125 var test_url = new URL('fetch-status.php?status=500', location.href).href; | |
126 var request = new Request(test_url); | |
127 var response; | |
128 return fetch(test_url) | |
129 .then(function(fetch_result) { | |
130 response = fetch_result.clone(); | |
131 return cache.put(request, fetch_result); | |
132 }) | |
133 .then(function() { | |
134 return cache.match(test_url); | |
135 }) | |
136 .then(function(result) { | |
137 assert_object_equals(result, response, | |
138 'Cache.put should update the cache with ' + | |
139 'new request and response.'); | |
140 return result.text(); | |
141 }) | |
142 .then(function(body) { | |
143 assert_equals(body, '', | |
144 'Cache.put should store response body.'); | |
145 }); | |
146 }, 'Cache.put with HTTP 500 response'); | |
147 } | |
148 | |
149 cache_test(function(cache) { | |
150 var alternate_response = new_test_response('Lorem ipsum'); | |
151 return cache.put(new_test_request(), new_test_response()) | |
152 .then(function() { | |
153 return cache.put(new_test_request(), alternate_response); | |
154 }) | |
155 .then(function() { | |
156 return cache.match(test_url); | |
157 }) | |
158 .then(function(result) { | |
159 assert_object_equals(result, alternate_response, | |
160 'Cache.put should replace existing ' + | |
161 'response with new response.'); | |
162 }); | |
163 }, 'Cache.put with an existing request'); | |
164 | |
165 cache_test(function(cache) { | |
166 return assert_promise_rejects( | |
167 cache.put('http://example.com/foo', new_test_response()), | |
168 new TypeError(), | |
169 'Cache.put should only accept a Request object as the request.'); | |
170 }, 'Cache.put with an invalid request'); | |
171 | |
172 // TODO: This test currently causes a renderer crash. Enable test again once the | |
173 // crash is resolved. http://crbug.com/426153 | |
jsbell
2014/10/23 00:30:32
Testing locally: resolved by https://codereview.ch
asanka
2014/10/23 06:12:39
Ack. If this lands before that CL, I'll enable the
| |
174 if (false) { | |
175 cache_test(function(cache) { | |
176 return assert_promise_rejects( | |
177 cache.put(new_test_request(), 'Hello world!'), | |
178 new TypeError(), | |
179 'Cache.put should only accept a Response object as the response.'); | |
180 }, 'Cache.put with an invalid response'); | |
181 } | |
182 | |
183 cache_test(function(cache) { | |
184 return assert_promise_rejects( | |
185 cache.put(new Request('file:///etc/passwd'), new_test_response()), | |
186 new TypeError(), | |
187 'Cache.put should reject non-HTTP/HTTPS requests with a TypeError.'); | |
188 }, 'Cache.put with a non-HTTP/HTTPS request'); | |
189 | |
190 cache_test(function(cache) { | |
191 var response = new_test_response(); | |
192 return cache.put(new Request('relative-url'), response) | |
193 .then(function() { | |
194 return cache.match(new URL('relative-url', location.href).href); | |
195 }) | |
196 .then(function(result) { | |
197 assert_object_equals(result, response, | |
198 'Cache.put should accept a relative URL ' + | |
199 'as the request.'); | |
200 }); | |
201 }, 'Cache.put with a relative URL'); | |
202 | |
203 cache_test(function(cache) { | |
204 var request = new Request('http://example.com/foo', {method: 'get'}); | |
205 return assert_promise_rejects( | |
206 cache.put(request, new_test_response()), | |
207 new TypeError(), | |
208 'Cache.put should throw a TypeError for non-GET requests.'); | |
209 }, 'Cache.put with a non-GET request'); | |
210 | |
211 cache_test(function(cache) { | |
212 var request = new Request('http://example.com/foo', {method: 'HEAD'}); | |
213 return assert_promise_rejects( | |
214 cache.put(request, new_test_response()), | |
215 new TypeError(), | |
216 'Cache.put should throw a TypeError for non-GET requests.'); | |
217 }, 'Cache.put with a non-GET request'); | |
218 | |
219 // TODO: This test currently cuases a renderer crash. Enable test again once the | |
220 // crash is resolved. http://crbug.com/426153 | |
jsbell
2014/10/23 00:30:32
Testing locally: resolved by https://codereview.ch
asanka
2014/10/23 06:12:39
Acknowledged.
| |
221 if (false) { | |
222 cache_test(function(cache) { | |
223 return assert_promise_rejects( | |
224 cache.put(new_test_request(), null), | |
225 new TypeError(), | |
226 'Cache.put should throw a TypeError for an empty response.'); | |
227 }, 'Cache.put with an empty response'); | |
228 } | |
OLD | NEW |