Skip to content

Commit

Permalink
Add Cookie#inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Dec 2, 2024
1 parent 28fc458 commit 2c8561a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spec/std/http/cookie_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,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
Expand Down
15 changes: 15 additions & 0 deletions src/http/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c8561a

Please sign in to comment.