diff --git a/spec/std/http/http_spec.cr b/spec/std/http/http_spec.cr index 6159cdc9dc5e..84eb50197ad7 100644 --- a/spec/std/http/http_spec.cr +++ b/spec/std/http/http_spec.cr @@ -11,39 +11,45 @@ private def http_quote_string(string) end describe HTTP do - it "parses RFC 1123" do - time = Time.utc(1994, 11, 6, 8, 49, 37) - HTTP.parse_time("Sun, 06 Nov 1994 08:49:37 GMT").should eq(time) - end + describe ".parse_time" do + it "parses RFC 1123" do + time = Time.utc(1994, 11, 6, 8, 49, 37) + HTTP.parse_time("Sun, 06 Nov 1994 08:49:37 GMT").should eq(time) + end - it "parses RFC 1123 without day name" do - time = Time.utc(1994, 11, 6, 8, 49, 37) - HTTP.parse_time("06 Nov 1994 08:49:37 GMT").should eq(time) - end + it "parses RFC 1123 without day name" do + time = Time.utc(1994, 11, 6, 8, 49, 37) + HTTP.parse_time("06 Nov 1994 08:49:37 GMT").should eq(time) + end - it "parses RFC 1036" do - time = Time.utc(1994, 11, 6, 8, 49, 37) - HTTP.parse_time("Sunday, 06-Nov-94 08:49:37 GMT").should eq(time) - end + it "parses RFC 1036" do + time = Time.utc(1994, 11, 6, 8, 49, 37) + HTTP.parse_time("Sunday, 06-Nov-94 08:49:37 GMT").should eq(time) + end - it "parses ANSI C" do - time = Time.utc(1994, 11, 6, 8, 49, 37) - HTTP.parse_time("Sun Nov 6 08:49:37 1994").should eq(time) - time2 = Time.utc(1994, 11, 16, 8, 49, 37) - HTTP.parse_time("Sun Nov 16 08:49:37 1994").should eq(time2) - end + it "parses ANSI C" do + time = Time.utc(1994, 11, 6, 8, 49, 37) + HTTP.parse_time("Sun Nov 6 08:49:37 1994").should eq(time) + time2 = Time.utc(1994, 11, 16, 8, 49, 37) + HTTP.parse_time("Sun Nov 16 08:49:37 1994").should eq(time2) + end - it "parses and is UTC (#2744)" do - date = "Mon, 09 Sep 2011 23:36:00 GMT" - parsed_time = HTTP.parse_time(date).not_nil! - parsed_time.utc?.should be_true - end + it "parses and is UTC (#2744)" do + date = "Mon, 09 Sep 2011 23:36:00 GMT" + parsed_time = HTTP.parse_time(date).not_nil! + parsed_time.utc?.should be_true + end - it "parses and is local (#2744)" do - date = "Mon, 09 Sep 2011 23:36:00 -0300" - parsed_time = HTTP.parse_time(date).not_nil! - parsed_time.offset.should eq -3 * 3600 - parsed_time.to_utc.to_s.should eq("2011-09-10 02:36:00 UTC") + it "parses and is local (#2744)" do + date = "Mon, 09 Sep 2011 23:36:00 -0300" + parsed_time = HTTP.parse_time(date).not_nil! + parsed_time.offset.should eq -3 * 3600 + parsed_time.to_utc.to_s.should eq("2011-09-10 02:36:00 UTC") + end + + it "handles errors" do + HTTP.parse_time("Thu").should be_nil + end end describe "generates HTTP date" do diff --git a/src/time/format/custom/http_date.cr b/src/time/format/custom/http_date.cr index d9ca38b9d7e5..25847b21aa00 100644 --- a/src/time/format/custom/http_date.cr +++ b/src/time/format/custom/http_date.cr @@ -102,6 +102,7 @@ struct Time::Format ansi_c_format = current_char != ',' next_char unless ansi_c_format + raise "Invalid date format" unless current_char.ascii_whitespace? whitespace ansi_c_format