OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } | 80 } |
81 ); | 81 ); |
82 server.addRequestHandler( | 82 server.addRequestHandler( |
83 (HttpRequest request) => request.path == "/target", | 83 (HttpRequest request) => request.path == "/target", |
84 (HttpRequest request, HttpResponse response) { | 84 (HttpRequest request, HttpResponse response) { |
85 Expect.equals("value", request.headers.value("X-Request-Header")); | 85 Expect.equals("value", request.headers.value("X-Request-Header")); |
86 response.outputStream.close(); | 86 response.outputStream.close(); |
87 } | 87 } |
88 ); | 88 ); |
89 | 89 |
| 90 // Setup redirect for 301 where POST should not redirect. |
| 91 server.addRequestHandler( |
| 92 (HttpRequest request) => request.path == "/301src", |
| 93 (HttpRequest request, HttpResponse response) { |
| 94 Expect.equals("POST", request.method); |
| 95 response.headers.set(HttpHeaders.LOCATION, |
| 96 "http://127.0.0.1:${server.port}/301target"); |
| 97 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 98 response.outputStream.close(); |
| 99 } |
| 100 ); |
| 101 server.addRequestHandler( |
| 102 (HttpRequest request) => request.path == "/301target", |
| 103 (HttpRequest request, HttpResponse response) { |
| 104 Expect.fail("Redirect of POST should not happen"); |
| 105 } |
| 106 ); |
| 107 |
| 108 // Setup redirect for 303 where POST should turn into GET. |
| 109 server.addRequestHandler( |
| 110 (HttpRequest request) => request.path == "/303src", |
| 111 (HttpRequest request, HttpResponse response) { |
| 112 Expect.equals("POST", request.method); |
| 113 response.headers.set(HttpHeaders.LOCATION, |
| 114 "http://127.0.0.1:${server.port}/303target"); |
| 115 response.statusCode = HttpStatus.SEE_OTHER; |
| 116 response.outputStream.close(); |
| 117 } |
| 118 ); |
| 119 server.addRequestHandler( |
| 120 (HttpRequest request) => request.path == "/303target", |
| 121 (HttpRequest request, HttpResponse response) { |
| 122 Expect.equals("GET", request.method); |
| 123 response.outputStream.close(); |
| 124 } |
| 125 ); |
| 126 |
90 return server; | 127 return server; |
91 } | 128 } |
92 | 129 |
93 void checkRedirects(int redirectCount, HttpClientConnection conn) { | 130 void checkRedirects(int redirectCount, HttpClientConnection conn) { |
94 if (redirectCount < 2) { | 131 if (redirectCount < 2) { |
95 Expect.isNull(conn.redirects); | 132 Expect.isNull(conn.redirects); |
96 } else { | 133 } else { |
97 Expect.equals(redirectCount - 1, conn.redirects.length); | 134 Expect.equals(redirectCount - 1, conn.redirects.length); |
98 for (int i = 0; i < redirectCount - 2; i++) { | 135 for (int i = 0; i < redirectCount - 2; i++) { |
99 Expect.equals(conn.redirects[i].location.path, "/${i + 2}"); | 136 Expect.equals(conn.redirects[i].location.path, "/${i + 2}"); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 }; | 242 }; |
206 }; | 243 }; |
207 | 244 |
208 HttpClientConnection conn = | 245 HttpClientConnection conn = |
209 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/src")); | 246 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/src")); |
210 conn.onRequest = onRequest; | 247 conn.onRequest = onRequest; |
211 conn.onResponse = onResponse; | 248 conn.onResponse = onResponse; |
212 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | 249 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
213 } | 250 } |
214 | 251 |
| 252 void testAutoRedirect301POST() { |
| 253 HttpServer server = setupServer(); |
| 254 HttpClient client = new HttpClient(); |
| 255 |
| 256 var requestCount = 0; |
| 257 |
| 258 void onRequest(HttpClientRequest request) { |
| 259 requestCount++; |
| 260 request.outputStream.close(); |
| 261 }; |
| 262 |
| 263 void onResponse(HttpClientResponse response) { |
| 264 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 265 response.inputStream.onData = |
| 266 () => Expect.fail("Response data not expected"); |
| 267 response.inputStream.onClosed = () { |
| 268 Expect.equals(1, requestCount); |
| 269 server.close(); |
| 270 client.shutdown(); |
| 271 }; |
| 272 }; |
| 273 |
| 274 HttpClientConnection conn = |
| 275 client.postUrl( |
| 276 new Uri.fromString("http://127.0.0.1:${server.port}/301src")); |
| 277 conn.onRequest = onRequest; |
| 278 conn.onResponse = onResponse; |
| 279 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
| 280 } |
| 281 |
| 282 void testAutoRedirect303POST() { |
| 283 HttpServer server = setupServer(); |
| 284 HttpClient client = new HttpClient(); |
| 285 |
| 286 var requestCount = 0; |
| 287 |
| 288 void onRequest(HttpClientRequest request) { |
| 289 requestCount++; |
| 290 request.outputStream.close(); |
| 291 }; |
| 292 |
| 293 void onResponse(HttpClientResponse response) { |
| 294 Expect.equals(HttpStatus.OK, response.statusCode); |
| 295 response.inputStream.onData = |
| 296 () => Expect.fail("Response data not expected"); |
| 297 response.inputStream.onClosed = () { |
| 298 Expect.equals(1, requestCount); |
| 299 server.close(); |
| 300 client.shutdown(); |
| 301 }; |
| 302 }; |
| 303 |
| 304 HttpClientConnection conn = |
| 305 client.postUrl( |
| 306 new Uri.fromString("http://127.0.0.1:${server.port}/303src")); |
| 307 conn.onRequest = onRequest; |
| 308 conn.onResponse = onResponse; |
| 309 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
| 310 } |
| 311 |
215 void testAutoRedirectLimit() { | 312 void testAutoRedirectLimit() { |
216 HttpServer server = setupServer(); | 313 HttpServer server = setupServer(); |
217 HttpClient client = new HttpClient(); | 314 HttpClient client = new HttpClient(); |
218 | 315 |
219 HttpClientConnection conn = | 316 HttpClientConnection conn = |
220 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/1")); | 317 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/1")); |
221 conn.onResponse = (HttpClientResponse response) { | 318 conn.onResponse = (HttpClientResponse response) { |
222 response.inputStream.onData = () => Expect.fail("Response not expected"); | 319 response.inputStream.onData = () => Expect.fail("Response not expected"); |
223 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | 320 response.inputStream.onClosed = () => Expect.fail("Response not expected"); |
224 }; | 321 }; |
(...skipping 22 matching lines...) Expand all Loading... |
247 server.close(); | 344 server.close(); |
248 client.shutdown(); | 345 client.shutdown(); |
249 }; | 346 }; |
250 } | 347 } |
251 | 348 |
252 main() { | 349 main() { |
253 testManualRedirect(); | 350 testManualRedirect(); |
254 testManualRedirectWithHeaders(); | 351 testManualRedirectWithHeaders(); |
255 testAutoRedirect(); | 352 testAutoRedirect(); |
256 testAutoRedirectWithHeaders(); | 353 testAutoRedirectWithHeaders(); |
| 354 testAutoRedirect301POST(); |
| 355 testAutoRedirect303POST(); |
257 testAutoRedirectLimit(); | 356 testAutoRedirectLimit(); |
258 testRedirectLoop(); | 357 testRedirectLoop(); |
259 } | 358 } |
OLD | NEW |