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

Side by Side Diff: tests/standalone/io/http_session_test.dart

Issue 11175030: Add sessions to HttpServer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Review update. Created 8 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
« runtime/bin/http_impl.dart ('K') | « runtime/bin/io_sources.gypi ('k') | no next file » | 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) 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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #import("dart:io");
6
7 const SESSION_ID = "DARTSESSID";
8
9 String getSessionId(List<Cookie> cookies) {
10 var id = cookies.reduce(null, (last, cookie) {
11 if (last != null) return last;
12 if (cookie.name.toUpperCase() == SESSION_ID) {
13 return cookie.value;
14 }
15 return null;
16 });
17 Expect.isNotNull(id);
18 return id;
19 }
20
21 Future<String> connectGetSession(int port, [String session]) {
22 var c = new Completer();
23 var client = new HttpClient();
24 var conn = client.get("127.0.0.1", port, "/");
25 conn.onRequest = (request) {
26 if (session != null) {
27 request.cookies.add(new Cookie(SESSION_ID, session));
28 }
29 request.outputStream.close();
30 };
31 conn.onResponse = (response) {
32 client.shutdown();
33 c.complete(getSessionId(response.cookies));
34 };
35 return c.future;
36 }
37
38 void testSessions(int sessionCount) {
39 HttpServer server = new HttpServer();
40 server.listen("127.0.0.1", 0);
41 var sessions = new Set();
42 server.defaultRequestHandler = (request, response) {
43 sessions.add(request.session().id);
44 response.outputStream.close();
45 };
46
47 var futures = [];
48 for (int i = 0; i < sessionCount; i++) {
49 futures.add(connectGetSession(server.port).chain((session) {
50 Expect.isNotNull(session);
51 Expect.isTrue(sessions.contains(session));
52 return connectGetSession(server.port, session).transform((session2) {
53 Expect.equals(session2, session);
54 Expect.isTrue(sessions.contains(session2));
55 return session2;
56 });
57 }));
58 }
59 Futures.wait(futures).then((clientSessions) {
60 Expect.equals(sessions.length, sessionCount);
61 Expect.setEquals(new Set.from(clientSessions), sessions);
62 server.close();
63 });
64 }
65
66 void testTimeout(int sessionCount) {
67 HttpServer server = new HttpServer();
68 server.sessionTimeout = 0;
69 server.listen("127.0.0.1", 0);
70 var timeouts = [];
71 server.defaultRequestHandler = (request, response) {
72 var c = new Completer();
73 timeouts.add(c.future);
74 request.session().onTimeout = () {
75 c.complete(null);
76 };
77 response.outputStream.close();
78 };
79
80 var futures = [];
81 for (int i = 0; i < sessionCount; i++) {
82 futures.add(connectGetSession(server.port));
83 }
84 Futures.wait(futures).then((clientSessions) {
85 Futures.wait(timeouts).then((_) {
86 futures = [];
87 for (var id in clientSessions) {
88 futures.add(connectGetSession(server.port, id).transform((session) {
89 Expect.isNotNull(session);
90 Expect.notEquals(id, session);
91 }));
92 }
93 Futures.wait(futures).then((_) {
94 server.close();
95 });
96 });
97 });
98 }
99
100 void main() {
101 testSessions(5);
102 testTimeout(5);
103 }
OLDNEW
« runtime/bin/http_impl.dart ('K') | « runtime/bin/io_sources.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698