From ac442ae389bf712deb5a2db0f5e9d1f4da09817e Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Tue, 16 Apr 2024 15:05:41 +1200 Subject: [PATCH] `as_json` should ignore all arguments. Add default `to_json`. (#54) --- lib/protocol/http/body/readable.rb | 6 +++++- lib/protocol/http/body/wrapper.rb | 6 +++++- lib/protocol/http/request.rb | 6 +++++- lib/protocol/http/response.rb | 6 +++++- test/protocol/http/body/readable.rb | 16 ++++++++++++++++ test/protocol/http/body/wrapper.rb | 4 ++++ test/protocol/http/request.rb | 6 ++++++ test/protocol/http/response.rb | 8 ++++++++ 8 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/protocol/http/body/readable.rb b/lib/protocol/http/body/readable.rb index 2100b02..6215cc9 100644 --- a/lib/protocol/http/body/readable.rb +++ b/lib/protocol/http/body/readable.rb @@ -93,7 +93,7 @@ def join end end - def as_json + def as_json(...) { class: self.class.name, length: self.length, @@ -102,6 +102,10 @@ def as_json empty: self.empty? } end + + def to_json(...) + as_json.to_json(...) + end end end end diff --git a/lib/protocol/http/body/wrapper.rb b/lib/protocol/http/body/wrapper.rb index c5e248b..4df4c99 100644 --- a/lib/protocol/http/body/wrapper.rb +++ b/lib/protocol/http/body/wrapper.rb @@ -51,13 +51,17 @@ def read @body.read end - def as_json + def as_json(...) { class: self.class.name, body: @body&.as_json } end + def to_json(...) + as_json.to_json(...) + end + def inspect @body.inspect end diff --git a/lib/protocol/http/request.rb b/lib/protocol/http/request.rb index 93a5595..a69d833 100644 --- a/lib/protocol/http/request.rb +++ b/lib/protocol/http/request.rb @@ -73,7 +73,7 @@ def idempotent? @method != Methods::POST && (@body.nil? || @body.empty?) end - def as_json + def as_json(...) { scheme: @scheme, authority: @authority, @@ -86,6 +86,10 @@ def as_json } end + def to_json(...) + as_json.to_json(...) + end + def to_s "#{@scheme}://#{@authority}: #{@method} #{@path} #{@version}" end diff --git a/lib/protocol/http/response.rb b/lib/protocol/http/response.rb index 2d625c7..ac034d6 100644 --- a/lib/protocol/http/response.rb +++ b/lib/protocol/http/response.rb @@ -104,7 +104,7 @@ def self.for_exception(exception) Response[500, Headers['content-type' => 'text/plain'], ["#{exception.class}: #{exception.message}"]] end - def as_json + def as_json(...) { version: @version, status: @status, @@ -114,6 +114,10 @@ def as_json } end + def to_json(...) + as_json.to_json(...) + end + def to_s "#{@status} #{@version}" end diff --git a/test/protocol/http/body/readable.rb b/test/protocol/http/body/readable.rb index e0a7fee..e7b9072 100644 --- a/test/protocol/http/body/readable.rb +++ b/test/protocol/http/body/readable.rb @@ -42,4 +42,20 @@ expect(body.join).to be_nil end end + + with "#as_json" do + it "generates a JSON representation" do + expect(body.as_json).to have_keys( + class: be == subject.name, + length: be_nil, + stream: be == false, + ready: be == false, + empty: be == false, + ) + end + + it "generates a JSON string" do + expect(JSON.dump(body)).to be == body.to_json + end + end end diff --git a/test/protocol/http/body/wrapper.rb b/test/protocol/http/body/wrapper.rb index 527e820..f54a4cb 100644 --- a/test/protocol/http/body/wrapper.rb +++ b/test/protocol/http/body/wrapper.rb @@ -71,5 +71,9 @@ body: be == source.as_json ) end + + it "generates a JSON string" do + expect(JSON.dump(body)).to be == body.to_json + end end end diff --git a/test/protocol/http/request.rb b/test/protocol/http/request.rb index d9981b0..ee4b81a 100644 --- a/test/protocol/http/request.rb +++ b/test/protocol/http/request.rb @@ -5,6 +5,8 @@ require 'protocol/http/request' +require 'json' + describe Protocol::HTTP::Request do let(:headers) {Protocol::HTTP::Headers.new} let(:body) {nil} @@ -38,6 +40,10 @@ protocol: nil } end + + it "generates a JSON string" do + expect(JSON.dump(request)).to be == request.to_json + end end it "should not be HEAD" do diff --git a/test/protocol/http/response.rb b/test/protocol/http/response.rb index c4334b9..92d1731 100644 --- a/test/protocol/http/response.rb +++ b/test/protocol/http/response.rb @@ -113,6 +113,10 @@ protocol: be == nil, ) end + + it "generates a JSON string" do + expect(JSON.dump(response)).to be == response.to_json + end end it_behaves_like InformationalResponse @@ -182,6 +186,10 @@ protocol: be == nil, ) end + + it "generates a JSON string" do + expect(JSON.dump(response)).to be == response.to_json + end end it_behaves_like SuccessfulResponse