Skip to content

Commit

Permalink
fix: use content-type to detect json
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed Jun 14, 2019
1 parent fd46a16 commit 3eaa223
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/rollbar/request_data_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ def rollbar_raw_body_params(rack_req)
def json_request?(rack_req)
json_regex = /\bjson\b/

!!(rack_req.env['CONTENT_TYPE'] =~ json_regex ||
rack_req.env['HTTP_ACCEPT'] =~ json_regex)
!!(rack_req.env['CONTENT_TYPE'] =~ json_regex)
end

def rollbar_route_params(env)
Expand Down
18 changes: 18 additions & 0 deletions spec/rollbar/request_data_extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ class ExtractorDummy
end
end

context 'with form POST body (non-json)' do
let(:body) { 'foo=1&bar=2' }
let(:env) do
Rack::MockRequest.env_for('/?foo=bar',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'HTTP_ACCEPT' => 'application/json',
:input => body,
:method => 'POST')
end

it 'skips extracting the body' do
result = subject.extract_request_data_from_rack(env)
expect(result[:body]).to be_eql('{}')
end
end

context 'with JSON POST body' do
let(:params) { { 'key' => 'value' } }
let(:body) { params.to_json }
Expand All @@ -254,6 +270,8 @@ class ExtractorDummy
result = subject.extract_request_data_from_rack(env)
expect(result[:body]).to be_eql(body)
end


end

context 'with JSON DELETE body' do
Expand Down

0 comments on commit 3eaa223

Please sign in to comment.