OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 | |
6 import 'dart:io'; | |
7 import 'dart:isolate'; | |
8 | |
9 class Handler1 { | |
10 void onRequest(HttpRequest request, HttpResponse response) { | |
11 response.outputStream.writeString("Handler 1"); | |
12 response.outputStream.close(); | |
13 } | |
14 } | |
15 | |
16 class Handler2 { | |
17 void onRequest(HttpRequest request, HttpResponse response) { | |
18 response.outputStream.writeString("Handler 2"); | |
19 response.outputStream.close(); | |
20 } | |
21 } | |
22 | |
23 class French404Handler { | |
24 void onRequest(HttpRequest request, HttpResponse response) { | |
25 response.statusCode = HttpStatus.NOT_FOUND; | |
26 response.reasonPhrase = "Non Trouvé"; | |
27 response.outputStream.close(); | |
28 } | |
29 } | |
30 | |
31 class Server { | |
32 Server() { | |
33 server = new HttpServer(); | |
34 server.listen("127.0.0.1", 0); | |
35 port = server.port; | |
36 server.onError = (e) { | |
37 Expect.fail("No server errors expected: $e"); | |
38 }; | |
39 } | |
40 | |
41 void addHandler(Function matcher, handler) { | |
42 if (handler is Function) { | |
43 server.addRequestHandler(matcher, handler); | |
44 } else { | |
45 server.addRequestHandler(matcher, handler.onRequest); | |
46 } | |
47 } | |
48 | |
49 void set defaultHandler(handler) { | |
50 if (handler is Function) { | |
51 server.defaultRequestHandler = handler; | |
52 } else { | |
53 server.defaultRequestHandler = handler.onRequest; | |
54 } | |
55 } | |
56 | |
57 void close() { | |
58 server.close(); | |
59 } | |
60 | |
61 int port; | |
62 HttpServer server; | |
63 } | |
64 | |
65 void testDefaultHandler() { | |
66 Server server = new Server(); | |
67 HttpClient client = new HttpClient(); | |
68 | |
69 void done() { | |
70 server.close(); | |
71 client.shutdown(); | |
72 } | |
73 | |
74 void error(e) { | |
75 Expect.fail("No client error expected $e"); | |
76 done(); | |
77 }; | |
78 | |
79 void german404(HttpRequest request, HttpResponse response) { | |
80 response.statusCode = HttpStatus.NOT_FOUND; | |
81 response.reasonPhrase = "Nicht Gefunden"; | |
82 response.outputStream.close(); | |
83 } | |
84 | |
85 // Test the standard default handler. | |
86 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
87 conn.onResponse = (HttpClientResponse response) { | |
88 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | |
89 Expect.equals("Not Found", response.reasonPhrase); | |
90 | |
91 // Install a default handler. | |
92 server.defaultHandler = german404; | |
93 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
94 conn.onResponse = (HttpClientResponse response) { | |
95 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | |
96 Expect.equals("Nicht Gefunden", response.reasonPhrase); | |
97 | |
98 // Install another default handler. | |
99 server.defaultHandler = new French404Handler(); | |
100 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
101 conn.onResponse = (HttpClientResponse response) { | |
102 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | |
103 Expect.equals("Non Trouvé", response.reasonPhrase); | |
104 response.inputStream.onClosed = done; | |
105 }; | |
106 conn.onError = error; | |
107 }; | |
108 conn.onError = error; | |
109 }; | |
110 conn.onError = error; | |
111 } | |
112 | |
113 void testHandlers() { | |
114 Server server = new Server(); | |
115 HttpClient client = new HttpClient(); | |
116 int requests = 0; | |
117 int doneCount = 0; | |
118 | |
119 void done() { | |
120 doneCount++; | |
121 if (doneCount == requests) { | |
122 server.close(); | |
123 client.shutdown(); | |
124 } | |
125 } | |
126 | |
127 void error(e) { | |
128 Expect.fail("No client error expected $e"); | |
129 done(); | |
130 }; | |
131 | |
132 void handler3(HttpRequest request, HttpResponse response) { | |
133 response.statusCode = HttpStatus.OK; | |
134 response.outputStream.writeString("Handler 3"); | |
135 response.outputStream.close(); | |
136 } | |
137 | |
138 void handler4(HttpRequest request, HttpResponse response) { | |
139 response.statusCode = HttpStatus.OK; | |
140 response.outputStream.writeString("Handler 4"); | |
141 response.outputStream.close(); | |
142 } | |
143 | |
144 void checkBody(HttpClientResponse response, String expected) { | |
145 StringBuffer sb = new StringBuffer(); | |
146 StringInputStream stream = new StringInputStream(response.inputStream); | |
147 stream.onData = () { | |
148 sb.add(stream.read()); | |
149 }; | |
150 stream.onClosed = () { | |
151 Expect.equals(expected, sb.toString()); | |
152 done(); | |
153 }; | |
154 } | |
155 | |
156 server.addHandler( | |
157 (request) => request.path.startsWith("/xxx/yyy/"), new Handler1()); | |
158 server.addHandler( | |
159 (request) => new RegExp("^/xxx").hasMatch(request.path), new Handler2()); | |
160 server.addHandler( | |
161 (request) => request.path == "/yyy.dat", handler3); | |
162 server.addHandler( | |
163 (request) => request.path.endsWith(".dat"), handler4); | |
164 | |
165 void testRequest(String path, int expectedStatus, String expectedBody) { | |
166 HttpClientConnection conn = | |
167 client.get("127.0.0.1", server.port, path); | |
168 requests++; | |
169 conn.onResponse = (HttpClientResponse response) { | |
170 Expect.equals(expectedStatus, response.statusCode); | |
171 checkBody(response, expectedBody); | |
172 }; | |
173 conn.onError = error; | |
174 } | |
175 | |
176 testRequest("/xxx/yyy/zzz", HttpStatus.OK, "Handler 1"); | |
177 testRequest("/xxx/zzz", HttpStatus.OK, "Handler 2"); | |
178 testRequest("/yyy.dat", HttpStatus.OK, "Handler 3"); | |
179 testRequest("/abc.dat", HttpStatus.OK, "Handler 4"); | |
180 testRequest("/abcdat", HttpStatus.NOT_FOUND, ""); | |
181 testRequest("/xxx.dat", HttpStatus.OK, "Handler 2"); | |
182 } | |
183 | |
184 main() { | |
185 testDefaultHandler(); | |
186 testHandlers(); | |
187 } | |
OLD | NEW |