107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/better-faraday.rb', line 107
def inspect
@inspection_text ||= begin
= bf_protect_data(env..dup)
request_body = env.bf_request_body.yield_self { |body| String === body ? body : body.to_s }
request_body_bytes_count = env.bf_request_body.bytesize
response_body = env.body.yield_self { |body| String === body ? body : body.to_s }
response_body_bytes_count = response_body.bytesize
if env.["content-type"].to_s.match?(/\bapplication\/json\b/i)
request_json = bf_json_parse(request_body).yield_self do |data|
bf_json_dump(bf_protect_data(data)) if data
end
end
if env.
= bf_protect_data(env..to_hash)
end
if env. && env.["content-type"].to_s.match?(/\bapplication\/json\b/i)
response_json = bf_json_parse(response_body).yield_self do |data|
bf_json_dump(bf_protect_data(data)) if data
end
end
lines = [
"-- #{status} #{reason_phrase} --".upcase,
"",
"-- Request URL --",
env.url.to_s,
"",
"-- Request Method --",
env.method.to_s.upcase,
"",
"-- Request Headers --",
bf_json_dump().truncate(2048, omission: "... (truncated)"),
"",
%[-- Request Body (#{request_body_bytes_count} #{"byte".pluralize(request_body_bytes_count)}) --],
if request_json
request_json
else
request_body.inspect.gsub(/\\"/, "\"")[1..-2]
end.truncate(2048, omission: "... (truncated)"),
"",
"-- Request Sent At --",
env.bf_request_sent_at.strftime("%Y-%m-%d %H:%M:%S.%3N") + " UTC",
"",
"-- Response Headers --",
if
bf_json_dump()
else
env..to_s.inspect.gsub(/\\"/, "\"")[1..-2]
end.truncate(2048, omission: "... (truncated)"),
"",
%[-- Response Body (#{response_body_bytes_count} #{"byte".pluralize(response_body_bytes_count)}) --],
if response_json
response_json
else
response_body.inspect.gsub(/\\"/, "\"")[1..-2]
end.truncate(2048, omission: "... (truncated)"),
""
]
if env.bf_response_received_at
lines.concat [
"-- Response Received At --",
env.bf_response_received_at.strftime("%Y-%m-%d %H:%M:%S.%3N") + " UTC",
"",
"-- Response Received In --",
"#{((env.bf_response_received_at.to_f - env.bf_request_sent_at.to_f) * 1000.0).ceil(3)}ms",
""
]
end
lines.join("\n").freeze
end
@inspection_text.dup
end
|