File tree Expand file tree Collapse file tree 9 files changed +105
-1
lines changed Expand file tree Collapse file tree 9 files changed +105
-1
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,15 @@ def close(error = nil)
54
54
super
55
55
end
56
56
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
+
57
66
# Inspect the completable body.
58
67
#
59
68
# @returns [String] a string representation of the completable body.
Original file line number Diff line number Diff line change @@ -75,6 +75,17 @@ def ratio
75
75
end
76
76
end
77
77
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
+
78
89
# Inspect the body, including the compression ratio.
79
90
#
80
91
# @returns [String] a string representation of the body.
Original file line number Diff line number Diff line change @@ -64,6 +64,16 @@ def read
64
64
return nil
65
65
end
66
66
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
67
77
end
68
78
end
69
79
end
Original file line number Diff line number Diff line change @@ -82,6 +82,16 @@ def rewindable?
82
82
true
83
83
end
84
84
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
+
85
95
# Inspect the rewindable body.
86
96
#
87
97
# @returns [String] a string representation of the body.
Original file line number Diff line number Diff line change 150
150
151
151
with "#inspect" do
152
152
let ( :body ) { subject . new }
153
-
153
+
154
154
it "generates string representation for empty body" do
155
155
expect ( body . inspect ) . to be == "#<Protocol::HTTP::Body::Buffered empty>"
156
156
end
Original file line number Diff line number Diff line change 106
106
expect ( events ) . to be == [ :close2 , :close1 ]
107
107
end
108
108
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
109
129
end
Original file line number Diff line number Diff line change 61
61
expect ( compressed_body . inspect ) . to be == "#<Protocol::HTTP::Body::Buffered empty> | #<Protocol::HTTP::Body::Deflate 100.0%>"
62
62
end
63
63
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
64
75
end
Original file line number Diff line number Diff line change @@ -50,4 +50,27 @@ def before
50
50
expect ( body . etag ( weak : true ) ) . to be == 'W/"872e4e50ce9990d8b041330c47c9ddd11bec6b503ae9386a99da8584e9bb12c4"'
51
51
end
52
52
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
53
76
end
Original file line number Diff line number Diff line change 104
104
expect ( body . inspect ) . to be == "#<Protocol::HTTP::Body::Buffered empty> | #<Protocol::HTTP::Body::Rewindable 0/0 chunks read>"
105
105
end
106
106
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
107
117
end
You can’t perform that action at this time.
0 commit comments