Skip to content

Commit

Permalink
Improve BadRequest logic. Partially reverts 8f8df53, fixes sinatra#1211
Browse files Browse the repository at this point in the history
  • Loading branch information
mwpastore committed Dec 2, 2016
1 parent 63e81bc commit b4fdb72
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 69 deletions.
8 changes: 0 additions & 8 deletions lib/sinatra/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,6 @@ def server_error?
status.between? 500, 599
end

# whether or not the status is set to 400
def bad_request?
status == 400
end

# whether or not the status is set to 404
def not_found?
status == 404
Expand Down Expand Up @@ -1143,9 +1138,6 @@ def handle_exception!(boom)
elsif not_found?
headers['X-Cascade'] = 'pass' if settings.x_cascade?
body '<h1>Not Found</h1>'
elsif bad_request?
dump_errors! boom if settings.dump_errors?
halt status
end

res = error_block!(boom.class, boom) || error_block!(status, boom)
Expand Down
94 changes: 51 additions & 43 deletions lib/sinatra/show_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def call(env)

private

def bad_request?(e)
Sinatra::BadRequest === e
end

def prefers_plain_text?(env)
!(Request.new(env).preferred_type("text/plain","text/html") == "text/html") &&
[/curl/].index { |item| item =~ env["HTTP_USER_AGENT"] }
Expand Down Expand Up @@ -210,8 +214,10 @@ def frame_class(frame)
<p><a href="#" id="expando"
onclick="toggleBacktrace(); return false">(expand)</a></p>
<p id="nav"><strong>JUMP TO:</strong>
<a href="#get-info">GET</a>
<a href="#post-info">POST</a>
<% unless bad_request?(exception) %>
<a href="#get-info">GET</a>
<a href="#post-info">POST</a>
<% end %>
<a href="#cookie-info">COOKIES</a>
<a href="#env-info">ENV</a>
</p>
Expand Down Expand Up @@ -264,47 +270,49 @@ def frame_class(frame)
</ul>
</div> <!-- /BACKTRACE -->
<div id="get">
<h3 id="get-info">GET</h3>
<% if req.GET and not req.GET.empty? %>
<table class="req">
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
<% req.GET.sort_by { |k, v| k.to_s }.each { |key, val| %>
<tr>
<td><%=h key %></td>
<td class="code"><div><%=h val.inspect %></div></td>
</tr>
<% } %>
</table>
<% else %>
<p class="no-data">No GET data.</p>
<% end %>
<div class="clear"></div>
</div> <!-- /GET -->
<div id="post">
<h3 id="post-info">POST</h3>
<% if req.POST and not req.POST.empty? %>
<table class="req">
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
<% req.POST.sort_by { |k, v| k.to_s }.each { |key, val| %>
<tr>
<td><%=h key %></td>
<td class="code"><div><%=h val.inspect %></div></td>
</tr>
<% } %>
</table>
<% else %>
<p class="no-data">No POST data.</p>
<% end %>
<div class="clear"></div>
</div> <!-- /POST -->
<% unless bad_request?(exception) %>
<div id="get">
<h3 id="get-info">GET</h3>
<% if req.GET and not req.GET.empty? %>
<table class="req">
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
<% req.GET.sort_by { |k, v| k.to_s }.each { |key, val| %>
<tr>
<td><%=h key %></td>
<td class="code"><div><%=h val.inspect %></div></td>
</tr>
<% } %>
</table>
<% else %>
<p class="no-data">No GET data.</p>
<% end %>
<div class="clear"></div>
</div> <!-- /GET -->
<div id="post">
<h3 id="post-info">POST</h3>
<% if req.POST and not req.POST.empty? %>
<table class="req">
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
<% req.POST.sort_by { |k, v| k.to_s }.each { |key, val| %>
<tr>
<td><%=h key %></td>
<td class="code"><div><%=h val.inspect %></div></td>
</tr>
<% } %>
</table>
<% else %>
<p class="no-data">No POST data.</p>
<% end %>
<div class="clear"></div>
</div> <!-- /POST -->
<% end %>
<div id="cookies">
<h3 id="cookie-info">COOKIES</h3>
Expand Down
17 changes: 0 additions & 17 deletions test/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ def status_app(code, &block)
end
end

describe 'bad_request?' do
it 'is true for status == 400' do
status_app(400) { bad_request? }
assert_body 'true'
end

it 'is false for status gt 400' do
status_app(401) { bad_request? }
assert_body 'false'
end

it 'is false for status lt 400' do
status_app(399) { bad_request? }
assert_body 'false'
end
end

describe 'not_found?' do
it 'is true for status == 404' do
status_app(404) { not_found? }
Expand Down
2 changes: 1 addition & 1 deletion test/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class RoutingTest < Minitest::Test

request = Rack::MockRequest.new(@app)
response = request.request('GET', '/foo?bar=&bar[]=', {})
assert response.bad_request?
assert_equal 400, response.status
end

it "404s when no route satisfies the request" do
Expand Down
16 changes: 16 additions & 0 deletions test/settings_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ def foo=(value)
assert body.include?("<code>show_exceptions</code> setting")
end

it 'does not attempt to show unparseable query parameters' do
klass = Sinatra.new(Sinatra::Application)
mock_app(klass) {
enable :show_exceptions

get '/' do
raise Sinatra::BadRequest
end
}

get '/'
assert_equal 400, status
refute body.include?('<div id="get">')
refute body.include?('<div id="post">')
end

it 'does not override app-specified error handling when set to :after_handler' do
ran = false
mock_app do
Expand Down

0 comments on commit b4fdb72

Please sign in to comment.