Skip to content

Commit a2b20d0

Browse files
Improve as_json support for Body wrappers.
1 parent f54c6db commit a2b20d0

File tree

9 files changed

+105
-1
lines changed

9 files changed

+105
-1
lines changed

lib/protocol/http/body/completable.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def close(error = nil)
5454
super
5555
end
5656

57+
# Convert the body to a hash suitable for serialization.
58+
#
59+
# @returns [Hash] The body as a hash.
60+
def as_json(...)
61+
super.merge(
62+
callback: @callback&.to_s
63+
)
64+
end
65+
5766
# Inspect the completable body.
5867
#
5968
# @returns [String] a string representation of the completable body.

lib/protocol/http/body/deflate.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def ratio
7575
end
7676
end
7777

78+
# Convert the body to a hash suitable for serialization.
79+
#
80+
# @returns [Hash] The body as a hash.
81+
def as_json(...)
82+
super.merge(
83+
input_length: @input_length,
84+
output_length: @output_length,
85+
compression_ratio: (ratio * 100).round(2)
86+
)
87+
end
88+
7889
# Inspect the body, including the compression ratio.
7990
#
8091
# @returns [String] a string representation of the body.

lib/protocol/http/body/digestable.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ def read
6464
return nil
6565
end
6666
end
67+
68+
# Convert the body to a hash suitable for serialization.
69+
#
70+
# @returns [Hash] The body as a hash.
71+
def as_json(...)
72+
super.merge(
73+
digest_class: @digest.class.name,
74+
callback: @callback&.to_s
75+
)
76+
end
6777
end
6878
end
6979
end

lib/protocol/http/body/rewindable.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def rewindable?
8282
true
8383
end
8484

85+
# Convert the body to a hash suitable for serialization.
86+
#
87+
# @returns [Hash] The body as a hash.
88+
def as_json(...)
89+
super.merge(
90+
index: @index,
91+
chunks: @chunks.size
92+
)
93+
end
94+
8595
# Inspect the rewindable body.
8696
#
8797
# @returns [String] a string representation of the body.

test/protocol/http/body/buffered.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150

151151
with "#inspect" do
152152
let(:body) {subject.new}
153-
153+
154154
it "generates string representation for empty body" do
155155
expect(body.inspect).to be == "#<Protocol::HTTP::Body::Buffered empty>"
156156
end

test/protocol/http/body/completable.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,24 @@
106106
expect(events).to be == [:close2, :close1]
107107
end
108108
end
109+
110+
with "#as_json" do
111+
it "includes callback information" do
112+
completable = subject.new(body, proc{events << :close})
113+
114+
expect(completable.as_json).to have_keys(
115+
class: be == "Protocol::HTTP::Body::Completable",
116+
callback: be =~ /Proc/
117+
)
118+
end
119+
120+
it "shows nil when no callback" do
121+
completable = subject.new(body, nil)
122+
123+
expect(completable.as_json).to have_keys(
124+
class: be == "Protocol::HTTP::Body::Completable",
125+
callback: be == nil
126+
)
127+
end
128+
end
109129
end

test/protocol/http/body/deflate.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@
6161
expect(compressed_body.inspect).to be == "#<Protocol::HTTP::Body::Buffered empty> | #<Protocol::HTTP::Body::Deflate 100.0%>"
6262
end
6363
end
64+
65+
with "#as_json" do
66+
it "includes compression information" do
67+
expect(compressed_body.as_json).to have_keys(
68+
class: be == "Protocol::HTTP::Body::Deflate",
69+
input_length: be == 0,
70+
output_length: be == 0,
71+
compression_ratio: be == 100.0
72+
)
73+
end
74+
end
6475
end

test/protocol/http/body/digestable.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,27 @@ def before
5050
expect(body.etag(weak: true)).to be == 'W/"872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4"'
5151
end
5252
end
53+
54+
with "#as_json" do
55+
it "includes digest information" do
56+
expect(body.as_json).to have_keys(
57+
class: be == "Protocol::HTTP::Body::Digestable",
58+
digest_class: be == "Digest::SHA256",
59+
callback: be == nil
60+
)
61+
end
62+
63+
with "callback" do
64+
let(:callback) {proc {puts "digest complete"}}
65+
let(:body) {subject.new(source, Digest::SHA256.new, callback)}
66+
67+
it "includes callback information" do
68+
expect(body.as_json).to have_keys(
69+
class: be == "Protocol::HTTP::Body::Digestable",
70+
digest_class: be == "Digest::SHA256",
71+
callback: be =~ /Proc/
72+
)
73+
end
74+
end
75+
end
5376
end

test/protocol/http/body/rewindable.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,14 @@
104104
expect(body.inspect).to be == "#<Protocol::HTTP::Body::Buffered empty> | #<Protocol::HTTP::Body::Rewindable 0/0 chunks read>"
105105
end
106106
end
107+
108+
with "#as_json" do
109+
it "includes rewind tracking information" do
110+
expect(body.as_json).to have_keys(
111+
class: be == "Protocol::HTTP::Body::Rewindable",
112+
index: be == 0,
113+
chunks: be == 0
114+
)
115+
end
116+
end
107117
end

0 commit comments

Comments
 (0)