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"); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 // Set the "Expires" header. | 147 // Set the "Expires" header. |
148 void _expires2Handler(HttpRequest request, HttpResponse response) { | 148 void _expires2Handler(HttpRequest request, HttpResponse response) { |
149 response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT"); | 149 response.headers.set("Expires", "Fri, 11 Jun 1999 18:46:53 GMT"); |
150 Date date = | 150 Date date = |
151 new Date.withTimeZone( | 151 new Date.withTimeZone( |
152 1999, Date.JUN, 11, 18, 46, 53, 0, new TimeZone.utc()); | 152 1999, Date.JUN, 11, 18, 46, 53, 0, new TimeZone.utc()); |
153 Expect.equals(date, response.headers.expires); | 153 Expect.equals(date, response.headers.expires); |
154 response.outputStream.close(); | 154 response.outputStream.close(); |
155 } | 155 } |
156 | 156 |
157 void _contentType1Handler(HttpRequest request, HttpResponse response) { | |
158 Expect.equals("text/html", request.headers.contentType.value); | |
159 Expect.equals("text", request.headers.contentType.primaryType); | |
160 Expect.equals("html", request.headers.contentType.subType); | |
161 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); | |
162 | |
163 response.headers.contentType.value = "text/html"; | |
164 response.headers.contentType.parameters["charset"] = "utf-8"; | |
165 response.outputStream.close(); | |
166 } | |
167 | |
168 void _contentType2Handler(HttpRequest request, HttpResponse response) { | |
169 Expect.equals("text/html", request.headers.contentType.value); | |
170 Expect.equals("text", request.headers.contentType.primaryType); | |
171 Expect.equals("html", request.headers.contentType.subType); | |
172 Expect.equals("utf-8", request.headers.contentType.parameters["charset"]); | |
173 | |
174 response.headers.set(HttpHeaders.CONTENT_TYPE, "text/html; charset = utf-8" ); | |
Mads Ager (google)
2012/05/21 07:40:39
Long line. Also multiple long lines below.
Søren Gjesse
2012/05/21 11:11:06
Done.
| |
175 response.outputStream.close(); | |
176 } | |
177 | |
157 void main() { | 178 void main() { |
158 // Setup request handlers. | 179 // Setup request handlers. |
159 _requestHandlers = new Map(); | 180 _requestHandlers = new Map(); |
160 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { | 181 _requestHandlers["/echo"] = (HttpRequest request, HttpResponse response) { |
161 _echoHandler(request, response); | 182 _echoHandler(request, response); |
162 }; | 183 }; |
163 _requestHandlers["/0123456789"] = | 184 _requestHandlers["/0123456789"] = |
164 (HttpRequest request, HttpResponse response) { | 185 (HttpRequest request, HttpResponse response) { |
165 _zeroToTenHandler(request, response); | 186 _zeroToTenHandler(request, response); |
166 }; | 187 }; |
167 _requestHandlers["/reasonformoving"] = | 188 _requestHandlers["/reasonformoving"] = |
168 (HttpRequest request, HttpResponse response) { | 189 (HttpRequest request, HttpResponse response) { |
169 _reasonForMovingHandler(request, response); | 190 _reasonForMovingHandler(request, response); |
170 }; | 191 }; |
171 _requestHandlers["/host"] = | 192 _requestHandlers["/host"] = |
172 (HttpRequest request, HttpResponse response) { | 193 (HttpRequest request, HttpResponse response) { |
173 _hostHandler(request, response); | 194 _hostHandler(request, response); |
174 }; | 195 }; |
175 _requestHandlers["/expires1"] = | 196 _requestHandlers["/expires1"] = |
176 (HttpRequest request, HttpResponse response) { | 197 (HttpRequest request, HttpResponse response) { |
177 _expires1Handler(request, response); | 198 _expires1Handler(request, response); |
178 }; | 199 }; |
179 _requestHandlers["/expires2"] = | 200 _requestHandlers["/expires2"] = |
180 (HttpRequest request, HttpResponse response) { | 201 (HttpRequest request, HttpResponse response) { |
181 _expires2Handler(request, response); | 202 _expires2Handler(request, response); |
182 }; | 203 }; |
204 _requestHandlers["/contenttype1"] = | |
205 (HttpRequest request, HttpResponse response) { | |
206 _contentType1Handler(request, response); | |
207 }; | |
208 _requestHandlers["/contenttype2"] = | |
209 (HttpRequest request, HttpResponse response) { | |
210 _contentType2Handler(request, response); | |
211 }; | |
183 | 212 |
184 this.port.receive((var message, SendPort replyTo) { | 213 this.port.receive((var message, SendPort replyTo) { |
185 if (message.isStart) { | 214 if (message.isStart) { |
186 _server = new HttpServer(); | 215 _server = new HttpServer(); |
187 try { | 216 try { |
188 _server.listen("127.0.0.1", 0); | 217 _server.listen("127.0.0.1", 0); |
189 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { | 218 _server.defaultRequestHandler = (HttpRequest req, HttpResponse rsp) { |
190 _requestReceivedHandler(req, rsp); | 219 _requestReceivedHandler(req, rsp); |
191 }; | 220 }; |
192 replyTo.send(new TestServerStatus.started(_server.port), null); | 221 replyTo.send(new TestServerStatus.started(_server.port), null); |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 processResponse(response); | 536 processResponse(response); |
508 }; | 537 }; |
509 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); | 538 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/expires2"); |
510 conn2.onResponse = (HttpClientResponse response) { | 539 conn2.onResponse = (HttpClientResponse response) { |
511 processResponse(response); | 540 processResponse(response); |
512 }; | 541 }; |
513 }); | 542 }); |
514 testServerMain.start(); | 543 testServerMain.start(); |
515 } | 544 } |
516 | 545 |
546 void testContentType() { | |
547 TestServerMain testServerMain = new TestServerMain(); | |
548 testServerMain.setServerStartedHandler((int port) { | |
549 int responses = 0; | |
550 HttpClient httpClient = new HttpClient(); | |
551 | |
552 void processResponse(HttpClientResponse response) { | |
553 Expect.equals(HttpStatus.OK, response.statusCode); | |
554 Expect.equals("text/html; charset=utf-8", | |
555 response.headers.contentType.toString()); | |
556 Expect.equals("text/html", response.headers.contentType.value); | |
557 Expect.equals("text", response.headers.contentType.primaryType); | |
558 Expect.equals("html", response.headers.contentType.subType); | |
559 Expect.equals("utf-8", response.headers.contentType.parameters["charset"]) ; | |
560 responses++; | |
561 if (responses == 2) { | |
562 httpClient.shutdown(); | |
563 testServerMain.shutdown(); | |
564 } | |
565 } | |
566 | |
567 HttpClientConnection conn1 = httpClient.get("127.0.0.1", port, "/contenttype 1"); | |
568 conn1.onRequest = (HttpClientRequest request) { | |
569 request.headers.contentType.value = "text/html"; | |
570 request.headers.contentType.parameters["charset"] = "utf-8"; | |
571 request.outputStream.close(); | |
572 }; | |
573 conn1.onResponse = (HttpClientResponse response) { | |
574 processResponse(response); | |
575 }; | |
576 HttpClientConnection conn2 = httpClient.get("127.0.0.1", port, "/contenttype 2"); | |
577 conn2.onRequest = (HttpClientRequest request) { | |
578 request.headers.set(HttpHeaders.CONTENT_TYPE, "text/html; charset = utf-8 "); | |
579 request.outputStream.close(); | |
580 }; | |
581 conn2.onResponse = (HttpClientResponse response) { | |
582 processResponse(response); | |
583 }; | |
584 }); | |
585 testServerMain.start(); | |
586 } | |
587 | |
517 | 588 |
518 void main() { | 589 void main() { |
519 testStartStop(); | 590 /*testStartStop(); |
Anders Johnsen
2012/05/21 07:37:30
We should still run every test ;)
Mads Ager (google)
2012/05/21 07:40:39
Code in comments.
Søren Gjesse
2012/05/21 11:11:06
Done.
Søren Gjesse
2012/05/21 11:11:06
Done.
| |
520 testGET(); | 591 testGET(); |
521 testPOST(true); | 592 testPOST(true); |
522 testPOST(false); | 593 testPOST(false); |
523 testReadInto(true); | 594 testReadInto(true); |
524 testReadInto(false); | 595 testReadInto(false); |
525 testReadShort(true); | 596 testReadShort(true); |
526 testReadShort(false); | 597 testReadShort(false); |
527 test404(); | 598 test404(); |
528 testReasonPhrase(); | 599 testReasonPhrase(); |
529 testHost(); | 600 testHost(); |
530 testExpires(); | 601 testExpires();*/ |
602 testContentType(); | |
531 } | 603 } |
OLD | NEW |