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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 const SESSION_ID = "DARTSESSID"; | 8 const SESSION_ID = "DARTSESSID"; |
9 | 9 |
10 String getSessionId(List<Cookie> cookies) { | 10 String getSessionId(List<Cookie> cookies) { |
11 var id = cookies.reduce(null, (last, cookie) { | 11 var id = cookies.reduce(null, (last, cookie) { |
12 if (last != null) return last; | 12 if (last != null) return last; |
13 if (cookie.name.toUpperCase() == SESSION_ID) { | 13 if (cookie.name.toUpperCase() == SESSION_ID) { |
14 Expect.isTrue(cookie.httpOnly); | 14 Expect.isTrue(cookie.httpOnly); |
15 return cookie.value; | 15 return cookie.value; |
16 } | 16 } |
17 return null; | 17 return null; |
18 }); | 18 }); |
19 Expect.isNotNull(id); | 19 Expect.isNotNull(id); |
20 return id; | 20 return id; |
21 } | 21 } |
22 | 22 |
23 Future<String> connectGetSession(int port, [String session]) { | 23 Future<String> connectGetSession( |
24 var c = new Completer(); | 24 HttpClient client, int port, [String session]) { |
25 var client = new HttpClient(); | 25 return client.get("127.0.0.1", port, "/") |
26 var conn = client.get("127.0.0.1", port, "/"); | 26 .then((request) { |
27 conn.onRequest = (request) { | 27 if (session != null) { |
28 if (session != null) { | 28 request.cookies.add(new Cookie(SESSION_ID, session)); |
29 request.cookies.add(new Cookie(SESSION_ID, session)); | 29 } |
30 } | 30 return request.close(); |
31 request.outputStream.close(); | 31 }) |
32 }; | 32 .then((response) { |
33 conn.onResponse = (response) { | 33 return response.reduce(getSessionId(response.cookies), (v, _) => v); |
34 response.inputStream.onData = response.inputStream.read; | 34 }); |
35 response.inputStream.onClosed = () { | |
36 client.shutdown(); | |
37 c.complete(getSessionId(response.cookies)); | |
38 }; | |
39 }; | |
40 return c.future; | |
41 } | 35 } |
42 | 36 |
43 void testSessions(int sessionCount) { | 37 void testSessions(int sessionCount) { |
44 HttpServer server = new HttpServer(); | 38 var client = new HttpClient(); |
45 server.listen("127.0.0.1", 0); | 39 HttpServer.bind().then((server) { |
46 var sessions = new Set(); | 40 var sessions = new Set(); |
47 server.defaultRequestHandler = (request, response) { | 41 server.listen((request) { |
48 sessions.add(request.session().id); | 42 sessions.add(request.session.id); |
49 response.outputStream.close(); | 43 request.response.close(); |
50 }; | 44 }); |
51 | 45 |
52 var futures = []; | 46 var futures = []; |
53 for (int i = 0; i < sessionCount; i++) { | 47 for (int i = 0; i < sessionCount; i++) { |
54 futures.add(connectGetSession(server.port).then((session) { | 48 futures.add(connectGetSession(client, server.port).then((session) { |
55 Expect.isNotNull(session); | 49 Expect.isNotNull(session); |
56 Expect.isTrue(sessions.contains(session)); | 50 Expect.isTrue(sessions.contains(session)); |
57 return connectGetSession(server.port, session).then((session2) { | 51 return connectGetSession(client, server.port, session).then((session2) { |
58 Expect.equals(session2, session); | 52 Expect.equals(session2, session); |
59 Expect.isTrue(sessions.contains(session2)); | 53 Expect.isTrue(sessions.contains(session2)); |
60 return session2; | 54 return session2; |
61 }); | 55 }); |
62 })); | 56 })); |
63 } | 57 } |
64 Future.wait(futures).then((clientSessions) { | 58 Future.wait(futures).then((clientSessions) { |
65 Expect.equals(sessions.length, sessionCount); | 59 Expect.equals(sessions.length, sessionCount); |
66 Expect.setEquals(new Set.from(clientSessions), sessions); | 60 Expect.setEquals(new Set.from(clientSessions), sessions); |
67 server.close(); | 61 server.close(); |
| 62 client.close(); |
| 63 }); |
68 }); | 64 }); |
69 } | 65 } |
70 | 66 |
71 void testTimeout(int sessionCount) { | 67 void testTimeout(int sessionCount) { |
72 HttpServer server = new HttpServer(); | 68 var client = new HttpClient(); |
73 server.sessionTimeout = 0; | 69 HttpServer.bind().then((server) { |
74 server.listen("127.0.0.1", 0); | 70 server.sessionTimeout = 0; |
75 var timeouts = []; | 71 var timeouts = []; |
76 server.defaultRequestHandler = (request, response) { | 72 server.listen((request) { |
77 var c = new Completer(); | 73 var c = new Completer(); |
78 timeouts.add(c.future); | 74 timeouts.add(c.future); |
79 request.session().onTimeout = () { | 75 request.session.onTimeout = () { |
80 c.complete(null); | 76 c.complete(null); |
81 }; | 77 }; |
82 response.outputStream.close(); | 78 request.response.close(); |
83 }; | 79 }); |
84 | 80 |
85 var futures = []; | 81 var futures = []; |
86 for (int i = 0; i < sessionCount; i++) { | 82 for (int i = 0; i < sessionCount; i++) { |
87 futures.add(connectGetSession(server.port)); | 83 futures.add(connectGetSession(client, server.port)); |
88 } | 84 } |
89 Future.wait(futures).then((clientSessions) { | 85 Future.wait(futures).then((clientSessions) { |
90 Future.wait(timeouts).then((_) { | 86 Future.wait(timeouts).then((_) { |
91 futures = []; | 87 futures = []; |
92 for (var id in clientSessions) { | 88 for (var id in clientSessions) { |
93 futures.add(connectGetSession(server.port, id).then((session) { | 89 futures.add(connectGetSession( |
94 Expect.isNotNull(session); | 90 client, server.port, id).then((session) { |
95 Expect.notEquals(id, session); | 91 Expect.isNotNull(session); |
96 })); | 92 Expect.notEquals(id, session); |
97 } | 93 })); |
98 Future.wait(futures).then((_) { | 94 } |
99 server.close(); | 95 Future.wait(futures).then((_) { |
| 96 server.close(); |
| 97 client.close(); |
| 98 }); |
100 }); | 99 }); |
101 }); | 100 }); |
102 }); | 101 }); |
103 } | 102 } |
104 | 103 |
| 104 void testSessionsData() { |
| 105 HttpServer.bind().then((server) { |
| 106 bool firstHit = false; |
| 107 bool secondHit = false; |
| 108 server.listen((request) { |
| 109 var c = new Completer(); |
| 110 var session = request.session; |
| 111 if (session.isNew) { |
| 112 Expect.isFalse(firstHit); |
| 113 Expect.isFalse(secondHit); |
| 114 firstHit = true; |
| 115 session["data"] = "some data"; |
| 116 } else { |
| 117 Expect.isTrue(firstHit); |
| 118 Expect.isFalse(secondHit); |
| 119 secondHit = true; |
| 120 Expect.isTrue(session.containsKey("data")); |
| 121 Expect.equals("some data", session["data"]); |
| 122 }; |
| 123 request.response.close(); |
| 124 }); |
| 125 |
| 126 var client = new HttpClient(); |
| 127 client.get("127.0.0.1", server.port, "/") |
| 128 .then((request) => request.close()) |
| 129 .then((response) { |
| 130 response.listen((_) {}, onDone: () { |
| 131 var id = getSessionId(response.cookies); |
| 132 Expect.isNotNull(id); |
| 133 client.get("127.0.0.1", server.port, "/") |
| 134 .then((request) { |
| 135 request.cookies.add(new Cookie(SESSION_ID, id)); |
| 136 return request.close(); |
| 137 }) |
| 138 .then((response) { |
| 139 response.listen((_) {}, onDone: () { |
| 140 Expect.isTrue(firstHit); |
| 141 Expect.isTrue(secondHit); |
| 142 Expect.equals(id, getSessionId(response.cookies)); |
| 143 server.close(); |
| 144 client.close(); |
| 145 }); |
| 146 }); |
| 147 }); |
| 148 }); |
| 149 }); |
| 150 } |
| 151 |
105 void main() { | 152 void main() { |
106 testSessions(5); | 153 testSessions(1); |
107 testTimeout(5); | 154 testTimeout(5); |
| 155 testSessionsData(); |
108 } | 156 } |
OLD | NEW |