OLD | NEW |
1 // Copyright (c) 2012, 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 'dart:async'; | 10 import 'dart:async'; |
11 import 'dart:io'; | 11 import 'dart:io'; |
12 import 'dart:isolate'; | 12 import 'dart:isolate'; |
13 | 13 |
14 class TestServerMain { | 14 class IsolatedHttpServer { |
15 TestServerMain() | 15 IsolatedHttpServer() |
16 : _statusPort = new ReceivePort(), | 16 : _statusPort = new ReceivePort(), |
17 _serverPort = null { | 17 _serverPort = null { |
18 _serverPort = spawnFunction(startTestServer); | 18 _serverPort = spawnFunction(startIsolatedHttpServer); |
19 } | 19 } |
20 | 20 |
21 void setServerStartedHandler(void startedCallback(int port)) { | 21 void setServerStartedHandler(void startedCallback(int port)) { |
22 _startedCallback = startedCallback; | 22 _startedCallback = startedCallback; |
23 } | 23 } |
24 | 24 |
25 void start() { | 25 void start() { |
26 // Handle status messages from the server. | 26 // Handle status messages from the server. |
27 _statusPort.receive((var status, SendPort replyTo) { | 27 _statusPort.receive((var status, SendPort replyTo) { |
28 if (status.isStarted) { | 28 if (status.isStarted) { |
29 _startedCallback(status.port); | 29 _startedCallback(status.port); |
30 } | 30 } |
31 }); | 31 }); |
32 | 32 |
33 // Send server start message to the server. | 33 // Send server start message to the server. |
34 var command = new TestServerCommand.start(); | 34 var command = new IsolatedHttpServerCommand.start(); |
35 _serverPort.send(command, _statusPort.toSendPort()); | 35 _serverPort.send(command, _statusPort.toSendPort()); |
36 } | 36 } |
37 | 37 |
38 void shutdown() { | 38 void shutdown() { |
39 // Send server stop message to the server. | 39 // Send server stop message to the server. |
40 _serverPort.send(new TestServerCommand.stop(), _statusPort.toSendPort()); | 40 _serverPort.send(new IsolatedHttpServerCommand.stop(), |
| 41 _statusPort.toSendPort()); |
41 _statusPort.close(); | 42 _statusPort.close(); |
42 } | 43 } |
43 | 44 |
44 void chunkedEncoding() { | 45 void chunkedEncoding() { |
45 // Send chunked encoding message to the server. | 46 // Send chunked encoding message to the server. |
46 _serverPort.send( | 47 _serverPort.send( |
47 new TestServerCommand.chunkedEncoding(), _statusPort.toSendPort()); | 48 new IsolatedHttpServerCommand.chunkedEncoding(), |
| 49 _statusPort.toSendPort()); |
48 } | 50 } |
49 | 51 |
50 ReceivePort _statusPort; // Port for receiving messages from the server. | 52 ReceivePort _statusPort; // Port for receiving messages from the server. |
51 SendPort _serverPort; // Port for sending messages to the server. | 53 SendPort _serverPort; // Port for sending messages to the server. |
52 var _startedCallback; | 54 var _startedCallback; |
53 } | 55 } |
54 | 56 |
55 | 57 |
56 class TestServerCommand { | 58 class IsolatedHttpServerCommand { |
57 static const START = 0; | 59 static const START = 0; |
58 static const STOP = 1; | 60 static const STOP = 1; |
59 static const CHUNKED_ENCODING = 2; | 61 static const CHUNKED_ENCODING = 2; |
60 | 62 |
61 TestServerCommand.start() : _command = START; | 63 IsolatedHttpServerCommand.start() : _command = START; |
62 TestServerCommand.stop() : _command = STOP; | 64 IsolatedHttpServerCommand.stop() : _command = STOP; |
63 TestServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING; | 65 IsolatedHttpServerCommand.chunkedEncoding() : _command = CHUNKED_ENCODING; |
64 | 66 |
65 bool get isStart => _command == START; | 67 bool get isStart => _command == START; |
66 bool get isStop => _command == STOP; | 68 bool get isStop => _command == STOP; |
67 bool get isChunkedEncoding => _command == CHUNKED_ENCODING; | 69 bool get isChunkedEncoding => _command == CHUNKED_ENCODING; |
68 | 70 |
69 int _command; | 71 int _command; |
70 } | 72 } |
71 | 73 |
72 | 74 |
73 class TestServerStatus { | 75 class IsolatedHttpServerStatus { |
74 static const STARTED = 0; | 76 static const STARTED = 0; |
75 static const STOPPED = 1; | 77 static const STOPPED = 1; |
76 static const ERROR = 2; | 78 static const ERROR = 2; |
77 | 79 |
78 TestServerStatus.started(this._port) : _state = STARTED; | 80 IsolatedHttpServerStatus.started(this._port) : _state = STARTED; |
79 TestServerStatus.stopped() : _state = STOPPED; | 81 IsolatedHttpServerStatus.stopped() : _state = STOPPED; |
80 TestServerStatus.error() : _state = ERROR; | 82 IsolatedHttpServerStatus.error() : _state = ERROR; |
81 | 83 |
82 bool get isStarted => _state == STARTED; | 84 bool get isStarted => _state == STARTED; |
83 bool get isStopped => _state == STOPPED; | 85 bool get isStopped => _state == STOPPED; |
84 bool get isError => _state == ERROR; | 86 bool get isError => _state == ERROR; |
85 | 87 |
86 int get port => _port; | 88 int get port => _port; |
87 | 89 |
88 int _state; | 90 int _state; |
89 int _port; | 91 int _port; |
90 } | 92 } |
91 | 93 |
92 | 94 |
93 void startTestServer() { | 95 void startIsolatedHttpServer() { |
94 var server = new TestServer(); | 96 var server = new TestServer(); |
95 server.init(); | 97 server.init(); |
96 port.receive(server.dispatch); | 98 port.receive(server.dispatch); |
97 } | 99 } |
98 | 100 |
99 | 101 |
100 class TestServer { | 102 class TestServer { |
101 // Return a 404. | 103 // Return a 404. |
102 void _notFoundHandler(HttpRequest request, HttpResponse response) { | 104 void _notFoundHandler(HttpRequest request) { |
| 105 var response = request.response; |
103 response.statusCode = HttpStatus.NOT_FOUND; | 106 response.statusCode = HttpStatus.NOT_FOUND; |
104 response.headers.set("Content-Type", "text/html; charset=UTF-8"); | 107 response.headers.set("Content-Type", "text/html; charset=UTF-8"); |
105 response.outputStream.writeString("Page not found"); | 108 response.outputStream.writeString("Page not found"); |
106 response.outputStream.close(); | 109 response.close(); |
107 } | 110 } |
108 | 111 |
109 // Check the "Host" header. | 112 // Check the "Host" header. |
110 void _hostHandler(HttpRequest request, HttpResponse response) { | 113 void _hostHandler(HttpRequest request) { |
| 114 var response = request.response; |
111 Expect.equals(1, request.headers["Host"].length); | 115 Expect.equals(1, request.headers["Host"].length); |
112 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); | 116 Expect.equals("www.dartlang.org:1234", request.headers["Host"][0]); |
113 Expect.equals("www.dartlang.org", request.headers.host); | 117 Expect.equals("www.dartlang.org", request.headers.host); |
114 Expect.equals(1234, request.headers.port); | 118 Expect.equals(1234, request.headers.port); |
115 response.statusCode = HttpStatus.OK; | 119 response.statusCode = HttpStatus.OK; |
116 response.outputStream.close(); | 120 response.close(); |
117 } | 121 } |
118 | 122 |
119 // Set the "Expires" header using the expires property. | 123 // Set the "Expires" header using the expires property. |
120 void _expires1Handler(HttpRequest request, HttpResponse response) { | 124 void _expires1Handler(HttpRequest request) { |
| 125 var response = request.response; |
121 DateTime date = new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0); | 126 DateTime date = new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0); |
122 response.headers.expires = date; | 127 response.headers.expires = date; |
123 Expect.equals(date, response.headers.expires); | 128 Expect.equals(date, response.headers.expires); |
124 response.outputStream.close(); | 129 response.close(); |
125 } | 130 } |
126 | 131 |
127 // Set the "Expires" header. | 132 // Set the "Expires" header. |
128 void _expires2Handler(HttpRequest request, HttpResponse response) { | 133 void _expires2Handler(HttpRequest request) { |
| 134 var response = request.response; |
129 response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT"); | 135 response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT"); |
130 DateTime date = new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0); | 136 DateTime date = new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0); |
131 Expect.equals(date, response.headers.expires); | 137 Expect.equals(date, response.headers.expires); |
132 response.outputStream.close(); | 138 response.close(); |
133 } | 139 } |
134 | 140 |
135 void _contentType1Handler(HttpRequest request, HttpResponse response) { | 141 void _contentType1Handler(HttpRequest request) { |
| 142 var response = request.response; |
136 Expect.equals("text/html", request.headers.contentType.value); | 143 Expect.equals("text/html", request.headers.contentType.value); |
137 Expect.equals("text", request.headers.contentType.primaryType); | 144 Expect.equals("text", request.headers.contentType.primaryType); |
138 Expect.equals("html", request.headers.contentType.subType); | 145 Expect.equals("html", request.headers.contentType.subType); |
139 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); | 146 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); |
140 | 147 |
141 ContentType contentType = new ContentType("text", "html"); | 148 ContentType contentType = new ContentType("text", "html"); |
142 contentType.parameters["charset"] = "utf-8"; | 149 contentType.parameters["charset"] = "utf-8"; |
143 response.headers.contentType = contentType; | 150 response.headers.contentType = contentType; |
144 response.outputStream.close(); | 151 response.close(); |
145 } | 152 } |
146 | 153 |
147 void _contentType2Handler(HttpRequest request, HttpResponse response) { | 154 void _contentType2Handler(HttpRequest request) { |
| 155 var response = request.response; |
148 Expect.equals("text/html", request.headers.contentType.value); | 156 Expect.equals("text/html", request.headers.contentType.value); |
149 Expect.equals("text", request.headers.contentType.primaryType); | 157 Expect.equals("text", request.headers.contentType.primaryType); |
150 Expect.equals("html", request.headers.contentType.subType); | 158 Expect.equals("html", request.headers.contentType.subType); |
151 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); | 159 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); |
152 | 160 |
153 response.headers.set(HttpHeaders.CONTENT_TYPE, | 161 response.headers.set(HttpHeaders.CONTENT_TYPE, |
154 "text/html; charset = utf-8"); | 162 "text/html; charset = utf-8"); |
155 response.outputStream.close(); | 163 response.close(); |
156 } | 164 } |
157 | 165 |
158 void _cookie1Handler(HttpRequest request, HttpResponse response) { | 166 void _cookie1Handler(HttpRequest request) { |
| 167 var response = request.response; |
| 168 |
159 // No cookies passed with this request. | 169 // No cookies passed with this request. |
160 Expect.equals(0, request.cookies.length); | 170 Expect.equals(0, request.cookies.length); |
161 | 171 |
162 Cookie cookie1 = new Cookie("name1", "value1"); | 172 Cookie cookie1 = new Cookie("name1", "value1"); |
163 DateTime date = new DateTime.utc(2014, DateTime.JAN, 5, 23, 59, 59, 0); | 173 DateTime date = new DateTime.utc(2014, DateTime.JAN, 5, 23, 59, 59, 0); |
164 cookie1.expires = date; | 174 cookie1.expires = date; |
165 cookie1.domain = "www.example.com"; | 175 cookie1.domain = "www.example.com"; |
166 cookie1.httpOnly = true; | 176 cookie1.httpOnly = true; |
167 response.cookies.add(cookie1); | 177 response.cookies.add(cookie1); |
168 Cookie cookie2 = new Cookie("name2", "value2"); | 178 Cookie cookie2 = new Cookie("name2", "value2"); |
169 cookie2.maxAge = 100; | 179 cookie2.maxAge = 100; |
170 cookie2.domain = ".example.com"; | 180 cookie2.domain = ".example.com"; |
171 cookie2.path = "/shop"; | 181 cookie2.path = "/shop"; |
172 response.cookies.add(cookie2); | 182 response.cookies.add(cookie2); |
173 response.outputStream.close(); | 183 response.close(); |
174 } | 184 } |
175 | 185 |
176 void _cookie2Handler(HttpRequest request, HttpResponse response) { | 186 void _cookie2Handler(HttpRequest request) { |
| 187 var response = request.response; |
| 188 |
177 // Two cookies passed with this request. | 189 // Two cookies passed with this request. |
178 Expect.equals(2, request.cookies.length); | 190 Expect.equals(2, request.cookies.length); |
179 response.outputStream.close(); | 191 response.close(); |
180 } | |
181 | |
182 void _flushHandler(HttpRequest request, HttpResponse response) { | |
183 response.outputStream.flush(); | |
184 response.outputStream.close(); | |
185 } | 192 } |
186 | 193 |
187 void init() { | 194 void init() { |
188 // Setup request handlers. | 195 // Setup request handlers. |
189 _requestHandlers = new Map(); | 196 _requestHandlers = new Map(); |
190 _requestHandlers["/host"] = | 197 _requestHandlers["/host"] = _hostHandler; |
191 (HttpRequest request, HttpResponse response) { | 198 _requestHandlers["/expires1"] = _expires1Handler; |
192 _hostHandler(request, response); | 199 _requestHandlers["/expires2"] = _expires2Handler; |
193 }; | 200 _requestHandlers["/contenttype1"] = _contentType1Handler; |
194 _requestHandlers["/expires1"] = | 201 _requestHandlers["/contenttype2"] = _contentType2Handler; |
195 (HttpRequest request, HttpResponse response) { | 202 _requestHandlers["/cookie1"] = _cookie1Handler; |
196 _expires1Handler(request, response); | 203 _requestHandlers["/cookie2"] = _cookie2Handler; |
197 }; | |
198 _requestHandlers["/expires2"] = | |
199 (HttpRequest request, HttpResponse response) { | |
200 _expires2Handler(request, response); | |
201 }; | |
202 _requestHandlers["/contenttype1"] = | |
203 (HttpRequest request, HttpResponse response) { | |
204 _contentType1Handler(request, response); | |
205 }; | |
206 _requestHandlers["/contenttype2"] = | |
207 (HttpRequest request, HttpResponse response) { | |
208 _contentType2Handler(request, response); | |
209 }; | |
210 _requestHandlers["/cookie1"] = | |
211 (HttpRequest request, HttpResponse response) { | |
212 _cookie1Handler(request, response); | |
213 }; | |
214 _requestHandlers["/cookie2"] = | |
215 (HttpRequest request, HttpResponse response) { | |
216 _cookie2Handler(request, response); | |
217 }; | |
218 _requestHandlers["/flush"] = | |
219 (HttpRequest request, HttpResponse response) { | |
220 _flushHandler(request, response); | |
221 }; | |
222 } | 204 } |
223 | 205 |
224 void dispatch(message, replyTo) { | 206 void dispatch(message, replyTo) { |
225 if (message.isStart) { | 207 if (message.isStart) { |
226 _server = new HttpServer(); | |
227 try { | 208 try { |
228 _server.listen("127.0.0.1", 0); | 209 HttpServer.bind().then((server) { |
229 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | 210 _server = server; |
230 _requestReceivedHandler(req, rsp); | 211 _server.listen(_requestReceivedHandler); |
231 }; | 212 replyTo.send(new IsolatedHttpServerStatus.started(_server.port), |
232 replyTo.send(new TestServerStatus.started(_server.port), null); | 213 null); |
| 214 }); |
233 } catch (e) { | 215 } catch (e) { |
234 replyTo.send(new TestServerStatus.error(), null); | 216 replyTo.send(new IsolatedHttpServerStatus.error(), null); |
235 } | 217 } |
236 } else if (message.isStop) { | 218 } else if (message.isStop) { |
237 _server.close(); | 219 _server.close(); |
238 port.close(); | 220 port.close(); |
239 replyTo.send(new TestServerStatus.stopped(), null); | 221 replyTo.send(new IsolatedHttpServerStatus.stopped(), null); |
240 } else if (message.isChunkedEncoding) { | 222 } else if (message.isChunkedEncoding) { |
241 _chunkedEncoding = true; | 223 _chunkedEncoding = true; |
242 } | 224 } |
243 } | 225 } |
244 | 226 |
245 void _requestReceivedHandler(HttpRequest request, HttpResponse response) { | 227 void _requestReceivedHandler(HttpRequest request) { |
246 var requestHandler =_requestHandlers[request.path]; | 228 var requestHandler =_requestHandlers[request.uri.path]; |
247 if (requestHandler != null) { | 229 if (requestHandler != null) { |
248 requestHandler(request, response); | 230 requestHandler(request); |
249 } else { | 231 } else { |
250 _notFoundHandler(request, response); | 232 _notFoundHandler(request); |
251 } | 233 } |
252 } | 234 } |
253 | 235 |
254 HttpServer _server; // HTTP server instance. | 236 HttpServer _server; // HTTP server instance. |
255 Map _requestHandlers; | 237 Map _requestHandlers; |
256 bool _chunkedEncoding = false; | 238 bool _chunkedEncoding = false; |
257 } | 239 } |
258 | 240 |
259 Future testHost() { | 241 Future testHost() { |
260 Completer completer = new Completer(); | 242 Completer completer = new Completer(); |
261 TestServerMain testServerMain = new TestServerMain(); | 243 IsolatedHttpServer server = new IsolatedHttpServer(); |
262 testServerMain.setServerStartedHandler((int port) { | 244 server.setServerStartedHandler((int port) { |
263 HttpClient httpClient = new HttpClient(); | 245 HttpClient httpClient = new HttpClient(); |
264 HttpClientConnection conn = | 246 httpClient.get("127.0.0.1", port, "/host") |
265 httpClient.get("127.0.0.1", port, "/host"); | 247 .then((request) { |
266 conn.onRequest = (HttpClientRequest request) { | 248 Expect.equals("127.0.0.1:$port", request.headers["host"][0]); |
267 Expect.equals("127.0.0.1:$port", request.headers["host"][0]); | 249 request.headers.host = "www.dartlang.com"; |
268 request.headers.host = "www.dartlang.com"; | 250 Expect.equals("www.dartlang.com:$port", request.headers["host"][0]); |
269 Expect.equals("www.dartlang.com:$port", request.headers["host"][0]); | 251 Expect.equals("www.dartlang.com", request.headers.host); |
270 Expect.equals("www.dartlang.com", request.headers.host); | 252 Expect.equals(port, request.headers.port); |
271 Expect.equals(port, request.headers.port); | 253 request.headers.port = 1234; |
272 request.headers.port = 1234; | 254 Expect.equals("www.dartlang.com:1234", request.headers["host"][0]); |
273 Expect.equals("www.dartlang.com:1234", request.headers["host"][0]); | 255 Expect.equals(1234, request.headers.port); |
274 Expect.equals(1234, request.headers.port); | 256 request.headers.port = HttpClient.DEFAULT_HTTP_PORT; |
275 request.headers.port = HttpClient.DEFAULT_HTTP_PORT; | 257 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); |
276 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); | 258 Expect.equals("www.dartlang.com", request.headers["host"][0]); |
277 Expect.equals("www.dartlang.com", request.headers["host"][0]); | 259 request.headers.set("Host", "www.dartlang.org"); |
278 request.headers.set("Host", "www.dartlang.org"); | 260 Expect.equals("www.dartlang.org", request.headers.host); |
279 Expect.equals("www.dartlang.org", request.headers.host); | 261 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); |
280 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); | 262 request.headers.set("Host", "www.dartlang.org:"); |
281 request.headers.set("Host", "www.dartlang.org:"); | 263 Expect.equals("www.dartlang.org", request.headers.host); |
282 Expect.equals("www.dartlang.org", request.headers.host); | 264 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); |
283 Expect.equals(HttpClient.DEFAULT_HTTP_PORT, request.headers.port); | 265 request.headers.set("Host", "www.dartlang.org:1234"); |
284 request.headers.set("Host", "www.dartlang.org:1234"); | 266 Expect.equals("www.dartlang.org", request.headers.host); |
285 Expect.equals("www.dartlang.org", request.headers.host); | 267 Expect.equals(1234, request.headers.port); |
286 Expect.equals(1234, request.headers.port); | 268 return request.close(); |
287 request.outputStream.close(); | 269 }) |
288 }; | 270 .then((response) { |
289 conn.onResponse = (HttpClientResponse response) { | 271 Expect.equals(HttpStatus.OK, response.statusCode); |
290 Expect.equals(HttpStatus.OK, response.statusCode); | 272 response.listen( |
291 response.inputStream.onData = response.inputStream.read; | 273 (_) { }, |
292 response.inputStream.onClosed = () { | 274 onDone: () { |
293 httpClient.shutdown(); | 275 httpClient.close(); |
294 testServerMain.shutdown(); | 276 server.shutdown(); |
295 completer.complete(true); | 277 completer.complete(true); |
296 }; | 278 }); |
297 }; | 279 }); |
298 }); | 280 }); |
299 testServerMain.start(); | 281 server.start(); |
300 return completer.future; | 282 return completer.future; |
301 } | 283 } |
302 | 284 |
303 Future testExpires() { | 285 Future testExpires() { |
304 Completer completer = new Completer(); | 286 Completer completer = new Completer(); |
305 TestServerMain testServerMain = new TestServerMain(); | 287 IsolatedHttpServer server = new IsolatedHttpServer(); |
306 testServerMain.setServerStartedHandler((int port) { | 288 server.setServerStartedHandler((int port) { |
307 int responses = 0; | 289 int responses = 0; |
308 HttpClient httpClient = new HttpClient(); | 290 HttpClient httpClient = new HttpClient(); |
309 | 291 |
310 void processResponse(HttpClientResponse response) { | 292 void processResponse(HttpClientResponse response) { |
311 Expect.equals(HttpStatus.OK, response.statusCode); | 293 Expect.equals(HttpStatus.OK, response.statusCode); |
312 Expect.equals("Fri, 11 Jun 1999 18:46:53 GMT", | 294 Expect.equals("Fri, 11 Jun 1999 18:46:53 GMT", |
313 response.headers["expires"][0]); | 295 response.headers["expires"][0]); |
314 Expect.equals(new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0), | 296 Expect.equals(new DateTime.utc(1999, DateTime.JUN, 11, 18, 46, 53, 0), |
315 response.headers.expires); | 297 response.headers.expires); |
316 response.inputStream.onData = response.inputStream.read; | 298 response.listen((_) { }, |
317 response.inputStream.onClosed = () { | 299 onDone: () { |
318 responses++; | 300 responses++; |
319 if (responses == 2) { | 301 if (responses == 2) { |
320 httpClient.shutdown(); | 302 httpClient.close(); |
321 testServerMain.shutdown(); | 303 server.shutdown(); |
322 completer.complete(true); | 304 completer.complete(true); |
323 } | 305 } |
324 }; | 306 }); |
325 } | 307 } |
326 | 308 |
327 HttpClientConnection conn1 = httpClient.get("127.0.0.1", port, "/expires1"); | 309 httpClient.get("127.0.0.1", port, "/expires1") |
328 conn1.onResponse = (HttpClientResponse response) { | 310 .then((request) => request.close()) |
329 processResponse(response); | 311 .then(processResponse); |
330 }; | 312 httpClient.get("127.0.0.1", port, "/expires2") |
331 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); | 313 .then((request) => request.close()) |
332 conn2.onResponse = (HttpClientResponse response) { | 314 .then(processResponse); |
333 processResponse(response); | |
334 }; | |
335 }); | 315 }); |
336 testServerMain.start(); | 316 server.start(); |
337 return completer.future; | 317 return completer.future; |
338 } | 318 } |
339 | 319 |
340 Future testContentType() { | 320 Future testContentType() { |
341 Completer completer = new Completer(); | 321 Completer completer = new Completer(); |
342 TestServerMain testServerMain = new TestServerMain(); | 322 IsolatedHttpServer server = new IsolatedHttpServer(); |
343 testServerMain.setServerStartedHandler((int port) { | 323 server.setServerStartedHandler((int port) { |
344 int responses = 0; | 324 int responses = 0; |
345 HttpClient httpClient = new HttpClient(); | 325 HttpClient httpClient = new HttpClient(); |
346 | 326 |
347 void processResponse(HttpClientResponse response) { | 327 void processResponse(HttpClientResponse response) { |
348 Expect.equals(HttpStatus.OK, response.statusCode); | 328 Expect.equals(HttpStatus.OK, response.statusCode); |
349 Expect.equals("text/html; charset=utf-8", | 329 Expect.equals("text/html; charset=utf-8", |
350 response.headers.contentType.toString()); | 330 response.headers.contentType.toString()); |
351 Expect.equals("text/html", response.headers.contentType.value); | 331 Expect.equals("text/html", response.headers.contentType.value); |
352 Expect.equals("text", response.headers.contentType.primaryType); | 332 Expect.equals("text", response.headers.contentType.primaryType); |
353 Expect.equals("html", response.headers.contentType.subType); | 333 Expect.equals("html", response.headers.contentType.subType); |
354 Expect.equals("utf-8", | 334 Expect.equals("utf-8", |
355 response.headers.contentType.parameters["charset"]); | 335 response.headers.contentType.parameters["charset"]); |
356 response.inputStream.onData = response.inputStream.read; | 336 response.listen( |
357 response.inputStream.onClosed = () { | 337 (_) { }, |
358 responses++; | 338 onDone: () { |
359 if (responses == 2) { | 339 responses++; |
360 httpClient.shutdown(); | 340 if (responses == 2) { |
361 testServerMain.shutdown(); | 341 httpClient.close(); |
362 completer.complete(true); | 342 server.shutdown(); |
363 } | 343 completer.complete(true); |
364 }; | 344 } |
| 345 }); |
365 } | 346 } |
366 | 347 |
367 HttpClientConnection conn1 = | 348 httpClient.get("127.0.0.1", port, "/contenttype1") |
368 httpClient.get("127.0.0.1", port, "/contenttype1"); | 349 .then((request) { |
369 conn1.onRequest = (HttpClientRequest request) { | 350 ContentType contentType = new ContentType(); |
370 ContentType contentType = new ContentType(); | 351 contentType.value = "text/html"; |
371 contentType.value = "text/html"; | 352 contentType.parameters["charset"] = "utf-8"; |
372 contentType.parameters["charset"] = "utf-8"; | 353 request.headers.contentType = contentType; |
373 request.headers.contentType = contentType; | 354 return request.close(); |
374 request.outputStream.close(); | 355 }) |
375 }; | 356 .then(processResponse); |
376 conn1.onResponse = (HttpClientResponse response) { | 357 |
377 processResponse(response); | 358 httpClient.get("127.0.0.1", port, "/contenttype2") |
378 }; | 359 .then((request) { |
379 HttpClientConnection conn2 = | 360 request.headers.set(HttpHeaders.CONTENT_TYPE, |
380 httpClient.get("127.0.0.1", port, "/contenttype2"); | 361 "text/html; charset = utf-8"); |
381 conn2.onRequest = (HttpClientRequest request) { | 362 return request.close(); |
382 request.headers.set(HttpHeaders.CONTENT_TYPE, | 363 }) |
383 "text/html; charset = utf-8"); | 364 .then(processResponse); |
384 request.outputStream.close(); | |
385 }; | |
386 conn2.onResponse = (HttpClientResponse response) { | |
387 processResponse(response); | |
388 }; | |
389 }); | 365 }); |
390 testServerMain.start(); | 366 server.start(); |
391 return completer.future; | 367 return completer.future; |
392 } | 368 } |
393 | 369 |
394 Future testCookies() { | 370 Future testCookies() { |
395 Completer completer = new Completer(); | 371 Completer completer = new Completer(); |
396 TestServerMain testServerMain = new TestServerMain(); | 372 IsolatedHttpServer server = new IsolatedHttpServer(); |
397 testServerMain.setServerStartedHandler((int port) { | 373 server.setServerStartedHandler((int port) { |
398 int responses = 0; | 374 int responses = 0; |
399 HttpClient httpClient = new HttpClient(); | 375 HttpClient httpClient = new HttpClient(); |
400 | 376 |
401 HttpClientConnection conn1 = | 377 httpClient.get("127.0.0.1", port, "/cookie1") |
402 httpClient.get("127.0.0.1", port, "/cookie1"); | 378 .then((request) => request.close()) |
403 conn1.onResponse = (HttpClientResponse response) { | 379 .then((response) { |
404 Expect.equals(2, response.cookies.length); | 380 Expect.equals(2, response.cookies.length); |
405 response.cookies.forEach((cookie) { | 381 response.cookies.forEach((cookie) { |
406 if (cookie.name == "name1") { | 382 if (cookie.name == "name1") { |
407 Expect.equals("value1", cookie.value); | 383 Expect.equals("value1", cookie.value); |
408 DateTime date = new DateTime.utc(2014, DateTime.JAN, 5, 23, 59, 59, 0)
; | 384 DateTime date = |
409 Expect.equals(date, cookie.expires); | 385 new DateTime.utc(2014, DateTime.JAN, 5, 23, 59, 59, 0); |
410 Expect.equals("www.example.com", cookie.domain); | 386 Expect.equals(date, cookie.expires); |
411 Expect.isTrue(cookie.httpOnly); | 387 Expect.equals("www.example.com", cookie.domain); |
412 } else if (cookie.name == "name2") { | 388 Expect.isTrue(cookie.httpOnly); |
413 Expect.equals("value2", cookie.value); | 389 } else if (cookie.name == "name2") { |
414 Expect.equals(100, cookie.maxAge); | 390 Expect.equals("value2", cookie.value); |
415 Expect.equals(".example.com", cookie.domain); | 391 Expect.equals(100, cookie.maxAge); |
416 Expect.equals("/shop", cookie.path); | 392 Expect.equals(".example.com", cookie.domain); |
417 } else { | 393 Expect.equals("/shop", cookie.path); |
418 Expect.fail("Unexpected cookie"); | 394 } else { |
419 } | 395 Expect.fail("Unexpected cookie"); |
420 }); | 396 } |
421 HttpClientConnection conn2 = | 397 }); |
422 httpClient.get("127.0.0.1", port, "/cookie2"); | 398 |
423 conn2.onRequest = (HttpClientRequest request) { | 399 response.listen( |
424 request.cookies.add(response.cookies[0]); | 400 (_) { }, |
425 request.cookies.add(response.cookies[1]); | 401 onDone: () { |
426 request.outputStream.close(); | 402 httpClient.get("127.0.0.1", port, "/cookie2") |
427 }; | 403 .then((request) { |
428 conn2.onResponse = (HttpClientResponse response) { | 404 request.cookies.add(response.cookies[0]); |
429 response.inputStream.onData = response.inputStream.read; | 405 request.cookies.add(response.cookies[1]); |
430 response.inputStream.onClosed = () { | 406 return request.close(); |
431 httpClient.shutdown(); | 407 }) |
432 testServerMain.shutdown(); | 408 .then((response) { |
433 completer.complete(true); | 409 response.listen( |
434 }; | 410 (_) { }, |
435 }; | 411 onDone: () { |
436 }; | 412 httpClient.close(); |
| 413 server.shutdown(); |
| 414 completer.complete(true); |
| 415 }); |
| 416 }); |
| 417 }); |
| 418 }); |
437 }); | 419 }); |
438 testServerMain.start(); | 420 server.start(); |
439 return completer.future; | |
440 } | |
441 | |
442 Future testFlush() { | |
443 Completer completer = new Completer(); | |
444 TestServerMain testServerMain = new TestServerMain(); | |
445 testServerMain.setServerStartedHandler((int port) { | |
446 HttpClient httpClient = new HttpClient(); | |
447 | |
448 HttpClientConnection conn = httpClient.get("127.0.0.1", port, "/flush"); | |
449 conn.onRequest = (HttpClientRequest request) { | |
450 request.outputStream.flush(); | |
451 request.outputStream.close(); | |
452 }; | |
453 conn.onResponse = (HttpClientResponse response) { | |
454 Expect.equals(HttpStatus.OK, response.statusCode); | |
455 response.inputStream.onData = response.inputStream.read; | |
456 response.inputStream.onClosed = () { | |
457 httpClient.shutdown(); | |
458 testServerMain.shutdown(); | |
459 completer.complete(true); | |
460 }; | |
461 }; | |
462 }); | |
463 testServerMain.start(); | |
464 return completer.future; | 421 return completer.future; |
465 } | 422 } |
466 | 423 |
467 void main() { | 424 void main() { |
468 print('testHost()'); | |
469 testHost().then((_) { | 425 testHost().then((_) { |
470 print('testExpires()'); | |
471 return testExpires().then((_) { | 426 return testExpires().then((_) { |
472 print('testContentType()'); | |
473 return testContentType().then((_) { | 427 return testContentType().then((_) { |
474 print('testCookies()'); | 428 return testCookies(); |
475 return testCookies().then((_) { | |
476 print('testFlush()'); | |
477 return testFlush(); | |
478 }); | |
479 }); | 429 }); |
480 }); | 430 }); |
481 }).then((_) { | |
482 print('done'); | |
483 }); | 431 }); |
484 } | 432 } |
OLD | NEW |