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(); |
11 server.listen("127.0.0.1", 0, backlog: 5); | 11 server.listen("127.0.0.1", 0, backlog: 5); |
12 | 12 |
13 void addRedirectHandler(int number, int statusCode) { | 13 void addRedirectHandler(int number, int statusCode) { |
14 server.addRequestHandler( | 14 server.addRequestHandler( |
15 (HttpRequest request) => request.path == "/$number", | 15 (HttpRequest request) => request.path == "/$number", |
16 (HttpRequest request, HttpResponse response) { | 16 (HttpRequest request, HttpResponse response) { |
17 response.headers.set(HttpHeaders.LOCATION, | 17 response.headers.set(HttpHeaders.LOCATION, |
18 "http://127.0.0.1:${server.port}/${number + 1}"); | 18 "http://127.0.0.1:${server.port}/${number + 1}"); |
19 response.statusCode = statusCode; | 19 response.statusCode = statusCode; |
20 response.outputStream.close(); | 20 response.outputStream.close(); |
21 }); | 21 }); |
22 } | 22 } |
23 | 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 |
24 // Setup redirect chain. | 41 // Setup redirect chain. |
25 int n = 1; | 42 int n = 1; |
26 addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY); | 43 addRedirectHandler(n++, HttpStatus.MOVED_PERMANENTLY); |
27 addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY); | 44 addRedirectHandler(n++, HttpStatus.MOVED_TEMPORARILY); |
28 addRedirectHandler(n++, HttpStatus.SEE_OTHER); | 45 addRedirectHandler(n++, HttpStatus.SEE_OTHER); |
29 addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT); | 46 addRedirectHandler(n++, HttpStatus.TEMPORARY_REDIRECT); |
30 for (int i = n; i < 10; i++) { | 47 for (int i = n; i < 10; i++) { |
31 addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY); | 48 addRedirectHandler(i, HttpStatus.MOVED_PERMANENTLY); |
32 } | 49 } |
33 | 50 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 client.shutdown(); | 104 client.shutdown(); |
88 } | 105 } |
89 }; | 106 }; |
90 }; | 107 }; |
91 } | 108 } |
92 | 109 |
93 void testAutoRedirect() { | 110 void testAutoRedirect() { |
94 HttpServer server = setupServer(); | 111 HttpServer server = setupServer(); |
95 HttpClient client = new HttpClient(); | 112 HttpClient client = new HttpClient(); |
96 | 113 |
| 114 var requestCount = 0; |
| 115 |
| 116 void onRequest(HttpClientRequest request) { |
| 117 requestCount++; |
| 118 request.outputStream.close(); |
| 119 } |
| 120 |
| 121 void onResponse(HttpClientResponse response) { |
| 122 response.inputStream.onData = |
| 123 () => Expect.fail("Response data not expected"); |
| 124 response.inputStream.onClosed = () { |
| 125 Expect.equals(1, requestCount); |
| 126 server.close(); |
| 127 client.shutdown(); |
| 128 }; |
| 129 }; |
| 130 |
| 131 HttpClientConnection conn = |
| 132 client.getUrl( |
| 133 new Uri.fromString("http://127.0.0.1:${server.port}/redirect")); |
| 134 conn.onRequest = onRequest; |
| 135 conn.onResponse = onResponse; |
| 136 conn.onError = (e) => Expect.fail("Error not expected ($e)"); |
| 137 } |
| 138 |
| 139 void testAutoRedirectLimit() { |
| 140 HttpServer server = setupServer(); |
| 141 HttpClient client = new HttpClient(); |
| 142 |
97 HttpClientConnection conn = | 143 HttpClientConnection conn = |
98 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/1")); | 144 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/1")); |
99 conn.onResponse = (HttpClientResponse response) { | 145 conn.onResponse = (HttpClientResponse response) { |
100 response.inputStream.onData = () => Expect.fail("Response not expected"); | 146 response.inputStream.onData = () => Expect.fail("Response not expected"); |
101 response.inputStream.onClosed = () => Expect.fail("Response not expected"); | 147 response.inputStream.onClosed = () => Expect.fail("Response not expected"); |
102 }; | 148 }; |
103 conn.onError = (e) { | 149 conn.onError = (e) { |
104 Expect.isTrue(e is RedirectLimitExceededException); | 150 Expect.isTrue(e is RedirectLimitExceededException); |
105 Expect.equals(5, e.redirects.length); | 151 Expect.equals(5, e.redirects.length); |
106 server.close(); | 152 server.close(); |
(...skipping 16 matching lines...) Expand all Loading... |
123 Expect.isTrue(e is RedirectLoopException); | 169 Expect.isTrue(e is RedirectLoopException); |
124 Expect.equals(2, e.redirects.length); | 170 Expect.equals(2, e.redirects.length); |
125 server.close(); | 171 server.close(); |
126 client.shutdown(); | 172 client.shutdown(); |
127 }; | 173 }; |
128 } | 174 } |
129 | 175 |
130 main() { | 176 main() { |
131 testManualRedirect(); | 177 testManualRedirect(); |
132 testAutoRedirect(); | 178 testAutoRedirect(); |
| 179 testAutoRedirectRequestBody(); |
| 180 testAutoRedirectLimit(); |
133 testRedirectLoop(); | 181 testRedirectLoop(); |
134 } | 182 } |
OLD | NEW |