diff --git a/lib/protocol/http/body/readable.rb b/lib/protocol/http/body/readable.rb index 6cb4bf2..2100b02 100644 --- a/lib/protocol/http/body/readable.rb +++ b/lib/protocol/http/body/readable.rb @@ -92,6 +92,16 @@ def join return buffer end end + + def as_json + { + class: self.class.name, + length: self.length, + stream: self.stream?, + ready: self.ready?, + empty: self.empty? + } + end end end end diff --git a/lib/protocol/http/body/wrapper.rb b/lib/protocol/http/body/wrapper.rb index e71588f..c5e248b 100644 --- a/lib/protocol/http/body/wrapper.rb +++ b/lib/protocol/http/body/wrapper.rb @@ -51,6 +51,13 @@ def read @body.read end + def as_json + { + class: self.class.name, + body: @body&.as_json + } + end + def inspect @body.inspect end diff --git a/lib/protocol/http/headers.rb b/lib/protocol/http/headers.rb index b409b1b..0db13e9 100644 --- a/lib/protocol/http/headers.rb +++ b/lib/protocol/http/headers.rb @@ -299,6 +299,8 @@ def to_h end end + alias as_json to_h + def inspect "#<#{self.class} #{@fields.inspect}>" end diff --git a/lib/protocol/http/request.rb b/lib/protocol/http/request.rb index 220f0b8..93a5595 100644 --- a/lib/protocol/http/request.rb +++ b/lib/protocol/http/request.rb @@ -73,6 +73,19 @@ def idempotent? @method != Methods::POST && (@body.nil? || @body.empty?) end + def as_json + { + scheme: @scheme, + authority: @authority, + method: @method, + path: @path, + version: @version, + headers: @headers&.as_json, + body: @body&.as_json, + protocol: @protocol + } + 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 01293cb..2d625c7 100644 --- a/lib/protocol/http/response.rb +++ b/lib/protocol/http/response.rb @@ -104,6 +104,16 @@ def self.for_exception(exception) Response[500, Headers['content-type' => 'text/plain'], ["#{exception.class}: #{exception.message}"]] end + def as_json + { + version: @version, + status: @status, + headers: @headers&.as_json, + body: @body&.as_json, + protocol: @protocol + } + end + def to_s "#{@status} #{@version}" end diff --git a/test/protocol/http/body/wrapper.rb b/test/protocol/http/body/wrapper.rb index 53cbb6b..527e820 100644 --- a/test/protocol/http/body/wrapper.rb +++ b/test/protocol/http/body/wrapper.rb @@ -63,4 +63,13 @@ expect(message.body).to be_a(Protocol::HTTP::Body::Wrapper) end end + + with "#as_json" do + it "generates a JSON representation" do + expect(body.as_json).to have_keys( + class: be == "Protocol::HTTP::Body::Wrapper", + body: be == source.as_json + ) + end + end end diff --git a/test/protocol/http/request.rb b/test/protocol/http/request.rb index 20cc1e9..d9981b0 100644 --- a/test/protocol/http/request.rb +++ b/test/protocol/http/request.rb @@ -25,6 +25,21 @@ ) end + with "#as_json" do + it "generates a JSON representation" do + expect(request.as_json).to be == { + scheme: "http", + authority: "localhost", + method: "GET", + path: "/index.html", + version: "HTTP/1.0", + headers: headers.as_json, + body: nil, + protocol: nil + } + end + end + it "should not be HEAD" do expect(request).not.to be(:head?) end diff --git a/test/protocol/http/response.rb b/test/protocol/http/response.rb index 492378c..c4334b9 100644 --- a/test/protocol/http/response.rb +++ b/test/protocol/http/response.rb @@ -13,6 +13,7 @@ InformationalResponse = Sus::Shared("informational response") do it "should be informational" do expect(response).to be(:informational?) + expect(response.as_json).to have_keys(status: be_within(100...200)) end it "should not be a failure" do @@ -23,6 +24,7 @@ SuccessfulResponse = Sus::Shared("successful response") do it "should be successful" do expect(response).to be(:success?) + expect(response.as_json).to have_keys(status: be_within(200...300)) end it "should be final" do @@ -45,6 +47,7 @@ it "should be a redirection" do expect(response).to be(:redirection?) + expect(response.as_json).to have_keys(status: be_within(300...400)) end it "should not be informational" do @@ -71,6 +74,7 @@ it "should be a failure" do expect(response).to be(:failure?) + expect(response.as_json).to have_keys(status: be_within(400...600)) end end @@ -99,6 +103,18 @@ ) end + with "#as_json" do + it "generates a JSON representation" do + expect(response.as_json).to have_keys( + version: be == "HTTP/1.1", + status: be == 100, + headers: be == headers.as_json, + body: be == nil, + protocol: be == nil, + ) + end + end + it_behaves_like InformationalResponse it "should be a continue" do @@ -143,6 +159,7 @@ end with "200 OK" do + let(:body) {Protocol::HTTP::Body::Buffered.wrap("Hello, World!")} let(:response) {subject.new("HTTP/1.0", 200, headers, body)} it "should have attributes" do @@ -155,6 +172,18 @@ ) end + with "#as_json" do + it "generates a JSON representation" do + expect(response.as_json).to have_keys( + version: be == "HTTP/1.0", + status: be == 200, + headers: be == headers.as_json, + body: be == body.as_json, + protocol: be == nil, + ) + end + end + it_behaves_like SuccessfulResponse it "should be ok" do