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) { | |
kasperl
2012/08/03 05:20:27
port.receive(server.dispatch)?
| |
96 server.dispatch(message, replyTo); | |
97 }); | |
98 } | |
99 | |
100 | |
101 class TestServer { | |
95 // Echo the request content back to the response. | 102 // Echo the request content back to the response. |
96 void _echoHandler(HttpRequest request, HttpResponse response) { | 103 void _echoHandler(HttpRequest request, HttpResponse response) { |
97 Expect.equals("POST", request.method); | 104 Expect.equals("POST", request.method); |
98 response.contentLength = request.contentLength; | 105 response.contentLength = request.contentLength; |
99 request.inputStream.pipe(response.outputStream); | 106 request.inputStream.pipe(response.outputStream); |
100 } | 107 } |
101 | 108 |
102 // Echo the request content back to the response. | 109 // Echo the request content back to the response. |
103 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { | 110 void _zeroToTenHandler(HttpRequest request, HttpResponse response) { |
104 Expect.equals("GET", request.method); | 111 Expect.equals("GET", request.method); |
(...skipping 22 matching lines...) Expand all Loading... | |
127 // Check the "Host" header. | 134 // Check the "Host" header. |
128 void _hostHandler(HttpRequest request, HttpResponse response) { | 135 void _hostHandler(HttpRequest request, HttpResponse response) { |
129 Expect.equals(1, request.headers["Host"].length); | 136 Expect.equals(1, request.headers["Host"].length); |
130 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); | 137 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); |
131 Expect.equals("www.dartlang.org", request.headers.host); | 138 Expect.equals("www.dartlang.org", request.headers.host); |
132 Expect.equals(1234, request.headers.port); | 139 Expect.equals(1234, request.headers.port); |
133 response.statusCode = HttpStatus.OK; | 140 response.statusCode = HttpStatus.OK; |
134 response.outputStream.close(); | 141 response.outputStream.close(); |
135 } | 142 } |
136 | 143 |
137 void main() { | 144 void init() { |
138 // Setup request handlers. | 145 // Setup request handlers. |
139 _requestHandlers = new Map(); | 146 _requestHandlers = new Map(); |
140 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | 147 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
141 _echoHandler(request, response); | 148 _echoHandler(request, response); |
142 }; | 149 }; |
143 _requestHandlers["/0123456789"] = | 150 _requestHandlers["/0123456789"] = |
144 (HttpRequest request, HttpResponse response) { | 151 (HttpRequest request, HttpResponse response) { |
145 _zeroToTenHandler(request, response); | 152 _zeroToTenHandler(request, response); |
146 }; | 153 }; |
147 _requestHandlers["/reasonformoving"] = | 154 _requestHandlers["/reasonformoving"] = |
148 (HttpRequest request, HttpResponse response) { | 155 (HttpRequest request, HttpResponse response) { |
149 _reasonForMovingHandler(request, response); | 156 _reasonForMovingHandler(request, response); |
150 }; | 157 }; |
151 _requestHandlers["/host"] = | 158 _requestHandlers["/host"] = |
152 (HttpRequest request, HttpResponse response) { | 159 (HttpRequest request, HttpResponse response) { |
153 _hostHandler(request, response); | 160 _hostHandler(request, response); |
154 }; | 161 }; |
162 } | |
155 | 163 |
156 this.port.receive((var message, SendPort replyTo) { | 164 void dispatch(var message, SendPort replyTo) { |
157 if (message.isStart) { | 165 if (message.isStart) { |
158 _server = new HttpServer(); | 166 _server = new HttpServer(); |
159 try { | 167 try { |
160 _server.listen("127.0.0.1", 0); | 168 _server.listen("127.0.0.1", 0); |
161 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | 169 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { |
162 _requestReceivedHandler(req, rsp); | 170 _requestReceivedHandler(req, rsp); |
163 }; | 171 }; |
164 replyTo.send(new TestServerStatus.started(_server.port), null); | 172 replyTo.send(new TestServerStatus.started(_server.port), null); |
165 } catch (var e) { | 173 } catch (var e) { |
166 replyTo.send(new TestServerStatus.error(), null); | 174 replyTo.send(new TestServerStatus.error(), null); |
167 } | |
168 } else if (message.isStop) { | |
169 _server.close(); | |
170 this.port.close(); | |
171 replyTo.send(new TestServerStatus.stopped(), null); | |
172 } else if (message.isChunkedEncoding) { | |
173 _chunkedEncoding = true; | |
174 } | 175 } |
175 }); | 176 } else if (message.isStop) { |
177 _server.close(); | |
178 port.close(); | |
179 replyTo.send(new TestServerStatus.stopped(), null); | |
180 } else if (message.isChunkedEncoding) { | |
181 _chunkedEncoding = true; | |
182 } | |
176 } | 183 } |
177 | 184 |
178 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { | 185 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { |
179 var requestHandler =_requestHandlers[request.path]; | 186 var requestHandler =_requestHandlers[request.path]; |
180 if (requestHandler != null) { | 187 if (requestHandler != null) { |
181 requestHandler(request, response); | 188 requestHandler(request, response); |
182 } else { | 189 } else { |
183 _notFoundHandler(request, response); | 190 _notFoundHandler(request, response); |
184 } | 191 } |
185 } | 192 } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
301 } | 308 } |
302 | 309 |
303 void main() { | 310 void main() { |
304 testStartStop(); | 311 testStartStop(); |
305 testGET(); | 312 testGET(); |
306 testPOST(true); | 313 testPOST(true); |
307 testPOST(false); | 314 testPOST(false); |
308 test404(); | 315 test404(); |
309 testReasonPhrase(); | 316 testReasonPhrase(); |
310 } | 317 } |
OLD | NEW |