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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 #import("dart:isolate"); | 10 #import("dart:isolate"); |
11 #import("dart:io"); | 11 #import("dart:io"); |
12 | 12 |
13 class TestServerMain { | 13 class TestServerMain { |
14 TestServerMain() | 14 TestServerMain() |
15 : _statusPort = new ReceivePort(), | 15 : _statusPort = new ReceivePort(), |
16 _serverPort = null { | 16 _serverPort = null { |
17 new TestServer().spawn().then((SendPort port) { | 17 _serverPort = spawnFunction(startTestServer); |
18 _serverPort = port; | |
19 }); | |
20 } | 18 } |
21 | 19 |
22 void setServerStartedHandler(void startedCallback(int port)) { | 20 void setServerStartedHandler(void startedCallback(int port)) { |
23 _startedCallback = startedCallback; | 21 _startedCallback = startedCallback; |
24 } | 22 } |
25 | 23 |
26 void start() { | 24 void start() { |
27 // Handle status messages from the server. | 25 // Handle status messages from the server. |
28 _statusPort.receive((var status, SendPort replyTo) { | 26 _statusPort.receive((var status, SendPort replyTo) { |
29 if (status.isStarted) { | 27 if (status.isStarted) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 bool get isStopped() => _state == STOPPED; | 82 bool get isStopped() => _state == STOPPED; |
85 bool get isError() => _state == ERROR; | 83 bool get isError() => _state == ERROR; |
86 | 84 |
87 int get port() => _port; | 85 int get port() => _port; |
88 | 86 |
89 int _state; | 87 int _state; |
90 int _port; | 88 int _port; |
91 } | 89 } |
92 | 90 |
93 | 91 |
94 class TestServer extends Isolate { | 92 void startTestServer() { |
93 var server = new TestServer(); | |
94 server.init(); | |
95 port.receive((message, replyTo) { | |
96 server.dispatch(message, replyTo); | |
kasperl
2012/08/03 05:20:27
port.receive(server.dispatch)?
| |
97 }); | |
98 } | |
99 | |
100 | |
101 class TestServer { | |
95 // Return a 404. | 102 // Return a 404. |
96 void _notFoundHandler(HttpRequest request, HttpResponse response) { | 103 void _notFoundHandler(HttpRequest request, HttpResponse response) { |
97 response.statusCode = HttpStatus.NOT_FOUND; | 104 response.statusCode = HttpStatus.NOT_FOUND; |
98 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | 105 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
99 response.outputStream.writeString("Page not found"); | 106 response.outputStream.writeString("Page not found"); |
100 response.outputStream.close(); | 107 response.outputStream.close(); |
101 } | 108 } |
102 | 109 |
103 // Check the "Host" header. | 110 // Check the "Host" header. |
104 void _hostHandler(HttpRequest request, HttpResponse response) { | 111 void _hostHandler(HttpRequest request, HttpResponse response) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 response.cookies.add(cookie2); | 173 response.cookies.add(cookie2); |
167 response.outputStream.close(); | 174 response.outputStream.close(); |
168 } | 175 } |
169 | 176 |
170 void _cookie2Handler(HttpRequest request, HttpResponse response) { | 177 void _cookie2Handler(HttpRequest request, HttpResponse response) { |
171 // Two cookies passed with this request. | 178 // Two cookies passed with this request. |
172 Expect.equals(2, request.cookies.length); | 179 Expect.equals(2, request.cookies.length); |
173 response.outputStream.close(); | 180 response.outputStream.close(); |
174 } | 181 } |
175 | 182 |
176 void main() { | 183 void init() { |
177 // Setup request handlers. | 184 // Setup request handlers. |
178 _requestHandlers = new Map(); | 185 _requestHandlers = new Map(); |
179 _requestHandlers["/host"] = | 186 _requestHandlers["/host"] = |
180 (HttpRequest request, HttpResponse response) { | 187 (HttpRequest request, HttpResponse response) { |
181 _hostHandler(request, response); | 188 _hostHandler(request, response); |
182 }; | 189 }; |
183 _requestHandlers["/expires1"] = | 190 _requestHandlers["/expires1"] = |
184 (HttpRequest request, HttpResponse response) { | 191 (HttpRequest request, HttpResponse response) { |
185 _expires1Handler(request, response); | 192 _expires1Handler(request, response); |
186 }; | 193 }; |
(...skipping 10 matching lines...) Expand all Loading... | |
197 _contentType2Handler(request, response); | 204 _contentType2Handler(request, response); |
198 }; | 205 }; |
199 _requestHandlers["/cookie1"] = | 206 _requestHandlers["/cookie1"] = |
200 (HttpRequest request, HttpResponse response) { | 207 (HttpRequest request, HttpResponse response) { |
201 _cookie1Handler(request, response); | 208 _cookie1Handler(request, response); |
202 }; | 209 }; |
203 _requestHandlers["/cookie2"] = | 210 _requestHandlers["/cookie2"] = |
204 (HttpRequest request, HttpResponse response) { | 211 (HttpRequest request, HttpResponse response) { |
205 _cookie2Handler(request, response); | 212 _cookie2Handler(request, response); |
206 }; | 213 }; |
214 } | |
207 | 215 |
208 this.port.receive((var message, SendPort replyTo) { | 216 void dispatch(message, replyTo) { |
209 if (message.isStart) { | 217 if (message.isStart) { |
210 _server = new HttpServer(); | 218 _server = new HttpServer(); |
211 try { | 219 try { |
212 _server.listen("127.0.0.1", 0); | 220 _server.listen("127.0.0.1", 0); |
213 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | 221 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { |
214 _requestReceivedHandler(req, rsp); | 222 _requestReceivedHandler(req, rsp); |
215 }; | 223 }; |
216 replyTo.send(new TestServerStatus.started(_server.port), null); | 224 replyTo.send(new TestServerStatus.started(_server.port), null); |
217 } catch (var e) { | 225 } catch (var e) { |
218 replyTo.send(new TestServerStatus.error(), null); | 226 replyTo.send(new TestServerStatus.error(), null); |
219 } | |
220 } else if (message.isStop) { | |
221 _server.close(); | |
222 this.port.close(); | |
223 replyTo.send(new TestServerStatus.stopped(), null); | |
224 } else if (message.isChunkedEncoding) { | |
225 _chunkedEncoding = true; | |
226 } | 227 } |
227 }); | 228 } else if (message.isStop) { |
229 _server.close(); | |
230 port.close(); | |
231 replyTo.send(new TestServerStatus.stopped(), null); | |
232 } else if (message.isChunkedEncoding) { | |
233 _chunkedEncoding = true; | |
234 } | |
228 } | 235 } |
229 | 236 |
230 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { | 237 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { |
231 var requestHandler =_requestHandlers[request.path]; | 238 var requestHandler =_requestHandlers[request.path]; |
232 if (requestHandler != null) { | 239 if (requestHandler != null) { |
233 requestHandler(request, response); | 240 requestHandler(request, response); |
234 } else { | 241 } else { |
235 _notFoundHandler(request, response); | 242 _notFoundHandler(request, response); |
236 } | 243 } |
237 } | 244 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 }); | 406 }); |
400 testServerMain.start(); | 407 testServerMain.start(); |
401 } | 408 } |
402 | 409 |
403 void main() { | 410 void main() { |
404 testHost(); | 411 testHost(); |
405 testExpires(); | 412 testExpires(); |
406 testContentType(); | 413 testContentType(); |
407 testCookies(); | 414 testCookies(); |
408 } | 415 } |
OLD | NEW |