lib/contrast/agent/response.rb in contrast-agent-3.10.1 vs lib/contrast/agent/response.rb in contrast-agent-3.10.2
- old
+ new
@@ -49,12 +49,12 @@
v = Contrast::Utils::StringUtils.force_utf8(value)
context_response.response_headers[k] = v
end
context_response.parsed_response_headers = true
- context_response.response_body = Contrast::Utils::StringUtils.force_utf8(body)
- context_response.parsed_response_body = true
+ context_response.response_body_binary = Contrast::Utils::StringUtils.force_utf8(body)
+ context_response.parsed_response_body = false
doc_type = document_type
context_response.document_type = doc_type if doc_type
context_response
@@ -118,19 +118,16 @@
end
end
# The response body can change during the request lifecycle
# We should not extract it out as a variable here, or we'll miss those
- # changes. (headdesk)
+ # changes.
def body
return unless @rack_response
- if @is_array
- extract_body(@rack_response[2])
- elsif Contrast::Utils::DuckUtils.quacks_to?(@rack_response, :body)
- extract_body(@rack_response.body)
- end
+ body_content = @is_array ? @rack_response[2] : @rack_response.body
+ extract_body(body_content)
end
def update_body body_string
return unless @rack_response
@@ -182,19 +179,41 @@
return false
end
true
end
+ # Given some holder of the content of the response's body, extract that
+ # content and return it as a String
+ #
+ # @param body [String, Rack::File, Rack::BodyProxy,
+ # ActionDispatch::Response::RackBody, Rack::Response] Something that
+ # holds, wraps, or is the body of the Response
+ # @return [nil, String] the content of the body
def extract_body body
return nil unless body
if defined?(Rack::File) && body.is_a?(Rack::File)
# not sure what to do in this situation, so don't do anything.
nil
+ elsif body.is_a?(Rack::BodyProxy)
+ next_body = body.instance_variable_get(:@body)
+ case next_body
+ when Array
+ extract_body(next_body[0])
+ else
+ extract_body(next_body)
+ end
+ elsif defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)
+ extract_body(body.body)
+ elsif body.is_a?(Rack::Response)
+ extract_body(body.body)
elsif Contrast::Utils::DuckUtils.quacks_to?(body, :each)
acc = []
body.each { |tmp| acc << read_or_string(tmp) }
acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
+ elsif ActionView::OutputBuffer
+ # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
+ body.to_str
else
read_or_string(body)
end
end