From c33869463abb8101bcf2b377ecaeb230d377d657 Mon Sep 17 00:00:00 2001 From: Dhurba Baral Date: Sat, 23 May 2026 23:51:16 +1000 Subject: [PATCH] Add custom #inspect to core classes for console readability --- lib/wreq_ruby/client.rb | 8 +++ lib/wreq_ruby/cookie.rb | 19 ++++++ lib/wreq_ruby/header.rb | 8 +++ lib/wreq_ruby/http.rb | 14 +++++ lib/wreq_ruby/response.rb | 20 ++++-- test/inspect_test.rb | 125 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 test/inspect_test.rb diff --git a/lib/wreq_ruby/client.rb b/lib/wreq_ruby/client.rb index 2dc5f23..6eb4368 100644 --- a/lib/wreq_ruby/client.rb +++ b/lib/wreq_ruby/client.rb @@ -514,3 +514,11 @@ def patch(url, **options) end end end + +module Wreq + class Client + def inspect + "#" + end + end +end diff --git a/lib/wreq_ruby/cookie.rb b/lib/wreq_ruby/cookie.rb index b008544..faba5a4 100644 --- a/lib/wreq_ruby/cookie.rb +++ b/lib/wreq_ruby/cookie.rb @@ -142,3 +142,22 @@ def clear end end end + +module Wreq + class Cookie + def inspect + parts = ["#" + end + end + + class Jar + def inspect + "#" + end + end +end diff --git a/lib/wreq_ruby/header.rb b/lib/wreq_ruby/header.rb index e9c8024..b6ce5c8 100644 --- a/lib/wreq_ruby/header.rb +++ b/lib/wreq_ruby/header.rb @@ -195,3 +195,11 @@ def to_s end end end + +module Wreq + class Headers + def inspect + "#" + end + end +end diff --git a/lib/wreq_ruby/http.rb b/lib/wreq_ruby/http.rb index 82cf2da..c53cdde 100644 --- a/lib/wreq_ruby/http.rb +++ b/lib/wreq_ruby/http.rb @@ -130,3 +130,17 @@ def to_s end end end + +module Wreq + class StatusCode + def inspect + "#" + end + end + + class Version + def inspect + "#" + end + end +end diff --git a/lib/wreq_ruby/response.rb b/lib/wreq_ruby/response.rb index 9c8d4f9..e312ab6 100644 --- a/lib/wreq_ruby/response.rb +++ b/lib/wreq_ruby/response.rb @@ -156,27 +156,35 @@ def close module Wreq class Response - # Returns a compact string representation of the response. + # Returns the response body as a string. + # + # @return [String] Response body text + # @example + # puts response.to_s + # puts response + # File.write("page.html", response) + def to_s + text + end + + # Returns a compact string representation for debugging. # # Format: # # # @return [String] Compact formatted response information # @example - # puts response.to_s + # p response # # => # - def to_s + def inspect parts = ["#", headers.inspect + end + + def test_headers_inspect_with_entries + headers = Wreq::Headers.new + headers.set("Content-Type", "text/html") + headers.set("Accept", "application/json") + assert_equal "#", headers.inspect + end + + # ---- Cookie ---- + + def test_cookie_inspect_minimal + c = Wreq::Cookie.new("sid", "secret123") + result = c.inspect + assert_includes result, "#") + end + + def test_cookie_inspect_with_domain_and_path + c = Wreq::Cookie.new("sid", "val", + domain: "example.com", + path: "/app") + result = c.inspect + assert_includes result, "domain=example.com" + assert_includes result, "path=/app" + end + + def test_cookie_inspect_with_flags + c = Wreq::Cookie.new("sid", "val", + secure: true, + http_only: true) + result = c.inspect + assert_includes result, "secure" + assert_includes result, "http_only" + end + + def test_cookie_inspect_omits_nil_attributes + c = Wreq::Cookie.new("sid", "val") + result = c.inspect + refute_includes result, "domain=" + refute_includes result, "path=" + refute_includes result, "secure" + refute_includes result, "http_only" + end + + # ---- Jar ---- + + def test_jar_inspect_empty + jar = Wreq::Jar.new + assert_equal "#", jar.inspect + end + + def test_jar_inspect_with_cookies + jar = Wreq::Jar.new + jar.add_cookie_str("a=1; Path=/", "https://example.com") + jar.add_cookie_str("b=2; Path=/", "https://example.com") + assert_equal "#", jar.inspect + end + + # ---- Client ---- + + def test_client_inspect + client = Wreq::Client.new + assert_equal "#", client.inspect + end + + def test_client_inspect_with_options + client = Wreq::Client.new(timeout: 30, gzip: true) + assert_equal "#", client.inspect + end + + # ---- Response ---- + + def test_response_to_s_returns_body + response = Wreq.get("http://localhost:8080/json") + assert_equal response.text, response.to_s + end + + def test_response_inspect_format + response = Wreq.get("http://localhost:8080/json") + result = response.inspect + assert result.start_with?("#") + end + + # ---- StatusCode ---- + + def test_status_code_inspect + response = Wreq.get("http://localhost:8080/status/200") + result = response.status.inspect + assert result.start_with?("#") + end + + # ---- Version ---- + + def test_version_inspect_from_constant + v = Wreq::Version::HTTP_11 + result = v.inspect + assert result.start_with?("#") + end + + def test_version_inspect_from_response + response = Wreq.get("http://localhost:8080/get") + result = response.version.inspect + assert result.start_with?("#") + end +end