Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handle RegExp routing conditions that return nil #39

Merged
merged 1 commit into from

2 participants

@pixeltrix

Methods on a request may return nil in certain circumstances,
for example the domain method in Rails will return nil when
using an IP address for testing and development.

This commit fixes this issue by checking that the value responds
to match before checking against the RegExp.

@pixeltrix pixeltrix Handle RegExp routing conditions that return nil
Methods on a request may return nil in certain circumstances,
for example the domain method in Rails will return nil when
using an IP address for testing and development.

This commit fixes this issue by checking that the value responds
to match before checking against the RegExp.
57ebb46
@josh josh merged commit 3e1a6a8 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 12, 2012
  1. @pixeltrix

    Handle RegExp routing conditions that return nil

    pixeltrix authored
    Methods on a request may return nil in certain circumstances,
    for example the domain method in Rails will return nil when
    using an IP address for testing and development.
    
    This commit fixes this issue by checking that the value responds
    to match before checking against the RegExp.
This page is out of date. Refresh to see the latest.
View
3  lib/rack/mount/code_generation.rb
@@ -38,7 +38,8 @@ def optimize_container_iterator(container)
route.conditions.each do |method, condition|
b = []
if condition.is_a?(Regexp)
- b << "if m = obj.#{method}.match(#{condition.inspect})"
+ b << "value = obj.#{method}"
+ b << "if value.respond_to?(:match) && m = value.match(#{condition.inspect})"
b << "matches[:#{method}] = m"
if (named_captures = route.named_captures[method]) && named_captures.any?
b << 'captures = m.captures'
View
2  lib/rack/mount/route_set.rb
@@ -95,7 +95,7 @@ def recognize(obj)
if route.conditions.all? { |method, condition|
value = obj.send(method)
- if condition.is_a?(Regexp) && (m = value.match(condition))
+ if condition.is_a?(Regexp) && value.respond_to?(:match) && (m = value.match(condition))
matches[method] = m
captures = m.captures
route.named_captures[method].each do |k, i|
View
1  test/fixtures/basic_set_map_19.rb
@@ -67,6 +67,7 @@
set.add_route(EchoApp, { :path_info => '/method', :request_method => /get|post/i }, { :controller => 'method', :action => 'index' })
set.add_route(EchoApp, { :path_info => '/host', :host => %r{^(?<account>[0-9a-z]+)\.backpackit\.com$} }, { :controller => 'account' })
set.add_route(EchoApp, { :path_info => '/xhr', :xhr? => true }, { :controller => 'xhr' })
+ set.add_route(EchoApp, { :path_info => '/nil', :referer => %r{^http://github.com} }, { :controller => 'nil', :action => 'index' })
set.add_route(EchoApp, { :url => %r{^(?<scheme>.+)://(?<host>.+)/full/(?<foo>.+)$} }, {}, :full_url)
set.add_route(EchoApp, { :path_info => %r{^/static_group/(foo)(bar)$} })
View
9 test/test_recognition.rb
@@ -136,6 +136,15 @@ def test_xhr_boolean_condition
assert_not_found
end
+ def test_nil_condition
+ get '/nil', 'HTTP_REFERER' => 'http://github.com'
+ assert_success
+ assert_equal({ :controller => 'nil', :action => 'index' }, routing_args)
+
+ get '/nil', 'HTTP_REFERER' => nil
+ assert_not_found
+ end
+
def test_slashes
get '/slashes/trailing/'
assert_success
View
2  test/test_route_set.rb
@@ -78,7 +78,7 @@ def test_average_case
# This will probably change wildly, but still an interesting
# statistic to track
assert_equal 7, @app.instance_variable_get('@recognition_graph').average_height.to_i
- assert_equal 10, @app.instance_variable_get('@generation_graph').average_height.to_i
+ assert_equal 9, @app.instance_variable_get('@generation_graph').average_height.to_i
end
private
Something went wrong with that request. Please try again.