Skip to content

Commit

Permalink
Normalize UploadedFile objects in sample data
Browse files Browse the repository at this point in the history
Include some information about the uploaded file to help with debugging
which file was being uploaded during a request.
  • Loading branch information
tombruijn committed Sep 16, 2024
1 parent 40b9ce5 commit f6c60e4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
16 changes: 16 additions & 0 deletions .changesets/normalize-uploadedfile-objects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
bump: patch
type: change
---

Normalize Rack and Rails `UploadedFile` objects. Instead of displaying the Ruby class name, it will now show object details like the filename and content type.

```
# Before
#<Rack::Multipart::UploadedFile>
#<ActionDispatch::Http::UploadedFile>
# After
#<Rack::Multipart::UploadedFile original_filename: "uploaded_file.txt", content_type: "text/plain">
#<ActionDispatch::Http::UploadedFile original_filename: "uploaded_file.txt", content_type: "text/plain">
```
16 changes: 15 additions & 1 deletion lib/appsignal/utils/sample_data_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,21 @@ def sanitize_value(value, filter_keys, seen)
when Date
"#<Date: #{value.iso8601}>"
else
inspected(value)
if defined?(::Rack::Multipart::UploadedFile) &&
value.is_a?(::Rack::Multipart::UploadedFile)
"#<Rack::Multipart::UploadedFile " \
"original_filename: #{value.original_filename.inspect}, " \
"content_type: #{value.content_type.inspect}" \
">"
elsif defined?(::ActionDispatch::Http::UploadedFile) &&
value.is_a?(::ActionDispatch::Http::UploadedFile)
"#<ActionDispatch::Http::UploadedFile " \
"original_filename: #{File.basename(value.original_filename).inspect}, " \
"content_type: #{value.content_type.inspect}" \
">"
else
inspected(value)
end
end
end

Expand Down
44 changes: 33 additions & 11 deletions spec/lib/appsignal/utils/sample_data_sanitizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ def sanitize(value, filter_keys = [])
.to eq(:date => "#<Date: 2024-09-11>")
end

it "normalizes unsupported objects" do
expect(sanitize(:file => uploaded_file)[:file])
.to include("::UploadedFile")

expect(sanitize(:file => [:file => uploaded_file])[:file].first[:file])
.to include("::UploadedFile")

expect(sanitize(:object => Object.new))
.to eq(:object => "#<Object>")
end

it "normalizes Time objects" do
expect(sanitize(:time_in_utc => Time.utc(2024, 9, 12, 13, 14, 15)))
.to eq(:time_in_utc => "#<Time: 2024-09-12T13:14:15Z>")
Expand All @@ -51,6 +40,39 @@ def sanitize(value, filter_keys = [])
.to eq(:time_with_timezone => "#<Time: 2024-09-12T13:14:15+09:00>")
end

it "normalizes Rack UploadedFile objects" do
normalized_file =
"#<Rack::Multipart::UploadedFile " \
"original_filename: \"uploaded_file.txt\", " \
"content_type: \"text/plain\"" \
">"
expect(sanitize(:file => rack_uploaded_file))
.to eq(:file => normalized_file)

expect(sanitize(:file => [:file => rack_uploaded_file])[:file].first[:file])
.to eq(normalized_file)
end

if DependencyHelper.rails_present?
it "normalizes Rails ActionDispatch::Http::UploadedFile objects" do
normalized_file =
"#<ActionDispatch::Http::UploadedFile " \
"original_filename: \"uploaded_file.txt\", " \
"content_type: \"text/plain\"" \
">"
expect(sanitize(:file => rails_uploaded_file))
.to eq(:file => normalized_file)

expect(sanitize(:file => [:file => rails_uploaded_file])[:file].first[:file])
.to eq(normalized_file)
end
end

it "normalizes unsupported objects" do
expect(sanitize(:object => Object.new))
.to eq(:object => "#<Object>")
end

it "accepts nested Hash values" do
expect(sanitize(:abc => 123)).to eq(:abc => 123)
expect(sanitize("abc" => [456])).to eq("abc" => [456])
Expand Down
1 change: 1 addition & 0 deletions spec/support/fixtures/uploaded_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123456
22 changes: 20 additions & 2 deletions spec/support/helpers/transaction_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
module TransactionHelpers
def uploaded_file
if DependencyHelper.rails_present?
ActionDispatch::Http::UploadedFile.new(:tempfile => "/tmp")
rails_uploaded_file
else
::Rack::Multipart::UploadedFile.new(File.join(fixtures_dir, "/uploaded_file.txt"))
rack_uploaded_file
end
end

def rails_uploaded_file
tempfile = Tempfile.new(uploaded_file_path)
tempfile.write(File.read(uploaded_file_path))
ActionDispatch::Http::UploadedFile.new(
:tempfile => tempfile,
:type => "text/plain",
:filename => File.basename(uploaded_file_path)
)
end

def rack_uploaded_file
::Rack::Multipart::UploadedFile.new(uploaded_file_path)
end

def uploaded_file_path
File.join(fixtures_dir, "uploaded_file.txt")
end

def default_namespace
Appsignal::Transaction::HTTP_REQUEST
end
Expand Down

0 comments on commit f6c60e4

Please sign in to comment.