| 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 '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.fold(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( | 23 Future<String> connectGetSession( |
| 24 HttpClient client, int port, [String session]) { | 24 HttpClient client, int port, [String session]) { |
| 25 return client.get("127.0.0.1", port, "/") | 25 return client.get("127.0.0.1", port, "/") |
| 26 .then((request) { | 26 .then((request) { |
| 27 if (session != null) { | 27 if (session != null) { |
| 28 request.cookies.add(new Cookie(SESSION_ID, session)); | 28 request.cookies.add(new Cookie(SESSION_ID, session)); |
| 29 } | 29 } |
| 30 return request.close(); | 30 return request.close(); |
| 31 }) | 31 }) |
| 32 .then((response) { | 32 .then((response) { |
| 33 return response.reduce(getSessionId(response.cookies), (v, _) => v); | 33 return response.fold(getSessionId(response.cookies), (v, _) => v); |
| 34 }); | 34 }); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void testSessions(int sessionCount) { | 37 void testSessions(int sessionCount) { |
| 38 var client = new HttpClient(); | 38 var client = new HttpClient(); |
| 39 HttpServer.bind().then((server) { | 39 HttpServer.bind().then((server) { |
| 40 var sessions = new Set(); | 40 var sessions = new Set(); |
| 41 server.listen((request) { | 41 server.listen((request) { |
| 42 sessions.add(request.session.id); | 42 sessions.add(request.session.id); |
| 43 request.response.close(); | 43 request.response.close(); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 }); | 147 }); |
| 148 }); | 148 }); |
| 149 }); | 149 }); |
| 150 } | 150 } |
| 151 | 151 |
| 152 void main() { | 152 void main() { |
| 153 testSessions(1); | 153 testSessions(1); |
| 154 testTimeout(5); | 154 testTimeout(5); |
| 155 testSessionsData(); | 155 testSessionsData(); |
| 156 } | 156 } |
| OLD | NEW |