OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
zra
2017/04/19 22:42:43
2017
bkonyi
2017/04/20 14:40:48
Done.
| |
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 import "dart:async"; | |
6 import "dart:isolate"; | |
7 import "dart:io"; | |
8 import "package:sync_http/sync_http.dart"; | |
9 import "package:test/test.dart"; | |
10 | |
11 typedef void ServerCallback(int port); | |
12 | |
13 class TestServerMain { | |
14 TestServerMain() : _statusPort = new ReceivePort(); | |
15 | |
16 ReceivePort _statusPort; // Port for receiving messages from the server. | |
17 SendPort _serverPort; // Port for sending messages to the server. | |
18 ServerCallback _startedCallback; | |
19 | |
20 void setServerStartedHandler(ServerCallback startedCallback) { | |
21 _startedCallback = startedCallback; | |
22 } | |
23 | |
24 void start() { | |
25 ReceivePort receivePort = new ReceivePort(); | |
26 Isolate.spawn(startTestServer, receivePort.sendPort); | |
27 receivePort.first.then((port) { | |
28 _serverPort = port; | |
29 | |
30 // Send server start message to the server. | |
31 var command = new TestServerCommand.start(); | |
32 port.send([command, _statusPort.sendPort]); | |
33 }); | |
34 | |
35 // Handle status messages from the server. | |
36 _statusPort.listen((var status) { | |
37 if (status.isStarted) { | |
38 _startedCallback(status.port); | |
39 } | |
40 }); | |
41 } | |
42 | |
43 void close() { | |
44 // Send server stop message to the server. | |
45 _serverPort.send([new TestServerCommand.stop(), _statusPort.sendPort]); | |
46 _statusPort.close(); | |
47 } | |
48 } | |
49 | |
50 enum TestServerCommandState { | |
51 start, | |
52 stop, | |
53 } | |
54 | |
55 class TestServerCommand { | |
56 TestServerCommand.start() : _command = TestServerCommandState.start; | |
57 TestServerCommand.stop() : _command = TestServerCommandState.stop; | |
58 | |
59 bool get isStart => (_command == TestServerCommandState.start); | |
60 bool get isStop => (_command == TestServerCommandState.stop); | |
61 | |
62 TestServerCommandState _command; | |
63 } | |
64 | |
65 enum TestServerStatusState { | |
66 started, | |
67 stopped, | |
68 error, | |
69 } | |
70 | |
71 class TestServerStatus { | |
72 TestServerStatus.started(this._port) : _state = TestServerStatusState.started; | |
73 TestServerStatus.stopped() : _state = TestServerStatusState.stopped; | |
74 TestServerStatus.error() : _state = TestServerStatusState.error; | |
75 | |
76 bool get isStarted => (_state == TestServerStatusState.started); | |
77 bool get isStopped => (_state == TestServerStatusState.stopped); | |
78 bool get isError => (_state == TestServerStatusState.error); | |
79 | |
80 int get port => _port; | |
81 | |
82 TestServerStatusState _state; | |
83 int _port; | |
84 } | |
85 | |
86 void startTestServer(SendPort replyTo) { | |
87 var server = new TestServer(); | |
88 server.init(); | |
89 replyTo.send(server.dispatchSendPort); | |
90 } | |
91 | |
92 class TestServer { | |
93 // Echo the request content back to the response. | |
94 void _echoHandler(HttpRequest request) { | |
95 var response = request.response; | |
96 expect("POST", equals(request.method)); | |
97 response.contentLength = request.contentLength; | |
98 request.listen((List<int> data) { | |
99 var string = new String.fromCharCodes(data); | |
100 response.write(string); | |
101 response.close(); | |
102 }); | |
103 } | |
104 | |
105 // Echo the request content back to the response. | |
106 void _zeroToTenHandler(HttpRequest request) { | |
107 var response = request.response; | |
108 String msg = "01234567890"; | |
109 expect("GET", equals(request.method)); | |
110 response.contentLength = msg.length; | |
111 response.write(msg); | |
112 response.close(); | |
113 } | |
114 | |
115 // Return a 404. | |
116 void _notFoundHandler(HttpRequest request) { | |
117 var response = request.response; | |
118 response.statusCode = HttpStatus.NOT_FOUND; | |
119 String msg = "Page not found"; | |
120 response.contentLength = msg.length; | |
121 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | |
122 response.write(msg); | |
123 response.close(); | |
124 } | |
125 | |
126 // Return a 301 with a custom reason phrase. | |
127 void _reasonForMovingHandler(HttpRequest request) { | |
128 var response = request.response; | |
129 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | |
130 response.reasonPhrase = "Don't come looking here any more"; | |
131 response.close(); | |
132 } | |
133 | |
134 // Check the "Host" header. | |
135 void _hostHandler(HttpRequest request) { | |
136 var response = request.response; | |
137 expect(1, equals(request.headers["Host"].length)); | |
138 expect("www.dartlang.org:1234", equals(request.headers["Host"][0])); | |
139 expect("www.dartlang.org", equals(request.headers.host)); | |
140 expect(1234, equals(request.headers.port)); | |
141 response.statusCode = HttpStatus.OK; | |
142 response.close(); | |
143 } | |
144 | |
145 void init() { | |
146 // Setup request handlers. | |
147 _requestHandlers = new Map(); | |
148 _requestHandlers["/echo"] = _echoHandler; | |
149 _requestHandlers["/0123456789"] = _zeroToTenHandler; | |
150 _requestHandlers["/reasonformoving"] = _reasonForMovingHandler; | |
151 _requestHandlers["/host"] = _hostHandler; | |
152 _dispatchPort = new ReceivePort(); | |
153 _dispatchPort.listen(dispatch); | |
154 } | |
155 | |
156 SendPort get dispatchSendPort => _dispatchPort.sendPort; | |
157 | |
158 void dispatch(var message) { | |
159 TestServerCommand command = message[0]; | |
160 SendPort replyTo = message[1]; | |
161 if (command.isStart) { | |
162 try { | |
163 HttpServer.bind("127.0.0.1", 0).then((server) { | |
164 _server = server; | |
165 _server.listen(_requestReceivedHandler); | |
166 replyTo.send(new TestServerStatus.started(_server.port)); | |
167 }); | |
168 } catch (e) { | |
169 replyTo.send(new TestServerStatus.error()); | |
170 } | |
171 } else if (command.isStop) { | |
172 _server.close(); | |
173 _dispatchPort.close(); | |
174 replyTo.send(new TestServerStatus.stopped()); | |
175 } | |
176 } | |
177 | |
178 void _requestReceivedHandler(HttpRequest request) { | |
179 var requestHandler = _requestHandlers[request.uri.path]; | |
180 if (requestHandler != null) { | |
181 requestHandler(request); | |
182 } else { | |
183 _notFoundHandler(request); | |
184 } | |
185 } | |
186 | |
187 HttpServer _server; // HTTP server instance. | |
188 ReceivePort _dispatchPort; | |
189 Map _requestHandlers; | |
190 } | |
191 | |
192 Future testStartStop() async { | |
193 Completer completer = new Completer(); | |
194 TestServerMain testServerMain = new TestServerMain(); | |
195 testServerMain.setServerStartedHandler((int port) { | |
196 testServerMain.close(); | |
197 completer.complete(); | |
198 }); | |
199 testServerMain.start(); | |
200 return completer.future; | |
201 } | |
202 | |
203 Future testGET() async { | |
204 Completer completer = new Completer(); | |
205 TestServerMain testServerMain = new TestServerMain(); | |
206 testServerMain.setServerStartedHandler((int port) { | |
207 var request = | |
208 SyncHttpClient.getUrl(new Uri.http("127.0.0.1:$port", "/0123456789")); | |
209 var response = request.close(); | |
210 expect(HttpStatus.OK, equals(response.statusCode)); | |
211 expect(11, equals(response.contentLength)); | |
212 expect("01234567890", equals(response.body)); | |
213 testServerMain.close(); | |
214 completer.complete(); | |
215 }); | |
216 testServerMain.start(); | |
217 return completer.future; | |
218 } | |
219 | |
220 Future testPOST() async { | |
221 Completer completer = new Completer(); | |
222 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ"; | |
223 final int kMessageCount = 10; | |
224 | |
225 TestServerMain testServerMain = new TestServerMain(); | |
226 | |
227 void runTest(int port) { | |
228 int count = 0; | |
229 void sendRequest() { | |
230 var request = | |
231 SyncHttpClient.postUrl(new Uri.http("127.0.0.1:$port", "/echo")); | |
232 request.write(data); | |
233 var response = request.close(); | |
234 expect(HttpStatus.OK, equals(response.statusCode)); | |
235 expect(data, equals(response.body)); | |
236 count++; | |
237 if (count < kMessageCount) { | |
238 sendRequest(); | |
239 } else { | |
240 testServerMain.close(); | |
241 completer.complete(); | |
242 } | |
243 } | |
244 | |
245 sendRequest(); | |
246 } | |
247 | |
248 testServerMain.setServerStartedHandler(runTest); | |
249 testServerMain.start(); | |
250 return completer.future; | |
251 } | |
252 | |
253 Future test404() async { | |
254 Completer completer = new Completer(); | |
255 TestServerMain testServerMain = new TestServerMain(); | |
256 testServerMain.setServerStartedHandler((int port) { | |
257 var request = SyncHttpClient | |
258 .getUrl(new Uri.http("127.0.0.1:$port", "/thisisnotfound")); | |
259 var response = request.close(); | |
260 expect(HttpStatus.NOT_FOUND, equals(response.statusCode)); | |
261 expect("Page not found", equals(response.body)); | |
262 testServerMain.close(); | |
263 completer.complete(); | |
264 }); | |
265 testServerMain.start(); | |
266 return completer.future; | |
267 } | |
268 | |
269 Future testReasonPhrase() async { | |
270 Completer completer = new Completer(); | |
271 TestServerMain testServerMain = new TestServerMain(); | |
272 testServerMain.setServerStartedHandler((int port) { | |
273 var request = SyncHttpClient | |
274 .getUrl(new Uri.http("127.0.0.1:$port", "/reasonformoving")); | |
275 var response = request.close(); | |
276 expect(HttpStatus.MOVED_PERMANENTLY, equals(response.statusCode)); | |
277 expect( | |
278 "Don't come looking here any more\r\n", equals(response.reasonPhrase)); | |
279 testServerMain.close(); | |
280 completer.complete(); | |
281 }); | |
282 testServerMain.start(); | |
283 return completer.future; | |
284 } | |
285 | |
286 void main() { | |
287 test("Simple server test", () async { | |
288 await testStartStop(); | |
289 }); | |
290 test("Sync HTTP GET test", () async { | |
291 await testGET(); | |
292 }); | |
293 test("Sync HTTP POST test", () async { | |
294 await testPOST(); | |
295 }); | |
296 test("Sync HTTP 404 test", () async { | |
297 await test404(); | |
298 }); | |
299 test("Sync HTTP moved test", () async { | |
300 await testReasonPhrase(); | |
301 }); | |
302 } | |
OLD | NEW |