| OLD | NEW |
| (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 #source("../../../../runtime/bin/http_utils.dart"); | |
| 6 | |
| 7 class HttpException implements Exception { | |
| 8 const HttpException([String this.message = ""]); | |
| 9 String toString() => "HttpException: $message"; | |
| 10 final String message; | |
| 11 } | |
| 12 | |
| 13 void testParseHttpDate() { | |
| 14 TimeZone utc = new TimeZone.utc(); | |
| 15 Date date; | |
| 16 date = new Date.withTimeZone(1999, Date.JUN, 11, 18, 46, 53, 0, utc); | |
| 17 Expect.equals(date, _HttpUtils.parseDate("Fri, 11 Jun 1999 18:46:53 GMT")); | |
| 18 Expect.equals(date, _HttpUtils.parseDate("Friday, 11-Jun-1999 18:46:53 GMT")); | |
| 19 Expect.equals(date, _HttpUtils.parseDate("Fri Jun 11 18:46:53 1999")); | |
| 20 | |
| 21 date = new Date.withTimeZone(1970, Date.JAN, 1, 0, 0, 0, 0, utc); | |
| 22 Expect.equals(date, _HttpUtils.parseDate("Thu, 1 Jan 1970 00:00:00 GMT")); | |
| 23 Expect.equals(date, | |
| 24 _HttpUtils.parseDate("Thursday, 1-Jan-1970 00:00:00 GMT")); | |
| 25 Expect.equals(date, _HttpUtils.parseDate("Thu Jan 1 00:00:00 1970")); | |
| 26 | |
| 27 date = new Date.withTimeZone(2012, Date.MAR, 5, 23, 59, 59, 0, utc); | |
| 28 Expect.equals(date, _HttpUtils.parseDate("Mon, 5 Mar 2012 23:59:59 GMT")); | |
| 29 Expect.equals(date, _HttpUtils.parseDate("Monday, 5-Mar-2012 23:59:59 GMT")); | |
| 30 Expect.equals(date, _HttpUtils.parseDate("Mon Mar 5 23:59:59 2012")); | |
| 31 } | |
| 32 | |
| 33 void testFormatParseHttpDate() { | |
| 34 test(int year, | |
| 35 int month, | |
| 36 int day, | |
| 37 int hours, | |
| 38 int minutes, | |
| 39 int seconds, | |
| 40 String expectedFormatted) { | |
| 41 TimeZone utc = new TimeZone.utc(); | |
| 42 Date date; | |
| 43 String formatted; | |
| 44 date = new Date.withTimeZone( | |
| 45 year, month, day, hours, minutes, seconds, 0, utc); | |
| 46 formatted = _HttpUtils.formatDate(date); | |
| 47 Expect.equals(expectedFormatted, formatted); | |
| 48 Expect.equals(date, _HttpUtils.parseDate(formatted)); | |
| 49 } | |
| 50 | |
| 51 test(1999, Date.JUN, 11, 18, 46, 53, "Fri, 11 Jun 1999 18:46:53 GMT"); | |
| 52 test(1970, Date.JAN, 1, 0, 0, 0, "Thu, 1 Jan 1970 00:00:00 GMT"); | |
| 53 test(2012, Date.MAR, 5, 23, 59, 59, "Mon, 5 Mar 2012 23:59:59 GMT"); | |
| 54 } | |
| 55 | |
| 56 void testParseHttpDateFailures() { | |
| 57 Expect.throws(() { | |
| 58 _HttpUtils.parseDate(""); | |
| 59 }); | |
| 60 String valid = "Mon, 5 Mar 2012 23:59:59 GMT"; | |
| 61 for (int i = 1; i < valid.length - 1; i++) { | |
| 62 String tmp = valid.substring(0, i); | |
| 63 Expect.throws(() { | |
| 64 _HttpUtils.parseDate(tmp); | |
| 65 }); | |
| 66 Expect.throws(() { | |
| 67 _HttpUtils.parseDate(" $tmp"); | |
| 68 }); | |
| 69 Expect.throws(() { | |
| 70 _HttpUtils.parseDate(" $tmp "); | |
| 71 }); | |
| 72 Expect.throws(() { | |
| 73 _HttpUtils.parseDate("$tmp "); | |
| 74 }); | |
| 75 } | |
| 76 Expect.throws(() { | |
| 77 _HttpUtils.parseDate(" $valid"); | |
| 78 }); | |
| 79 Expect.throws(() { | |
| 80 _HttpUtils.parseDate(" $valid "); | |
| 81 }); | |
| 82 Expect.throws(() { | |
| 83 _HttpUtils.parseDate("$valid "); | |
| 84 }); | |
| 85 } | |
| 86 | |
| 87 void main() { | |
| 88 testParseHttpDate(); | |
| 89 testFormatParseHttpDate(); | |
| 90 testParseHttpDateFailures(); | |
| 91 } | |
| OLD | NEW |