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