Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: samples/tests/src/chat/http_test.dart

Issue 8274034: Make the sample tests run using the standard test configuration (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/tests/src/chat/http_parser_test.dart ('k') | tools/test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
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 #library("http_test.dart");
6 #import("../../../chat/http.dart");
7 #import("../../../../client/json/dart_json.dart");
8 #import("../../../chat/chat_server_lib.dart");
9
10
11 class TestServerMain {
12 TestServerMain()
13 : _statusPort = new ReceivePort(),
14 _serverPort = null {
15 new TestServer().spawn().then((SendPort port) {
16 _serverPort = port;
17 });
18 }
19
20 void setServerStartedHandler(void startedCallback(int port)) {
21 _startedCallback = startedCallback;
22 }
23
24 void start() {
25 // Handle status messages from the server.
26 _statusPort.receive(
27 void _(var status, SendPort replyTo) {
28 if (status.isStarted) {
29 _startedCallback(status.port);
30 }
31 });
32
33 // Send server start message to the server.
34 var command = new TestServerCommand.start();
35 _serverPort.send(command, _statusPort.toSendPort());
36 }
37
38 void shutdown() {
39 // Send server stop message to the server.
40 _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort());
41 _statusPort.close();
42 }
43
44 void chunkedEncoding() {
45 // Send chunked encoding message to the server.
46 _serverPort.send(
47 new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort());
48 _statusPort.close();
49 }
50
51 ReceivePort _statusPort; // Port for receiving messages from the server.
52 SendPort _serverPort; // Port for sending messages to the server.
53 var _startedCallback;
54 }
55
56
57 class TestServerCommand {
58 static final START = 0;
59 static final STOP = 1;
60 static final CHUNKED_ENCODING = 1;
61
62 TestServerCommand.start() : _command = START;
63 TestServerCommand.stop() : _command = STOP;
64 TestServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING;
65
66 bool get isStart() => _command == START;
67 bool get isStop() => _command == STOP;
68 bool get isChunkedEncoding() => _command == CHUNKED_ENCODING;
69
70 int _command;
71 }
72
73
74 class TestServerStatus {
75 static final STARTED = 0;
76 static final STOPPED = 1;
77 static final ERROR = 2;
78
79 TestServerStatus.started(this._port) : _state = STARTED;
80 TestServerStatus.stopped() : _state = STOPPED;
81 TestServerStatus.error() : _state = ERROR;
82
83 bool get isStarted() => _state == STARTED;
84 bool get isStopped() => _state == STOPPED;
85 bool get isError() => _state == ERROR;
86
87 int get port() => _port;
88
89 int _state;
90 int _port;
91 }
92
93
94 class TestServer extends Isolate {
95 // Echo the request content back to the response.
96 void _echoHandler(HTTPRequest request, HTTPResponse response) {
97 Expect.equals("POST", request.method);
98 request.dataEnd =
99 void _(String body) {
100 response.writeString(body);
101 //response.contentLength = body.length;
102 //response.writeList(body.charCodes(), 0, body.length);
103 response.writeDone();
104 };
105 }
106
107 // Echo the request content back to the response.
108 void _zeroToTenHandler(HTTPRequest request, HTTPResponse response) {
109 Expect.equals("GET", request.method);
110 request.dataEnd =
111 void _(String body) {
112 response.writeString("01234567890");
113 response.writeDone();
114 };
115 }
116
117 // Return a 404.
118 void _notFoundHandler(HTTPRequest request, HTTPResponse response) {
119 response.statusCode = HTTPStatus.NOT_FOUND;
120 response.setHeader("Content-Type", "text/html; charset=UTF-8");
121 response.writeString("Page not found");
122 response.writeDone();
123 }
124
125 void main() {
126 // Setup request handlers.
127 _requestHandlers = new Map();
128 _requestHandlers["/echo"] =
129 (HTTPRequest request, HTTPResponse response) =>
130 _echoHandler(request, response);
131 _requestHandlers["/0123456789"] =
132 (HTTPRequest request, HTTPResponse response) =>
133 _zeroToTenHandler(request, response);
134
135 this.port.receive(
136 void _(var message, SendPort replyTo) {
137 if (message.isStart) {
138 _server = new HTTPServer();
139 try {
140 _chunkedEncoding = false;
141 _server.listen(
142 "127.0.0.1",
143 0,
144 (HTTPRequest req, HTTPResponse rsp) =>
145 _requestReceivedHandler(req, rsp));
146 replyTo.send(new TestServerStatus.started(_server.port), null);
147 } catch (var e) {
148 replyTo.send(new TestServerStatus.error(), null);
149 }
150 } else if (message.isStop) {
151 _server.close();
152 this.port.close();
153 replyTo.send(new TestServerStatus.stopped(), null);
154 } else if (message.isChunkedEncoding) {
155 _chunkedEncoding = true;
156 }
157 });
158 }
159
160 void _requestReceivedHandler(HTTPRequest request, HTTPResponse response) {
161 var requestHandler =_requestHandlers[request.path];
162 if (requestHandler != null) {
163 requestHandler(request, response);
164 } else {
165 _notFoundHandler(request, response);
166 }
167 }
168
169 HTTPServer _server; // HTTP server instance.
170 Map _requestHandlers;
171 bool _chunkedEncoding;
172 }
173
174
175 void testStartStop() {
176 TestServerMain testServerMain = new TestServerMain();
177 testServerMain.setServerStartedHandler(
178 void _(int port) {
179 testServerMain.shutdown();
180 });
181 testServerMain.start();
182 }
183
184
185 void testGET() {
186 TestServerMain testServerMain = new TestServerMain();
187 testServerMain.setServerStartedHandler(
188 void _(int port) {
189 HTTPClient httpClient = new HTTPClient();
190 HTTPClientRequest request = httpClient.open("GET",
191 "127.0.0.1",
192 port,
193 "/0123456789");
194 request.responseReceived =
195 void _(HTTPClientResponse response) {
196 Expect.equals(HTTPStatus.OK, response.statusCode);
197 response.dataEnd =
198 void _(String body) {
199 Expect.equals("01234567890", body);
200 httpClient.shutdown();
201 testServerMain.shutdown();
202 };
203 };
204 request.writeDone();
205 });
206 testServerMain.start();
207 }
208
209
210 void testPOST(bool chunkedEncoding) {
211 String data = "ABCDEFGHIJKLMONPQRSTUVWXYZ";
212 final int kMessageCount = 10;
213
214 TestServerMain testServerMain = new TestServerMain();
215
216 void runTest(int port) {
217 int count = 0;
218 HTTPClient httpClient = new HTTPClient();
219 void sendRequest() {
220 HTTPClientRequest request = httpClient.open("POST",
221 "127.0.0.1",
222 port,
223 "/echo");
224
225 request.responseReceived =
226 void _(HTTPClientResponse response) {
227 Expect.equals(HTTPStatus.OK, response.statusCode);
228 response.dataEnd =
229 void _(String body) {
230 Expect.equals(data, body);
231 count++;
232 if (count < kMessageCount) {
233 sendRequest();
234 } else {
235 httpClient.shutdown();
236 testServerMain.shutdown();
237 }
238 };
239 };
240
241 if (chunkedEncoding) {
242 request.writeString(data);
243 } else {
244 request.contentLength = data.length;
245 request.writeList(data.charCodes(), 0, data.length);
246 }
247 request.writeDone();
248 }
249
250 sendRequest();
251 }
252
253 testServerMain.setServerStartedHandler(runTest);
254 testServerMain.start();
255 if (chunkedEncoding) {
256 testServerMain.chunkedEncoding();
257 }
258 }
259
260
261 void test404() {
262 TestServerMain testServerMain = new TestServerMain();
263 testServerMain.setServerStartedHandler(
264 void _(int port) {
265 HTTPClient httpClient = new HTTPClient();
266 HTTPClientRequest request = httpClient.open("GET",
267 "127.0.0.1",
268 port,
269 "/thisisnotfound");
270 request.writeDone();
271 request.keepAlive = false;
272 request.responseReceived =
273 void _(HTTPClientResponse response) {
274 Expect.equals(HTTPStatus.NOT_FOUND, response.statusCode);
275 httpClient.shutdown();
276 testServerMain.shutdown();
277 };
278 });
279 testServerMain.start();
280 }
281
282
283 void main() {
284 testStartStop();
285 testGET();
286 testPOST(true);
287 testPOST(false);
288 test404();
289 }
OLDNEW
« no previous file with comments | « samples/tests/src/chat/http_parser_test.dart ('k') | tools/test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698