OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import "dart:isolate"; | 11 import "dart:isolate"; |
12 import "dart:io"; | 12 import "dart:io"; |
13 | 13 |
14 class TestServerMain { | 14 class TestServerMain { |
15 TestServerMain() | 15 TestServerMain() |
16 : _statusPort = new ReceivePort(), | 16 : _statusPort = new ReceivePort(), |
17 _serverPort = null { | 17 _serverPort = null; |
18 _serverPort = spawnFunction(startTestServer); | |
19 } | |
20 | 18 |
21 void setServerStartedHandler(void startedCallback(int port)) { | 19 void setServerStartedHandler(void startedCallback(int port)) { |
22 _startedCallback = startedCallback; | 20 _startedCallback = startedCallback; |
23 } | 21 } |
24 | 22 |
25 void start() { | 23 void start([bool chunkedEncoding = false]) { |
| 24 ReceivePort receivePort = new ReceivePort(); |
| 25 var remote = Isolate.spawn(startTestServer, receivePort.sendPort); |
| 26 receivePort.first.then((port) { |
| 27 _serverPort = port; |
| 28 |
| 29 if (chunkedEncoding) { |
| 30 // Send chunked encoding message to the server. |
| 31 port.send( |
| 32 [new TestServerCommand.chunkedEncoding(), _statusPort.sendPort]); |
| 33 } |
| 34 |
| 35 // Send server start message to the server. |
| 36 var command = new TestServerCommand.start(); |
| 37 port.send([command, _statusPort.sendPort]); |
| 38 }); |
| 39 |
26 // Handle status messages from the server. | 40 // Handle status messages from the server. |
27 _statusPort.receive((var status, SendPort replyTo) { | 41 _statusPort.listen((var status) { |
28 if (status.isStarted) { | 42 if (status.isStarted) { |
29 _startedCallback(status.port); | 43 _startedCallback(status.port); |
30 } | 44 } |
31 }); | 45 }); |
32 | 46 |
33 // Send server start message to the server. | |
34 var command = new TestServerCommand.start(); | |
35 _serverPort.send(command, _statusPort.toSendPort()); | |
36 } | 47 } |
37 | 48 |
38 void close() { | 49 void close() { |
39 // Send server stop message to the server. | 50 // Send server stop message to the server. |
40 _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort()); | 51 _serverPort.send([new TestServerCommand.stop(), _statusPort.sendPort]); |
41 _statusPort.close(); | 52 _statusPort.close(); |
42 } | 53 } |
43 | 54 |
44 void chunkedEncoding() { | |
45 // Send chunked encoding message to the server. | |
46 _serverPort.send( | |
47 new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort()); | |
48 } | |
49 | |
50 ReceivePort _statusPort; // Port for receiving messages from the server. | 55 ReceivePort _statusPort; // Port for receiving messages from the server. |
51 SendPort _serverPort; // Port for sending messages to the server. | 56 SendPort _serverPort; // Port for sending messages to the server. |
52 var _startedCallback; | 57 var _startedCallback; |
53 } | 58 } |
54 | 59 |
55 | 60 |
56 class TestServerCommand { | 61 class TestServerCommand { |
57 static const START = 0; | 62 static const START = 0; |
58 static const STOP = 1; | 63 static const STOP = 1; |
59 static const CHUNKED_ENCODING = 2; | 64 static const CHUNKED_ENCODING = 2; |
(...skipping 23 matching lines...) Expand all Loading... |
83 bool get isStopped => _state == STOPPED; | 88 bool get isStopped => _state == STOPPED; |
84 bool get isError => _state == ERROR; | 89 bool get isError => _state == ERROR; |
85 | 90 |
86 int get port => _port; | 91 int get port => _port; |
87 | 92 |
88 int _state; | 93 int _state; |
89 int _port; | 94 int _port; |
90 } | 95 } |
91 | 96 |
92 | 97 |
93 void startTestServer() { | 98 void startTestServer(SendPort replyTo) { |
94 var server = new TestServer(); | 99 var server = new TestServer(); |
95 server.init(); | 100 server.init(); |
96 port.receive(server.dispatch); | 101 replyTo.send(server.dispatchSendPort); |
97 } | 102 } |
98 | 103 |
99 | 104 |
100 class TestServer { | 105 class TestServer { |
101 // Echo the request content back to the response. | 106 // Echo the request content back to the response. |
102 void _echoHandler(HttpRequest request) { | 107 void _echoHandler(HttpRequest request) { |
103 var response = request.response; | 108 var response = request.response; |
104 Expect.equals("POST", request.method); | 109 Expect.equals("POST", request.method); |
105 response.contentLength = request.contentLength; | 110 response.contentLength = request.contentLength; |
106 request.pipe(response); | 111 request.pipe(response); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 response.close(); | 149 response.close(); |
145 } | 150 } |
146 | 151 |
147 void init() { | 152 void init() { |
148 // Setup request handlers. | 153 // Setup request handlers. |
149 _requestHandlers = new Map(); | 154 _requestHandlers = new Map(); |
150 _requestHandlers["/echo"] = _echoHandler; | 155 _requestHandlers["/echo"] = _echoHandler; |
151 _requestHandlers["/0123456789"] = _zeroToTenHandler; | 156 _requestHandlers["/0123456789"] = _zeroToTenHandler; |
152 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; | 157 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; |
153 _requestHandlers["/host"] = _hostHandler; | 158 _requestHandlers["/host"] = _hostHandler; |
| 159 _dispatchPort = new ReceivePort(); |
| 160 _dispatchPort.listen(dispatch); |
154 } | 161 } |
155 | 162 |
156 void dispatch(var message, SendPort replyTo) { | 163 SendPort get dispatchSendPort => _dispatchPort.sendPort; |
157 if (message.isStart) { | 164 |
| 165 void dispatch(var message) { |
| 166 TestServerCommand command = message[0]; |
| 167 SendPort replyTo = message[1]; |
| 168 if (command.isStart) { |
158 try { | 169 try { |
159 HttpServer.bind("127.0.0.1", 0).then((server) { | 170 HttpServer.bind("127.0.0.1", 0).then((server) { |
160 _server = server; | 171 _server = server; |
161 _server.listen(_requestReceivedHandler); | 172 _server.listen(_requestReceivedHandler); |
162 replyTo.send(new TestServerStatus.started(_server.port), null); | 173 replyTo.send(new TestServerStatus.started(_server.port), null); |
163 }); | 174 }); |
164 } catch (e) { | 175 } catch (e) { |
165 replyTo.send(new TestServerStatus.error(), null); | 176 replyTo.send(new TestServerStatus.error(), null); |
166 } | 177 } |
167 } else if (message.isStop) { | 178 } else if (command.isStop) { |
168 _server.close(); | 179 _server.close(); |
| 180 _dispatchPort.close(); |
169 port.close(); | 181 port.close(); |
170 replyTo.send(new TestServerStatus.stopped(), null); | 182 replyTo.send(new TestServerStatus.stopped(), null); |
171 } else if (message.isChunkedEncoding) { | 183 } else if (command.isChunkedEncoding) { |
172 _chunkedEncoding = true; | 184 _chunkedEncoding = true; |
173 } | 185 } |
174 } | 186 } |
175 | 187 |
176 void _requestReceivedHandler(HttpRequest request) { | 188 void _requestReceivedHandler(HttpRequest request) { |
177 var requestHandler =_requestHandlers[request.uri.path]; | 189 var requestHandler =_requestHandlers[request.uri.path]; |
178 if (requestHandler != null) { | 190 if (requestHandler != null) { |
179 requestHandler(request); | 191 requestHandler(request); |
180 } else { | 192 } else { |
181 _notFoundHandler(request); | 193 _notFoundHandler(request); |
182 } | 194 } |
183 } | 195 } |
184 | 196 |
185 HttpServer _server; // HTTP server instance. | 197 HttpServer _server; // HTTP server instance. |
| 198 ReceivePort _dispatchPort; |
186 Map _requestHandlers; | 199 Map _requestHandlers; |
187 bool _chunkedEncoding = false; | 200 bool _chunkedEncoding = false; |
188 } | 201 } |
189 | 202 |
190 void testStartStop() { | 203 void testStartStop() { |
191 TestServerMain testServerMain = new TestServerMain(); | 204 TestServerMain testServerMain = new TestServerMain(); |
192 testServerMain.setServerStartedHandler((int port) { | 205 testServerMain.setServerStartedHandler((int port) { |
193 testServerMain.close(); | 206 testServerMain.close(); |
194 }); | 207 }); |
195 testServerMain.start(); | 208 testServerMain.start(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 testServerMain.close(); | 265 testServerMain.close(); |
253 } | 266 } |
254 }); | 267 }); |
255 }); | 268 }); |
256 } | 269 } |
257 | 270 |
258 sendRequest(); | 271 sendRequest(); |
259 } | 272 } |
260 | 273 |
261 testServerMain.setServerStartedHandler(runTest); | 274 testServerMain.setServerStartedHandler(runTest); |
262 if (chunkedEncoding) { | 275 testServerMain.start(chunkedEncoding); |
263 testServerMain.chunkedEncoding(); | |
264 } | |
265 testServerMain.start(); | |
266 } | 276 } |
267 | 277 |
268 void test404() { | 278 void test404() { |
269 TestServerMain testServerMain = new TestServerMain(); | 279 TestServerMain testServerMain = new TestServerMain(); |
270 testServerMain.setServerStartedHandler((int port) { | 280 testServerMain.setServerStartedHandler((int port) { |
271 HttpClient httpClient = new HttpClient(); | 281 HttpClient httpClient = new HttpClient(); |
272 httpClient.get("127.0.0.1", port, "/thisisnotfound") | 282 httpClient.get("127.0.0.1", port, "/thisisnotfound") |
273 .then((request) => request.close()) | 283 .then((request) => request.close()) |
274 .then((response) { | 284 .then((response) { |
275 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); | 285 Expect.equals(HttpStatus.NOT_FOUND, response.statusCode); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 } | 321 } |
312 | 322 |
313 void main() { | 323 void main() { |
314 testStartStop(); | 324 testStartStop(); |
315 testGET(); | 325 testGET(); |
316 testPOST(true); | 326 testPOST(true); |
317 testPOST(false); | 327 testPOST(false); |
318 test404(); | 328 test404(); |
319 testReasonPhrase(); | 329 testReasonPhrase(); |
320 } | 330 } |
OLD | NEW |