Skip to content

Commit

Permalink
💂‍♂️ Fix encoding error when converting uploaded file to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
timstott committed Dec 5, 2016
1 parent 00a679c commit b120db5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/loga/ext/core/tempfile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Fixes encoding error when converting uploaded file to JSON
# https://github.com/rails/rails/issues/25250
class Tempfile
def as_json(_ = nil)
to_s
end
end
17 changes: 12 additions & 5 deletions lib/loga/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(app)

def call
validate_user_options
change_tempfile_as_json
Loga.configure(user_options, rails_options)
app.config.colorize_logging = false if Loga.configuration.structured?
app.config.logger = Loga.logger
Expand Down Expand Up @@ -51,17 +52,23 @@ def sync
Rails::VERSION::MAJOR > 3 ? app.config.autoflush_log : true
end

# rubocop:disable Metrics/LineLength
def validate_user_options
if user_options[:tags].present?
raise Loga::ConfigurationError, 'Configure tags with Rails config.log_tags'
raise Loga::ConfigurationError,
'Configure tags with Rails config.log_tags'
elsif user_options[:level].present?
raise Loga::ConfigurationError, 'Configure level with Rails config.log_level'
raise Loga::ConfigurationError,
'Configure level with Rails config.log_level'
elsif user_options[:filter_parameters].present?
raise Loga::ConfigurationError, 'Configure filter_parameters with Rails config.filter_parameters'
raise Loga::ConfigurationError,
'Configure filter_parameters with Rails config.filter_parameters'
end
end
# rubocop:enable Metrics/LineLength

# Fixes encoding error when converting uploaded file to JSON
def change_tempfile_as_json
require 'loga/ext/core/tempfile'
end
end

initializer :loga_initialize_logger, before: :initialize_logger do |app|
Expand Down
Binary file added spec/fixtures/random_bin
Binary file not shown.
7 changes: 7 additions & 0 deletions spec/integration/rails/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
let(:app) { Rails.application }
let(:middlewares) { app.middleware.middlewares }

describe 'Tempfile' do
let(:tempfile) { Tempfile.new('README.md') }
it 'monkey patches #as_json' do
expect(tempfile.as_json).to eql(tempfile.to_s)
end
end

context 'development', if: Rails.env.development? do
describe 'loga_initialize_logger' do
let(:formatter) { Loga::Formatters::SimpleFormatter }
Expand Down
10 changes: 4 additions & 6 deletions spec/support/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,19 @@
end
end

describe 'when the request uploads a file' do
it 'works' do
describe 'when the request uploads a binary file', focus: true do
it 'logs the request' do
post '/users?username=yoshi',
{ bob_file: Rack::Test::UploadedFile.new('README.md', 'text/markdown') },
'HTTP_X_REQUEST_ID' => '471a34dc'
bob_file: Rack::Test::UploadedFile.new('spec/fixtures/random_bin')

expect(last_log_entry).to include(
'short_message' => 'POST /users?username=yoshi 200 in 0ms',
'level' => 6,
'_request.params' => {
'username' => 'yoshi',
'bob_file' => include(
'filename' => 'README.md',
'filename' => 'random_bin',
'name' => 'bob_file',
'type' => 'text/markdown',
),
},
)
Expand Down

0 comments on commit b120db5

Please sign in to comment.