OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 | 5 |
6 import "dart:io"; | 6 import "dart:io"; |
7 import "dart:uri"; | 7 import "dart:uri"; |
8 | 8 |
9 HttpServer setupServer() { | 9 HttpServer setupServer() { |
10 HttpServer server = new HttpServer(); | 10 HttpServer server = new HttpServer(); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 } | 141 } |
142 } | 142 } |
143 } | 143 } |
144 | 144 |
145 void testManualRedirect() { | 145 void testManualRedirect() { |
146 HttpServer server = setupServer(); | 146 HttpServer server = setupServer(); |
147 HttpClient client = new HttpClient(); | 147 HttpClient client = new HttpClient(); |
148 | 148 |
149 int redirectCount = 0; | 149 int redirectCount = 0; |
150 HttpClientConnection conn = | 150 HttpClientConnection conn = |
151 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/1")); | 151 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")); |
152 conn.followRedirects = false; | 152 conn.followRedirects = false; |
153 conn.onResponse = (HttpClientResponse response) { | 153 conn.onResponse = (HttpClientResponse response) { |
154 response.inputStream.onData = response.inputStream.read; | 154 response.inputStream.onData = response.inputStream.read; |
155 response.inputStream.onClosed = () { | 155 response.inputStream.onClosed = () { |
156 redirectCount++; | 156 redirectCount++; |
157 if (redirectCount < 10) { | 157 if (redirectCount < 10) { |
158 Expect.isTrue(response.isRedirect); | 158 Expect.isTrue(response.isRedirect); |
159 checkRedirects(redirectCount, conn); | 159 checkRedirects(redirectCount, conn); |
160 conn.redirect(); | 160 conn.redirect(); |
161 } else { | 161 } else { |
162 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | 162 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); |
163 server.close(); | 163 server.close(); |
164 client.shutdown(); | 164 client.shutdown(); |
165 } | 165 } |
166 }; | 166 }; |
167 }; | 167 }; |
168 } | 168 } |
169 | 169 |
170 void testManualRedirectWithHeaders() { | 170 void testManualRedirectWithHeaders() { |
171 HttpServer server = setupServer(); | 171 HttpServer server = setupServer(); |
172 HttpClient client = new HttpClient(); | 172 HttpClient client = new HttpClient(); |
173 | 173 |
174 int redirectCount = 0; | 174 int redirectCount = 0; |
175 HttpClientConnection conn = | 175 HttpClientConnection conn = |
176 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/src")); | 176 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")); |
177 conn.followRedirects = false; | 177 conn.followRedirects = false; |
178 conn.onRequest = (HttpClientRequest request) { | 178 conn.onRequest = (HttpClientRequest request) { |
179 request.headers.add("X-Request-Header", "value"); | 179 request.headers.add("X-Request-Header", "value"); |
180 request.outputStream.close(); | 180 request.outputStream.close(); |
181 }; | 181 }; |
182 conn.onResponse = (HttpClientResponse response) { | 182 conn.onResponse = (HttpClientResponse response) { |
183 response.inputStream.onData = response.inputStream.read; | 183 response.inputStream.onData = response.inputStream.read; |
184 response.inputStream.onClosed = () { | 184 response.inputStream.onClosed = () { |
185 redirectCount++; | 185 redirectCount++; |
186 if (redirectCount < 2) { | 186 if (redirectCount < 2) { |
(...skipping 24 matching lines...) Expand all Loading... |
211 () => Expect.fail("Response data not expected"); | 211 () => Expect.fail("Response data not expected"); |
212 response.inputStream.onClosed = () { | 212 response.inputStream.onClosed = () { |
213 Expect.equals(1, requestCount); | 213 Expect.equals(1, requestCount); |
214 server.close(); | 214 server.close(); |
215 client.shutdown(); | 215 client.shutdown(); |
216 }; | 216 }; |
217 }; | 217 }; |
218 | 218 |
219 HttpClientConnection conn = | 219 HttpClientConnection conn = |
220 client.getUrl( | 220 client.getUrl( |
221 new Uri.fromString("http://127.0.0.1:${server.port}/redirect")); | 221 Uri.parse("http://127.0.0.1:${server.port}/redirect")); |
222 conn.onRequest = onRequest; | 222 conn.onRequest = onRequest; |
223 conn.onResponse = onResponse; | 223 conn.onResponse = onResponse; |
224 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | 224 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
225 } | 225 } |
226 | 226 |
227 void testAutoRedirectWithHeaders() { | 227 void testAutoRedirectWithHeaders() { |
228 HttpServer server = setupServer(); | 228 HttpServer server = setupServer(); |
229 HttpClient client = new HttpClient(); | 229 HttpClient client = new HttpClient(); |
230 | 230 |
231 var requestCount = 0; | 231 var requestCount = 0; |
232 | 232 |
233 void onRequest(HttpClientRequest request) { | 233 void onRequest(HttpClientRequest request) { |
234 requestCount++; | 234 requestCount++; |
235 request.headers.add("X-Request-Header", "value"); | 235 request.headers.add("X-Request-Header", "value"); |
236 request.outputStream.close(); | 236 request.outputStream.close(); |
237 }; | 237 }; |
238 | 238 |
239 void onResponse(HttpClientResponse response) { | 239 void onResponse(HttpClientResponse response) { |
240 response.inputStream.onData = | 240 response.inputStream.onData = |
241 () => Expect.fail("Response data not expected"); | 241 () => Expect.fail("Response data not expected"); |
242 response.inputStream.onClosed = () { | 242 response.inputStream.onClosed = () { |
243 Expect.equals(1, requestCount); | 243 Expect.equals(1, requestCount); |
244 server.close(); | 244 server.close(); |
245 client.shutdown(); | 245 client.shutdown(); |
246 }; | 246 }; |
247 }; | 247 }; |
248 | 248 |
249 HttpClientConnection conn = | 249 HttpClientConnection conn = |
250 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/src")); | 250 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")); |
251 conn.onRequest = onRequest; | 251 conn.onRequest = onRequest; |
252 conn.onResponse = onResponse; | 252 conn.onResponse = onResponse; |
253 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | 253 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
254 } | 254 } |
255 | 255 |
256 void testAutoRedirect301POST() { | 256 void testAutoRedirect301POST() { |
257 HttpServer server = setupServer(); | 257 HttpServer server = setupServer(); |
258 HttpClient client = new HttpClient(); | 258 HttpClient client = new HttpClient(); |
259 | 259 |
260 var requestCount = 0; | 260 var requestCount = 0; |
261 | 261 |
262 void onRequest(HttpClientRequest request) { | 262 void onRequest(HttpClientRequest request) { |
263 requestCount++; | 263 requestCount++; |
264 request.outputStream.close(); | 264 request.outputStream.close(); |
265 }; | 265 }; |
266 | 266 |
267 void onResponse(HttpClientResponse response) { | 267 void onResponse(HttpClientResponse response) { |
268 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | 268 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
269 response.inputStream.onData = | 269 response.inputStream.onData = |
270 () => Expect.fail("Response data not expected"); | 270 () => Expect.fail("Response data not expected"); |
271 response.inputStream.onClosed = () { | 271 response.inputStream.onClosed = () { |
272 Expect.equals(1, requestCount); | 272 Expect.equals(1, requestCount); |
273 server.close(); | 273 server.close(); |
274 client.shutdown(); | 274 client.shutdown(); |
275 }; | 275 }; |
276 }; | 276 }; |
277 | 277 |
278 HttpClientConnection conn = | 278 HttpClientConnection conn = |
279 client.postUrl( | 279 client.postUrl( |
280 new Uri.fromString("http://127.0.0.1:${server.port}/301src")); | 280 Uri.parse("http://127.0.0.1:${server.port}/301src")); |
281 conn.onRequest = onRequest; | 281 conn.onRequest = onRequest; |
282 conn.onResponse = onResponse; | 282 conn.onResponse = onResponse; |
283 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | 283 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
284 } | 284 } |
285 | 285 |
286 void testAutoRedirect303POST() { | 286 void testAutoRedirect303POST() { |
287 HttpServer server = setupServer(); | 287 HttpServer server = setupServer(); |
288 HttpClient client = new HttpClient(); | 288 HttpClient client = new HttpClient(); |
289 | 289 |
290 var requestCount = 0; | 290 var requestCount = 0; |
(...skipping 11 matching lines...) Expand all Loading... |
302 () => Expect.fail("Response data not expected"); | 302 () => Expect.fail("Response data not expected"); |
303 response.inputStream.onClosed = () { | 303 response.inputStream.onClosed = () { |
304 Expect.equals(1, requestCount); | 304 Expect.equals(1, requestCount); |
305 server.close(); | 305 server.close(); |
306 client.shutdown(); | 306 client.shutdown(); |
307 }; | 307 }; |
308 }; | 308 }; |
309 | 309 |
310 HttpClientConnection conn = | 310 HttpClientConnection conn = |
311 client.postUrl( | 311 client.postUrl( |
312 new Uri.fromString("http://127.0.0.1:${server.port}/303src")); | 312 Uri.parse("http://127.0.0.1:${server.port}/303src")); |
313 conn.onRequest = onRequest; | 313 conn.onRequest = onRequest; |
314 conn.onResponse = onResponse; | 314 conn.onResponse = onResponse; |
315 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | 315 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
316 } | 316 } |
317 | 317 |
318 void testAutoRedirectLimit() { | 318 void testAutoRedirectLimit() { |
319 HttpServer server = setupServer(); | 319 HttpServer server = setupServer(); |
320 HttpClient client = new HttpClient(); | 320 HttpClient client = new HttpClient(); |
321 | 321 |
322 HttpClientConnection conn = | 322 HttpClientConnection conn = |
323 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/1")); | 323 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")); |
324 conn.onResponse = (HttpClientResponse response) { | 324 conn.onResponse = (HttpClientResponse response) { |
325 response.inputStream.onData = () => Expect.fail("Response not expected"); | 325 response.inputStream.onData = () => Expect.fail("Response not expected"); |
326 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | 326 response.inputStream.onClosed = () => Expect.fail("Response not expected"); |
327 }; | 327 }; |
328 conn.onError = (e) { | 328 conn.onError = (e) { |
329 Expect.isTrue(e is RedirectLimitExceededException); | 329 Expect.isTrue(e is RedirectLimitExceededException); |
330 Expect.equals(5, e.redirects.length); | 330 Expect.equals(5, e.redirects.length); |
331 server.close(); | 331 server.close(); |
332 client.shutdown(); | 332 client.shutdown(); |
333 }; | 333 }; |
334 } | 334 } |
335 | 335 |
336 void testRedirectLoop() { | 336 void testRedirectLoop() { |
337 HttpServer server = setupServer(); | 337 HttpServer server = setupServer(); |
338 HttpClient client = new HttpClient(); | 338 HttpClient client = new HttpClient(); |
339 | 339 |
340 int redirectCount = 0; | 340 int redirectCount = 0; |
341 HttpClientConnection conn = | 341 HttpClientConnection conn = |
342 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/A")); | 342 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/A")); |
343 conn.onResponse = (HttpClientResponse response) { | 343 conn.onResponse = (HttpClientResponse response) { |
344 response.inputStream.onData = () => Expect.fail("Response not expected"); | 344 response.inputStream.onData = () => Expect.fail("Response not expected"); |
345 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | 345 response.inputStream.onClosed = () => Expect.fail("Response not expected"); |
346 }; | 346 }; |
347 conn.onError = (e) { | 347 conn.onError = (e) { |
348 Expect.isTrue(e is RedirectLoopException); | 348 Expect.isTrue(e is RedirectLoopException); |
349 Expect.equals(2, e.redirects.length); | 349 Expect.equals(2, e.redirects.length); |
350 server.close(); | 350 server.close(); |
351 client.shutdown(); | 351 client.shutdown(); |
352 }; | 352 }; |
353 } | 353 } |
354 | 354 |
355 main() { | 355 main() { |
356 testManualRedirect(); | 356 testManualRedirect(); |
357 testManualRedirectWithHeaders(); | 357 testManualRedirectWithHeaders(); |
358 testAutoRedirect(); | 358 testAutoRedirect(); |
359 testAutoRedirectWithHeaders(); | 359 testAutoRedirectWithHeaders(); |
360 testAutoRedirect301POST(); | 360 testAutoRedirect301POST(); |
361 testAutoRedirect303POST(); | 361 testAutoRedirect303POST(); |
362 testAutoRedirectLimit(); | 362 testAutoRedirectLimit(); |
363 testRedirectLoop(); | 363 testRedirectLoop(); |
364 } | 364 } |
OLD | NEW |