diff --git a/spec/std/http/cookie_spec.cr b/spec/std/http/cookie_spec.cr index 5f937a8eaf06..f5c4fffee48e 100644 --- a/spec/std/http/cookie_spec.cr +++ b/spec/std/http/cookie_spec.cr @@ -172,6 +172,13 @@ module HTTP end end + describe "#inspect" do + it "stringifies" do + HTTP::Cookie.new("foo", "bar").inspect.should eq %(HTTP::Cookie["foo=bar"]) + HTTP::Cookie.new("x", "y", domain: "example.com", path: "/foo", expires: Time.unix(1257894000), samesite: :lax).inspect.should eq %(HTTP::Cookie["x=y; domain=example.com; path=/foo; expires=Tue, 10 Nov 2009 23:00:00 GMT; SameSite=Lax"]) + end + end + describe "#valid? & #validate!" do it "raises on invalid cookie with __Secure- prefix" do cookie = HTTP::Cookie.new "x", "", secure: false diff --git a/src/http/cookie.cr b/src/http/cookie.cr index 05bc32e6ae24..16862b08502e 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -104,6 +104,21 @@ module HTTP end end + # Returns an unambiguous string representation of this cookie. + # + # It uses the `Set-Cookie` serialization from `#to_set_cookie_header` which + # represents the full state of the cookie. + # + # ``` + # HTTP::Cookie.new("foo", "bar").inspect # => HTTP::Cookie["foo=bar"] + # HTTP::Cookie.new("foo", "bar", domain: "example.com").inspect # => HTTP::Cookie["foo=bar; domain=example.com"] + # ``` + def inspect(io : IO) : Nil + io << "HTTP::Cookie[" + to_s.inspect(io) + io << "]" + end + # Returns a string representation of this cookie. # # It uses the `Set-Cookie` serialization from `#to_set_cookie_header` which