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:async"; |
6 import "dart:io"; | 7 import "dart:io"; |
7 import "dart:uri"; | 8 import "dart:uri"; |
8 | 9 |
9 HttpServer setupServer() { | 10 Future<HttpServer> setupServer() { |
10 HttpServer server = new HttpServer(); | 11 Completer completer = new Completer(); |
11 server.listen("127.0.0.1", 0, backlog: 5); | 12 HttpServer.bind().then((server) { |
12 | 13 |
13 void addRedirectHandler(int number, int statusCode) { | 14 var handlers = new Map<String, Function>(); |
14 server.addRequestHandler( | 15 addRequestHandler(String path, void handler(HttpRequest request, |
15 (HttpRequest request) => request.path == "/$number", | 16 HttpResponse response)) { |
16 (HttpRequest request, HttpResponse response) { | 17 handlers[path] = handler; |
17 response.headers.set(HttpHeaders.LOCATION, | 18 } |
18 "http://127.0.0.1:${server.port}/${number + 1}"); | 19 |
19 response.statusCode = statusCode; | 20 server.listen((HttpRequest request) { |
20 response.outputStream.close(); | 21 if (handlers.containsKey(request.uri.path)) { |
21 }); | 22 handlers[request.uri.path](request, request.response); |
| 23 } else { |
| 24 request.listen((_) {}, onDone: () { |
| 25 request.response.statusCode = 404; |
| 26 request.response.close(); |
| 27 }); |
| 28 } |
| 29 }); |
| 30 |
| 31 void addRedirectHandler(int number, int statusCode) { |
| 32 addRequestHandler( |
| 33 "/$number", |
| 34 (HttpRequest request, HttpResponse response) { |
| 35 response.headers.set(HttpHeaders.LOCATION, |
| 36 "http://127.0.0.1:${server.port}/${number + 1}"); |
| 37 response.statusCode = statusCode; |
| 38 response.close(); |
| 39 }); |
| 40 } |
| 41 |
| 42 // Setup simple redirect. |
| 43 addRequestHandler( |
| 44 "/redirect", |
| 45 (HttpRequest request, HttpResponse response) { |
| 46 response.headers.set(HttpHeaders.LOCATION, |
| 47 "http://127.0.0.1:${server.port}/location"); |
| 48 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 49 response.close(); |
| 50 } |
| 51 ); |
| 52 addRequestHandler( |
| 53 "/location", |
| 54 (HttpRequest request, HttpResponse response) { |
| 55 response.close(); |
| 56 } |
| 57 ); |
| 58 |
| 59 // Setup redirect chain. |
| 60 int n = 1; |
| 61 addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY); |
| 62 addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY); |
| 63 addRedirectHandler(n++, HttpStatus.SEE_OTHER); |
| 64 addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT); |
| 65 for (int i = n; i < 10; i++) { |
| 66 addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY); |
| 67 } |
| 68 |
| 69 // Setup redirect loop. |
| 70 addRequestHandler( |
| 71 "/A", |
| 72 (HttpRequest request, HttpResponse response) { |
| 73 response.headers.set(HttpHeaders.LOCATION, |
| 74 "http://127.0.0.1:${server.port}/B"); |
| 75 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 76 response.close(); |
| 77 } |
| 78 ); |
| 79 addRequestHandler( |
| 80 "/B", |
| 81 (HttpRequest request, HttpResponse response) { |
| 82 response.headers.set(HttpHeaders.LOCATION, |
| 83 "http://127.0.0.1:${server.port}/A"); |
| 84 response.statusCode = HttpStatus.MOVED_TEMPORARILY; |
| 85 response.close(); |
| 86 } |
| 87 ); |
| 88 |
| 89 // Setup redirect checking headers. |
| 90 addRequestHandler( |
| 91 "/src", |
| 92 (HttpRequest request, HttpResponse response) { |
| 93 Expect.equals("value", request.headers.value("X-Request-Header")); |
| 94 response.headers.set(HttpHeaders.LOCATION, |
| 95 "http://127.0.0.1:${server.port}/target"); |
| 96 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 97 response.close(); |
| 98 } |
| 99 ); |
| 100 addRequestHandler( |
| 101 "/target", |
| 102 (HttpRequest request, HttpResponse response) { |
| 103 Expect.equals("value", request.headers.value("X-Request-Header")); |
| 104 response.close(); |
| 105 } |
| 106 ); |
| 107 |
| 108 // Setup redirect for 301 where POST should not redirect. |
| 109 addRequestHandler( |
| 110 "/301src", |
| 111 (HttpRequest request, HttpResponse response) { |
| 112 Expect.equals("POST", request.method); |
| 113 request.listen( |
| 114 (_) {}, |
| 115 onDone: () { |
| 116 response.headers.set( |
| 117 HttpHeaders.LOCATION, |
| 118 "http://127.0.0.1:${server.port}/301target"); |
| 119 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 120 response.close(); |
| 121 }); |
| 122 }); |
| 123 addRequestHandler( |
| 124 "/301target", |
| 125 (HttpRequest request, HttpResponse response) { |
| 126 Expect.fail("Redirect of POST should not happen"); |
| 127 } |
| 128 ); |
| 129 |
| 130 // Setup redirect for 303 where POST should turn into GET. |
| 131 addRequestHandler( |
| 132 "/303src", |
| 133 (HttpRequest request, HttpResponse response) { |
| 134 request.listen((_) {}, onDone: () { |
| 135 Expect.equals("POST", request.method); |
| 136 request.listen( |
| 137 (_) {}, |
| 138 onDone: () { |
| 139 response.headers.set( |
| 140 HttpHeaders.LOCATION, |
| 141 "http://127.0.0.1:${server.port}/303target"); |
| 142 response.statusCode = HttpStatus.SEE_OTHER; |
| 143 response.close(); |
| 144 }); |
| 145 }); |
| 146 }); |
| 147 addRequestHandler( |
| 148 "/303target", |
| 149 (HttpRequest request, HttpResponse response) { |
| 150 Expect.equals("GET", request.method); |
| 151 response.close(); |
| 152 }); |
| 153 |
| 154 // Setup redirect where we close the connection. |
| 155 addRequestHandler( |
| 156 "/closing", |
| 157 (HttpRequest request, HttpResponse response) { |
| 158 response.headers.set(HttpHeaders.LOCATION, |
| 159 "http://127.0.0.1:${server.port}/"); |
| 160 response.statusCode = HttpStatus.FOUND; |
| 161 response.persistentConnection = false; |
| 162 response.close(); |
| 163 }); |
| 164 |
| 165 completer.complete(server); |
| 166 }); |
| 167 return completer.future; |
| 168 } |
| 169 |
| 170 void checkRedirects(int redirectCount, HttpClientResponse response) { |
| 171 if (redirectCount < 2) { |
| 172 Expect.isTrue(response.redirects.isEmpty); |
| 173 } else { |
| 174 Expect.equals(redirectCount - 1, response.redirects.length); |
| 175 for (int i = 0; i < redirectCount - 2; i++) { |
| 176 Expect.equals(response.redirects[i].location.path, "/${i + 2}"); |
| 177 } |
22 } | 178 } |
23 | |
24 // Setup simple redirect. | |
25 server.addRequestHandler( | |
26 (HttpRequest request) => request.path == "/redirect", | |
27 (HttpRequest request, HttpResponse response) { | |
28 response.headers.set(HttpHeaders.LOCATION, | |
29 "http://127.0.0.1:${server.port}/location"); | |
30 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | |
31 response.outputStream.close(); | |
32 } | |
33 ); | |
34 server.addRequestHandler( | |
35 (HttpRequest request) => request.path == "/location", | |
36 (HttpRequest request, HttpResponse response) { | |
37 response.outputStream.close(); | |
38 } | |
39 ); | |
40 | |
41 // Setup redirect chain. | |
42 int n = 1; | |
43 addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY); | |
44 addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY); | |
45 addRedirectHandler(n++, HttpStatus.SEE_OTHER); | |
46 addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT); | |
47 for (int i = n; i < 10; i++) { | |
48 addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY); | |
49 } | |
50 | |
51 // Setup redirect loop. | |
52 server.addRequestHandler( | |
53 (HttpRequest request) => request.path == "/A", | |
54 (HttpRequest request, HttpResponse response) { | |
55 response.headers.set(HttpHeaders.LOCATION, | |
56 "http://127.0.0.1:${server.port}/B"); | |
57 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | |
58 response.outputStream.close(); | |
59 } | |
60 ); | |
61 server.addRequestHandler( | |
62 (HttpRequest request) => request.path == "/B", | |
63 (HttpRequest request, HttpResponse response) { | |
64 response.headers.set(HttpHeaders.LOCATION, | |
65 "http://127.0.0.1:${server.port}/A"); | |
66 response.statusCode = HttpStatus.MOVED_TEMPORARILY; | |
67 response.outputStream.close(); | |
68 } | |
69 ); | |
70 | |
71 // Setup redirect checking headers. | |
72 server.addRequestHandler( | |
73 (HttpRequest request) => request.path == "/src", | |
74 (HttpRequest request, HttpResponse response) { | |
75 Expect.equals("value", request.headers.value("X-Request-Header")); | |
76 response.headers.set(HttpHeaders.LOCATION, | |
77 "http://127.0.0.1:${server.port}/target"); | |
78 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | |
79 response.outputStream.close(); | |
80 } | |
81 ); | |
82 server.addRequestHandler( | |
83 (HttpRequest request) => request.path == "/target", | |
84 (HttpRequest request, HttpResponse response) { | |
85 Expect.equals("value", request.headers.value("X-Request-Header")); | |
86 response.outputStream.close(); | |
87 } | |
88 ); | |
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 Expect.equals(10, request.contentLength); | |
114 request.inputStream.onData = request.inputStream.read; | |
115 request.inputStream.onClosed = () { | |
116 response.headers.set(HttpHeaders.LOCATION, | |
117 "http://127.0.0.1:${server.port}/303target"); | |
118 response.statusCode = HttpStatus.SEE_OTHER; | |
119 response.outputStream.close(); | |
120 }; | |
121 } | |
122 ); | |
123 server.addRequestHandler( | |
124 (HttpRequest request) => request.path == "/303target", | |
125 (HttpRequest request, HttpResponse response) { | |
126 Expect.equals("GET", request.method); | |
127 response.outputStream.close(); | |
128 } | |
129 ); | |
130 | |
131 // Setup redirect where we close the connection. | |
132 server.addRequestHandler( | |
133 (HttpRequest request) => request.path == "/closing", | |
134 (HttpRequest request, HttpResponse response) { | |
135 response.headers.set(HttpHeaders.LOCATION, | |
136 "http://127.0.0.1:${server.port}/"); | |
137 response.statusCode = HttpStatus.FOUND; | |
138 response.persistentConnection = false; | |
139 response.outputStream.close(); | |
140 } | |
141 ); | |
142 | |
143 return server; | |
144 } | |
145 | |
146 void checkRedirects(int redirectCount, HttpClientConnection conn) { | |
147 if (redirectCount < 2) { | |
148 Expect.isNull(conn.redirects); | |
149 } else { | |
150 Expect.equals(redirectCount - 1, conn.redirects.length); | |
151 for (int i = 0; i < redirectCount - 2; i++) { | |
152 Expect.equals(conn.redirects[i].location.path, "/${i + 2}"); | |
153 } | |
154 } | |
155 } | 179 } |
156 | 180 |
157 void testManualRedirect() { | 181 void testManualRedirect() { |
158 HttpServer server = setupServer(); | 182 setupServer().then((server) { |
159 HttpClient client = new HttpClient(); | 183 HttpClient client = new HttpClient(); |
160 | 184 |
161 int redirectCount = 0; | 185 int redirectCount = 0; |
162 HttpClientConnection conn = | 186 handleResponse(HttpClientResponse response) { |
163 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")); | 187 response.listen( |
164 conn.followRedirects = false; | 188 (_) => Expect.fail("Response data not expected"), |
165 conn.onResponse = (HttpClientResponse response) { | 189 onDone: () { |
166 response.inputStream.onData = response.inputStream.read; | 190 redirectCount++; |
167 response.inputStream.onClosed = () { | 191 if (redirectCount < 10) { |
168 redirectCount++; | 192 Expect.isTrue(response.isRedirect); |
169 if (redirectCount < 10) { | 193 checkRedirects(redirectCount, response); |
170 Expect.isTrue(response.isRedirect); | 194 response.redirect().then(handleResponse); |
171 checkRedirects(redirectCount, conn); | 195 } else { |
172 conn.redirect(); | 196 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); |
173 } else { | 197 server.close(); |
174 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | 198 client.close(); |
| 199 } |
| 200 }); |
| 201 } |
| 202 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")) |
| 203 .then((HttpClientRequest request) { |
| 204 request.followRedirects = false; |
| 205 return request.close(); |
| 206 }) |
| 207 .then(handleResponse); |
| 208 }); |
| 209 } |
| 210 |
| 211 void testManualRedirectWithHeaders() { |
| 212 setupServer().then((server) { |
| 213 HttpClient client = new HttpClient(); |
| 214 |
| 215 int redirectCount = 0; |
| 216 |
| 217 handleResponse(HttpClientResponse response) { |
| 218 response.listen( |
| 219 (_) => Expect.fail("Response data not expected"), |
| 220 onDone: () { |
| 221 redirectCount++; |
| 222 if (redirectCount < 2) { |
| 223 Expect.isTrue(response.isRedirect); |
| 224 response.redirect().then(handleResponse); |
| 225 } else { |
| 226 Expect.equals(HttpStatus.OK, response.statusCode); |
| 227 server.close(); |
| 228 client.close(); |
| 229 } |
| 230 }); |
| 231 } |
| 232 |
| 233 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")) |
| 234 .then((HttpClientRequest request) { |
| 235 request.followRedirects = false; |
| 236 request.headers.add("X-Request-Header", "value"); |
| 237 return request.close(); |
| 238 }).then(handleResponse); |
| 239 }); |
| 240 } |
| 241 |
| 242 void testAutoRedirect() { |
| 243 setupServer().then((server) { |
| 244 HttpClient client = new HttpClient(); |
| 245 |
| 246 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/redirect")) |
| 247 .then((HttpClientRequest request) { |
| 248 return request.close(); |
| 249 }) |
| 250 .then((HttpClientResponse response) { |
| 251 response.listen( |
| 252 (_) => Expect.fail("Response data not expected"), |
| 253 onDone: () { |
| 254 Expect.equals(1, response.redirects.length); |
| 255 server.close(); |
| 256 client.close(); |
| 257 }); |
| 258 }); |
| 259 }); |
| 260 } |
| 261 |
| 262 void testAutoRedirectWithHeaders() { |
| 263 setupServer().then((server) { |
| 264 HttpClient client = new HttpClient(); |
| 265 |
| 266 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")) |
| 267 .then((HttpClientRequest request) { |
| 268 request.headers.add("X-Request-Header", "value"); |
| 269 return request.close(); |
| 270 }) |
| 271 .then((HttpClientResponse response) { |
| 272 response.listen( |
| 273 (_) => Expect.fail("Response data not expected"), |
| 274 onDone: () { |
| 275 Expect.equals(1, response.redirects.length); |
| 276 server.close(); |
| 277 client.close(); |
| 278 }); |
| 279 }); |
| 280 }); |
| 281 } |
| 282 |
| 283 void testAutoRedirect301POST() { |
| 284 setupServer().then((server) { |
| 285 HttpClient client = new HttpClient(); |
| 286 |
| 287 client.postUrl(Uri.parse("http://127.0.0.1:${server.port}/301src")) |
| 288 .then((HttpClientRequest request) { |
| 289 return request.close(); |
| 290 }) |
| 291 .then((HttpClientResponse response) { |
| 292 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 293 response.listen( |
| 294 (_) => Expect.fail("Response data not expected"), |
| 295 onDone: () { |
| 296 Expect.equals(0, response.redirects.length); |
| 297 server.close(); |
| 298 client.close(); |
| 299 }); |
| 300 }); |
| 301 }); |
| 302 } |
| 303 |
| 304 void testAutoRedirect303POST() { |
| 305 setupServer().then((server) { |
| 306 HttpClient client = new HttpClient(); |
| 307 |
| 308 client.postUrl(Uri.parse("http://127.0.0.1:${server.port}/303src")) |
| 309 .then((HttpClientRequest request) { |
| 310 return request.close(); |
| 311 }) |
| 312 .then((HttpClientResponse response) { |
| 313 Expect.equals(HttpStatus.OK, response.statusCode); |
| 314 response.listen( |
| 315 (_) => Expect.fail("Response data not expected"), |
| 316 onDone: () { |
| 317 Expect.equals(1, response.redirects.length); |
| 318 server.close(); |
| 319 client.close(); |
| 320 }); |
| 321 }); |
| 322 }); |
| 323 } |
| 324 |
| 325 void testAutoRedirectLimit() { |
| 326 setupServer().then((server) { |
| 327 HttpClient client = new HttpClient(); |
| 328 |
| 329 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")) |
| 330 .then((HttpClientRequest request) => request.close()) |
| 331 .catchError((e) { |
| 332 Expect.equals(5, e.error.redirects.length); |
175 server.close(); | 333 server.close(); |
176 client.shutdown(); | 334 client.close(); |
177 } | 335 }, test: (e) => e is RedirectLimitExceededException); |
178 }; | 336 }); |
179 }; | 337 } |
180 } | 338 |
181 | 339 void testRedirectLoop() { |
182 void testManualRedirectWithHeaders() { | 340 setupServer().then((server) { |
183 HttpServer server = setupServer(); | 341 HttpClient client = new HttpClient(); |
184 HttpClient client = new HttpClient(); | 342 |
185 | 343 int redirectCount = 0; |
186 int redirectCount = 0; | 344 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/A")) |
187 HttpClientConnection conn = | 345 .then((HttpClientRequest request) => request.close()) |
188 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")); | 346 .catchError((e) { |
189 conn.followRedirects = false; | 347 Expect.equals(2, e.error.redirects.length); |
190 conn.onRequest = (HttpClientRequest request) { | |
191 request.headers.add("X-Request-Header", "value"); | |
192 request.outputStream.close(); | |
193 }; | |
194 conn.onResponse = (HttpClientResponse response) { | |
195 response.inputStream.onData = response.inputStream.read; | |
196 response.inputStream.onClosed = () { | |
197 redirectCount++; | |
198 if (redirectCount < 2) { | |
199 Expect.isTrue(response.isRedirect); | |
200 conn.redirect(); | |
201 } else { | |
202 Expect.equals(HttpStatus.OK, response.statusCode); | |
203 server.close(); | 348 server.close(); |
204 client.shutdown(); | 349 client.close(); |
205 } | 350 }, test: (e) => e is RedirectLoopException); |
206 }; | 351 }); |
207 }; | |
208 } | |
209 | |
210 void testAutoRedirect() { | |
211 HttpServer server = setupServer(); | |
212 HttpClient client = new HttpClient(); | |
213 | |
214 var requestCount = 0; | |
215 | |
216 void onRequest(HttpClientRequest request) { | |
217 requestCount++; | |
218 request.outputStream.close(); | |
219 } | |
220 | |
221 void onResponse(HttpClientResponse response) { | |
222 response.inputStream.onData = | |
223 () => Expect.fail("Response data not expected"); | |
224 response.inputStream.onClosed = () { | |
225 Expect.equals(1, requestCount); | |
226 server.close(); | |
227 client.shutdown(); | |
228 }; | |
229 }; | |
230 | |
231 HttpClientConnection conn = | |
232 client.getUrl( | |
233 Uri.parse("http://127.0.0.1:${server.port}/redirect")); | |
234 conn.onRequest = onRequest; | |
235 conn.onResponse = onResponse; | |
236 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | |
237 } | |
238 | |
239 void testAutoRedirectWithHeaders() { | |
240 HttpServer server = setupServer(); | |
241 HttpClient client = new HttpClient(); | |
242 | |
243 var requestCount = 0; | |
244 | |
245 void onRequest(HttpClientRequest request) { | |
246 requestCount++; | |
247 request.headers.add("X-Request-Header", "value"); | |
248 request.outputStream.close(); | |
249 }; | |
250 | |
251 void onResponse(HttpClientResponse response) { | |
252 response.inputStream.onData = | |
253 () => Expect.fail("Response data not expected"); | |
254 response.inputStream.onClosed = () { | |
255 Expect.equals(1, requestCount); | |
256 server.close(); | |
257 client.shutdown(); | |
258 }; | |
259 }; | |
260 | |
261 HttpClientConnection conn = | |
262 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/src")); | |
263 conn.onRequest = onRequest; | |
264 conn.onResponse = onResponse; | |
265 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | |
266 } | |
267 | |
268 void testAutoRedirect301POST() { | |
269 HttpServer server = setupServer(); | |
270 HttpClient client = new HttpClient(); | |
271 | |
272 var requestCount = 0; | |
273 | |
274 void onRequest(HttpClientRequest request) { | |
275 requestCount++; | |
276 request.outputStream.close(); | |
277 }; | |
278 | |
279 void onResponse(HttpClientResponse response) { | |
280 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | |
281 response.inputStream.onData = | |
282 () => Expect.fail("Response data not expected"); | |
283 response.inputStream.onClosed = () { | |
284 Expect.equals(1, requestCount); | |
285 server.close(); | |
286 client.shutdown(); | |
287 }; | |
288 }; | |
289 | |
290 HttpClientConnection conn = | |
291 client.postUrl( | |
292 Uri.parse("http://127.0.0.1:${server.port}/301src")); | |
293 conn.onRequest = onRequest; | |
294 conn.onResponse = onResponse; | |
295 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | |
296 } | |
297 | |
298 void testAutoRedirect303POST() { | |
299 HttpServer server = setupServer(); | |
300 HttpClient client = new HttpClient(); | |
301 | |
302 var requestCount = 0; | |
303 | |
304 void onRequest(HttpClientRequest request) { | |
305 requestCount++; | |
306 request.contentLength = 10; | |
307 request.outputStream.write(new List<int>.fixedLength(10, fill: 0)); | |
308 request.outputStream.close(); | |
309 }; | |
310 | |
311 void onResponse(HttpClientResponse response) { | |
312 Expect.equals(HttpStatus.OK, response.statusCode); | |
313 response.inputStream.onData = | |
314 () => Expect.fail("Response data not expected"); | |
315 response.inputStream.onClosed = () { | |
316 Expect.equals(1, requestCount); | |
317 server.close(); | |
318 client.shutdown(); | |
319 }; | |
320 }; | |
321 | |
322 HttpClientConnection conn = | |
323 client.postUrl( | |
324 Uri.parse("http://127.0.0.1:${server.port}/303src")); | |
325 conn.onRequest = onRequest; | |
326 conn.onResponse = onResponse; | |
327 conn.onError = (e) => Expect.fail("Error not expected ($e)"); | |
328 } | |
329 | |
330 void testAutoRedirectLimit() { | |
331 HttpServer server = setupServer(); | |
332 HttpClient client = new HttpClient(); | |
333 | |
334 HttpClientConnection conn = | |
335 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/1")); | |
336 conn.onResponse = (HttpClientResponse response) { | |
337 response.inputStream.onData = () => Expect.fail("Response not expected"); | |
338 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | |
339 }; | |
340 conn.onError = (e) { | |
341 Expect.isTrue(e is RedirectLimitExceededException); | |
342 Expect.equals(5, e.redirects.length); | |
343 server.close(); | |
344 client.shutdown(); | |
345 }; | |
346 } | |
347 | |
348 void testRedirectLoop() { | |
349 HttpServer server = setupServer(); | |
350 HttpClient client = new HttpClient(); | |
351 | |
352 int redirectCount = 0; | |
353 HttpClientConnection conn = | |
354 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/A")); | |
355 conn.onResponse = (HttpClientResponse response) { | |
356 response.inputStream.onData = () => Expect.fail("Response not expected"); | |
357 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | |
358 }; | |
359 conn.onError = (e) { | |
360 Expect.isTrue(e is RedirectLoopException); | |
361 Expect.equals(2, e.redirects.length); | |
362 server.close(); | |
363 client.shutdown(); | |
364 }; | |
365 } | 352 } |
366 | 353 |
367 void testRedirectClosingConnection() { | 354 void testRedirectClosingConnection() { |
368 HttpServer server = setupServer(); | 355 setupServer().then((server) { |
369 HttpClient client = new HttpClient(); | 356 HttpClient client = new HttpClient(); |
370 | 357 |
371 int redirectCount = 0; | 358 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/closing")) |
372 HttpClientConnection conn = | 359 .then((request) => request.close()) |
373 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/closing")); | 360 .then((response) { |
374 | 361 response.listen( |
375 conn.followRedirects = true; | 362 (_) {}, |
376 conn.onResponse = (HttpClientResponse response) { | 363 onDone: () { |
377 response.inputStream.onData = () => Expect.fail("Response not expected"); | 364 Expect.equals(1, response.redirects.length); |
378 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | 365 server.close(); |
379 }; | 366 client.close(); |
380 conn.onError = (e) { | 367 }); |
381 Expect.isTrue(e is RedirectException); | 368 }); |
382 Expect.isNull(e.redirects); | 369 }); |
383 server.close(); | |
384 client.shutdown(); | |
385 }; | |
386 } | 370 } |
387 | 371 |
388 main() { | 372 main() { |
389 testManualRedirect(); | 373 testManualRedirect(); |
390 testManualRedirectWithHeaders(); | 374 testManualRedirectWithHeaders(); |
391 testAutoRedirect(); | 375 testAutoRedirect(); |
392 testAutoRedirectWithHeaders(); | 376 testAutoRedirectWithHeaders(); |
393 testAutoRedirect301POST(); | 377 testAutoRedirect301POST(); |
394 testAutoRedirect303POST(); | 378 testAutoRedirect303POST(); |
395 testAutoRedirectLimit(); | 379 testAutoRedirectLimit(); |
396 testRedirectLoop(); | 380 testRedirectLoop(); |
397 testRedirectClosingConnection(); | 381 testRedirectClosingConnection(); |
398 } | 382 } |
OLD | NEW |