Migrated http_date_test from SDK. (#57)
* Migrated http_date_test from SDK.
diff --git a/test/http_date_test.dart b/test/http_date_test.dart
new file mode 100644
index 0000000..47c5e30
--- /dev/null
+++ b/test/http_date_test.dart
@@ -0,0 +1,81 @@
+// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:http_io/http_io.dart';
+import 'package:test/test.dart';
+
+void testParseHttpDate() {
+ DateTime date;
+ date = new DateTime.utc(1999, DateTime.JUNE, 11, 18, 46, 53, 0);
+ expect(date, HttpDate.parse("Fri, 11 Jun 1999 18:46:53 GMT"));
+ expect(date, HttpDate.parse("Friday, 11-Jun-1999 18:46:53 GMT"));
+ expect(date, HttpDate.parse("Fri Jun 11 18:46:53 1999"));
+
+ date = new DateTime.utc(1970, DateTime.JANUARY, 1, 0, 0, 0, 0);
+ expect(date, HttpDate.parse("Thu, 1 Jan 1970 00:00:00 GMT"));
+ expect(date, HttpDate.parse("Thursday, 1-Jan-1970 00:00:00 GMT"));
+ expect(date, HttpDate.parse("Thu Jan 1 00:00:00 1970"));
+
+ date = new DateTime.utc(2012, DateTime.MARCH, 5, 23, 59, 59, 0);
+ expect(date, HttpDate.parse("Mon, 5 Mar 2012 23:59:59 GMT"));
+ expect(date, HttpDate.parse("Monday, 5-Mar-2012 23:59:59 GMT"));
+ expect(date, HttpDate.parse("Mon Mar 5 23:59:59 2012"));
+}
+
+void testFormatParseHttpDate() {
+ test(int year, int month, int day, int hours, int minutes, int seconds,
+ String expectedFormatted) {
+ DateTime date;
+ String formatted;
+ date = new DateTime.utc(year, month, day, hours, minutes, seconds, 0);
+ formatted = HttpDate.format(date);
+ expect(expectedFormatted, formatted);
+ expect(date, HttpDate.parse(formatted));
+ }
+
+ test(1999, DateTime.JUNE, 11, 18, 46, 53, "Fri, 11 Jun 1999 18:46:53 GMT");
+ test(1970, DateTime.JANUARY, 1, 0, 0, 0, "Thu, 01 Jan 1970 00:00:00 GMT");
+ test(1970, DateTime.JANUARY, 1, 9, 9, 9, "Thu, 01 Jan 1970 09:09:09 GMT");
+ test(2012, DateTime.MARCH, 5, 23, 59, 59, "Mon, 05 Mar 2012 23:59:59 GMT");
+}
+
+void testParseHttpDateFailures() {
+ // The calls below can throw different exceptions based on the iteration of
+ // the loop. This matcher catches all exceptions.
+ final throws = throwsA(new isInstanceOf<Object>());
+ expect(() {
+ HttpDate.parse("");
+ }, throws);
+ String valid = "Mon, 5 Mar 2012 23:59:59 GMT";
+ for (int i = 1; i < valid.length - 1; i++) {
+ String tmp = valid.substring(0, i);
+ expect(() {
+ HttpDate.parse(tmp);
+ }, throws);
+ expect(() {
+ HttpDate.parse(" $tmp");
+ }, throws);
+ expect(() {
+ HttpDate.parse(" $tmp ");
+ }, throws);
+ expect(() {
+ HttpDate.parse("$tmp ");
+ }, throws);
+ }
+ expect(() {
+ HttpDate.parse(" $valid");
+ }, throws);
+ expect(() {
+ HttpDate.parse(" $valid ");
+ }, throws);
+ expect(() {
+ HttpDate.parse("$valid ");
+ }, throws);
+}
+
+void main() {
+ test('parseHttpDate', testParseHttpDate);
+ test('formatHttpDate', testFormatParseHttpDate);
+ test('parseHttpDateFailures', testParseHttpDateFailures);
+}