OLD | NEW |
1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 const SESSION_ID = "DARTSESSID"; | 9 const SESSION_ID = "DARTSESSID"; |
10 | 10 |
11 String getSessionId(List<Cookie> cookies) { | 11 String getSessionId(List<Cookie> cookies) { |
12 var id = cookies.fold(null, (last, cookie) { | 12 var id = cookies.fold(null, (last, cookie) { |
13 if (last != null) return last; | 13 if (last != null) return last; |
14 if (cookie.name.toUpperCase() == SESSION_ID) { | 14 if (cookie.name.toUpperCase() == SESSION_ID) { |
15 Expect.isTrue(cookie.httpOnly); | 15 Expect.isTrue(cookie.httpOnly); |
16 return cookie.value; | 16 return cookie.value; |
17 } | 17 } |
18 return null; | 18 return null; |
19 }); | 19 }); |
20 Expect.isNotNull(id); | 20 Expect.isNotNull(id); |
21 return id; | 21 return id; |
22 } | 22 } |
23 | 23 |
24 Future<String> connectGetSession( | 24 Future<String> connectGetSession(HttpClient client, int port, |
25 HttpClient client, int port, [String session]) { | 25 [String session]) { |
26 return client.get("127.0.0.1", port, "/") | 26 return client.get("127.0.0.1", port, "/").then((request) { |
27 .then((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 return request.close(); | 31 }).then((response) { |
32 }) | 32 return response.fold(getSessionId(response.cookies), (v, _) => v); |
33 .then((response) { | 33 }); |
34 return response.fold(getSessionId(response.cookies), (v, _) => v); | |
35 }); | |
36 } | 34 } |
37 | 35 |
38 void testSessions(int sessionCount) { | 36 void testSessions(int sessionCount) { |
39 var client = new HttpClient(); | 37 var client = new HttpClient(); |
40 HttpServer.bind("127.0.0.1", 0).then((server) { | 38 HttpServer.bind("127.0.0.1", 0).then((server) { |
41 var sessions = new Set(); | 39 var sessions = new Set(); |
42 server.listen((request) { | 40 server.listen((request) { |
43 sessions.add(request.session.id); | 41 sessions.add(request.session.id); |
44 request.response.close(); | 42 request.response.close(); |
45 }); | 43 }); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 }); | 78 }); |
81 | 79 |
82 var futures = []; | 80 var futures = []; |
83 for (int i = 0; i < sessionCount; i++) { | 81 for (int i = 0; i < sessionCount; i++) { |
84 futures.add(connectGetSession(client, server.port)); | 82 futures.add(connectGetSession(client, server.port)); |
85 } | 83 } |
86 Future.wait(futures).then((clientSessions) { | 84 Future.wait(futures).then((clientSessions) { |
87 Future.wait(timeouts).then((_) { | 85 Future.wait(timeouts).then((_) { |
88 futures = []; | 86 futures = []; |
89 for (var id in clientSessions) { | 87 for (var id in clientSessions) { |
90 futures.add(connectGetSession( | 88 futures |
91 client, server.port, id).then((session) { | 89 .add(connectGetSession(client, server.port, id).then((session) { |
92 Expect.isNotNull(session); | 90 Expect.isNotNull(session); |
93 Expect.notEquals(id, session); | 91 Expect.notEquals(id, session); |
94 })); | 92 })); |
95 } | 93 } |
96 Future.wait(futures).then((_) { | 94 Future.wait(futures).then((_) { |
97 server.close(); | 95 server.close(); |
98 client.close(); | 96 client.close(); |
99 }); | 97 }); |
100 }); | 98 }); |
101 }); | 99 }); |
(...skipping 11 matching lines...) Expand all Loading... |
113 Expect.isFalse(firstHit); | 111 Expect.isFalse(firstHit); |
114 Expect.isFalse(secondHit); | 112 Expect.isFalse(secondHit); |
115 firstHit = true; | 113 firstHit = true; |
116 session["data"] = "some data"; | 114 session["data"] = "some data"; |
117 } else { | 115 } else { |
118 Expect.isTrue(firstHit); | 116 Expect.isTrue(firstHit); |
119 Expect.isFalse(secondHit); | 117 Expect.isFalse(secondHit); |
120 secondHit = true; | 118 secondHit = true; |
121 Expect.isTrue(session.containsKey("data")); | 119 Expect.isTrue(session.containsKey("data")); |
122 Expect.equals("some data", session["data"]); | 120 Expect.equals("some data", session["data"]); |
123 }; | 121 } |
| 122 ; |
124 request.response.close(); | 123 request.response.close(); |
125 }); | 124 }); |
126 | 125 |
127 var client = new HttpClient(); | 126 var client = new HttpClient(); |
128 client.get("127.0.0.1", server.port, "/") | 127 client |
129 .then((request) => request.close()) | 128 .get("127.0.0.1", server.port, "/") |
130 .then((response) { | 129 .then((request) => request.close()) |
131 response.listen((_) {}, onDone: () { | 130 .then((response) { |
132 var id = getSessionId(response.cookies); | 131 response.listen((_) {}, onDone: () { |
133 Expect.isNotNull(id); | 132 var id = getSessionId(response.cookies); |
134 client.get("127.0.0.1", server.port, "/") | 133 Expect.isNotNull(id); |
135 .then((request) { | 134 client.get("127.0.0.1", server.port, "/").then((request) { |
136 request.cookies.add(new Cookie(SESSION_ID, id)); | 135 request.cookies.add(new Cookie(SESSION_ID, id)); |
137 return request.close(); | 136 return request.close(); |
138 }) | 137 }).then((response) { |
139 .then((response) { | 138 response.listen((_) {}, onDone: () { |
140 response.listen((_) {}, onDone: () { | 139 Expect.isTrue(firstHit); |
141 Expect.isTrue(firstHit); | 140 Expect.isTrue(secondHit); |
142 Expect.isTrue(secondHit); | 141 Expect.equals(id, getSessionId(response.cookies)); |
143 Expect.equals(id, getSessionId(response.cookies)); | 142 server.close(); |
144 server.close(); | 143 client.close(); |
145 client.close(); | 144 }); |
146 }); | |
147 }); | |
148 }); | 145 }); |
149 }); | 146 }); |
| 147 }); |
150 }); | 148 }); |
151 } | 149 } |
152 | 150 |
153 void testSessionsDestroy() { | 151 void testSessionsDestroy() { |
154 HttpServer.bind("127.0.0.1", 0).then((server) { | 152 HttpServer.bind("127.0.0.1", 0).then((server) { |
155 bool firstHit = false; | 153 bool firstHit = false; |
156 server.listen((request) { | 154 server.listen((request) { |
157 var session = request.session; | 155 var session = request.session; |
158 if (session.isNew) { | 156 if (session.isNew) { |
159 Expect.isFalse(firstHit); | 157 Expect.isFalse(firstHit); |
160 firstHit = true; | 158 firstHit = true; |
161 } else { | 159 } else { |
162 Expect.isTrue(firstHit); | 160 Expect.isTrue(firstHit); |
163 session.destroy(); | 161 session.destroy(); |
164 var session2 = request.session; | 162 var session2 = request.session; |
165 Expect.notEquals(session.id, session2.id); | 163 Expect.notEquals(session.id, session2.id); |
166 }; | 164 } |
| 165 ; |
167 request.response.close(); | 166 request.response.close(); |
168 }); | 167 }); |
169 | 168 |
170 var client = new HttpClient(); | 169 var client = new HttpClient(); |
171 client.get("127.0.0.1", server.port, "/") | 170 client |
172 .then((request) => request.close()) | 171 .get("127.0.0.1", server.port, "/") |
173 .then((response) { | 172 .then((request) => request.close()) |
174 response.listen((_) {}, onDone: () { | 173 .then((response) { |
175 var id = getSessionId(response.cookies); | 174 response.listen((_) {}, onDone: () { |
176 Expect.isNotNull(id); | 175 var id = getSessionId(response.cookies); |
177 client.get("127.0.0.1", server.port, "/") | 176 Expect.isNotNull(id); |
178 .then((request) { | 177 client.get("127.0.0.1", server.port, "/").then((request) { |
179 request.cookies.add(new Cookie(SESSION_ID, id)); | 178 request.cookies.add(new Cookie(SESSION_ID, id)); |
180 return request.close(); | 179 return request.close(); |
181 }) | 180 }).then((response) { |
182 .then((response) { | 181 response.listen((_) {}, onDone: () { |
183 response.listen((_) {}, onDone: () { | 182 Expect.isTrue(firstHit); |
184 Expect.isTrue(firstHit); | 183 Expect.notEquals(id, getSessionId(response.cookies)); |
185 Expect.notEquals(id, getSessionId(response.cookies)); | 184 server.close(); |
186 server.close(); | 185 client.close(); |
187 client.close(); | 186 }); |
188 }); | |
189 }); | |
190 }); | 187 }); |
191 }); | 188 }); |
| 189 }); |
192 }); | 190 }); |
193 } | 191 } |
194 | 192 |
195 void main() { | 193 void main() { |
196 testSessions(1); | 194 testSessions(1); |
197 testTimeout(5); | 195 testTimeout(5); |
198 testSessionsData(); | 196 testSessionsData(); |
199 testSessionsDestroy(); | 197 testSessionsDestroy(); |
200 } | 198 } |
OLD | NEW |