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 #import("dart:io"); | 5 #import("dart:io"); |
6 #import("dart:uri"); | 6 #import("dart:uri"); |
7 | 7 |
8 class Server { | 8 class Server { |
9 HttpServer server; | 9 HttpServer server; |
10 int proxyHops; | 10 int proxyHops; |
11 int requestCount = 0; | 11 int requestCount = 0; |
12 | 12 |
13 Server(this.proxyHops) : server = new HttpServer(); | 13 Server(this.proxyHops) : server = new HttpServer(); |
14 | 14 |
15 void start() { | 15 void start() { |
16 server.listen("127.0.0.1", 0, 5); | 16 server.listen("127.0.0.1", 0); |
17 server.defaultRequestHandler = | 17 server.defaultRequestHandler = |
18 (HttpRequest request, HttpResponse response) { | 18 (HttpRequest request, HttpResponse response) { |
19 requestCount++; | 19 requestCount++; |
20 if (proxyHops > 0) { | 20 if (proxyHops > 0) { |
21 Expect.isNotNull(request.headers[HttpHeaders.VIA]); | 21 Expect.isNotNull(request.headers[HttpHeaders.VIA]); |
22 Expect.equals(1, request.headers[HttpHeaders.VIA].length); | 22 Expect.equals(1, request.headers[HttpHeaders.VIA].length); |
23 Expect.equals( | 23 Expect.equals( |
24 proxyHops, | 24 proxyHops, |
25 request.headers[HttpHeaders.VIA][0].split(",").length); | 25 request.headers[HttpHeaders.VIA][0].split(",").length); |
26 } else { | 26 } else { |
27 Expect.isNull(request.headers[HttpHeaders.VIA]); | 27 Expect.isNull(request.headers[HttpHeaders.VIA]); |
28 } | 28 } |
29 StringInputStream stream = new StringInputStream(request.inputStream); | 29 StringInputStream stream = new StringInputStream(request.inputStream); |
30 StringBuffer body = new StringBuffer(); | 30 StringBuffer body = new StringBuffer(); |
31 stream.onData = () => body.add(stream.read()); | 31 stream.onData = () => body.add(stream.read()); |
32 stream.onClosed = () { | 32 stream.onClosed = () { |
33 String path = request.path.substring(1); | 33 String path = request.path.substring(1); |
34 String content = "$path$path$path"; | 34 String content = "$path$path$path"; |
35 Expect.equals(content, body.toString()); | 35 Expect.equals(content, body.toString()); |
36 response.outputStream.writeString(request.path); | 36 response.outputStream.writeString(request.path); |
37 response.outputStream.close(); | 37 response.outputStream.close(); |
38 }; | 38 }; |
39 }; | 39 }; |
40 } | 40 } |
41 | 41 |
42 void shutdown() { | 42 void shutdown() { |
43 server.close(); | 43 server.close(); |
44 } | 44 } |
45 | 45 |
46 int get port => server.port; | 46 int get port => server.port; |
47 } | 47 } |
48 | 48 |
49 Server setupServer(int proxyHops) { | 49 Server setupServer(int proxyHops) { |
50 Server server = new Server(proxyHops); | 50 Server server = new Server(proxyHops); |
51 server.start(); | 51 server.start(); |
52 return server; | 52 return server; |
53 } | 53 } |
54 | 54 |
55 class ProxyServer { | 55 class ProxyServer { |
56 HttpServer server; | 56 HttpServer server; |
57 HttpClient client; | 57 HttpClient client; |
58 int requestCount = 0; | 58 int requestCount = 0; |
59 | 59 |
60 ProxyServer() : server = new HttpServer(), client = new HttpClient(); | 60 ProxyServer() : server = new HttpServer(), client = new HttpClient(); |
61 | 61 |
62 void start() { | 62 void start() { |
63 server.listen("127.0.0.1", 0, 5); | 63 server.listen("127.0.0.1", 0); |
64 server.defaultRequestHandler = | 64 server.defaultRequestHandler = |
65 (HttpRequest request, HttpResponse response) { | 65 (HttpRequest request, HttpResponse response) { |
66 requestCount++; | 66 requestCount++; |
67 // Open the connection from the proxy. | 67 // Open the connection from the proxy. |
68 HttpClientConnection conn = | 68 HttpClientConnection conn = |
69 client.openUrl(request.method, new Uri.fromString(request.path)); | 69 client.openUrl(request.method, new Uri.fromString(request.path)); |
70 conn.onRequest = (HttpClientRequest clientRequest) { | 70 conn.onRequest = (HttpClientRequest clientRequest) { |
71 // Forward all headers. | 71 // Forward all headers. |
72 request.headers.forEach((String name, List<String> values) { | 72 request.headers.forEach((String name, List<String> values) { |
73 values.forEach((String value) { | 73 values.forEach((String value) { |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 290 |
291 main() { | 291 main() { |
292 testInvalidProxy(); | 292 testInvalidProxy(); |
293 testDirectProxy(); | 293 testDirectProxy(); |
294 testProxy(); | 294 testProxy(); |
295 testProxyChain(); | 295 testProxyChain(); |
296 // This test is not normally run. It can be used for locally testing | 296 // This test is not normally run. It can be used for locally testing |
297 // with a real proxy server (e.g. Apache). | 297 // with a real proxy server (e.g. Apache). |
298 // testRealProxy(); | 298 // testRealProxy(); |
299 } | 299 } |
OLD | NEW |