Skip to content

Commit ed26770

Browse files
author
Peter Schrammel
committed
adding global option matching => :first to request the first matched url
1 parent bbf3729 commit ed26770

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/rack/reverse_proxy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ReverseProxy
66
def initialize(app = nil, &b)
77
@app = app || lambda { [404, [], []] }
88
@matchers = []
9-
@global_options = {:preserve_host => false}
9+
@global_options = {:preserve_host => false,:matching => :all}
1010
instance_eval &b if block_given?
1111
end
1212

@@ -63,7 +63,7 @@ def get_matcher path
6363

6464
if matches.length < 1
6565
nil
66-
elsif matches.length > 1
66+
elsif matches.length > 1 && @global_options[:matching] != :first
6767
raise AmbiguousProxyMatch.new(path, matches)
6868
else
6969
matches.first

spec/rack/reverse_proxy_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ def app
7777
end
7878
end
7979

80-
describe "with ambiguous routes" do
80+
describe "with ambiguous routes and all matching" do
8181
def app
8282
Rack::ReverseProxy.new(dummy_app) do
83+
reverse_proxy_options :matching => :all
8384
reverse_proxy '/test', 'http://example.com/'
8485
reverse_proxy /^\/test/, 'http://example.com/'
8586
end
@@ -90,6 +91,22 @@ def app
9091
end
9192
end
9293

94+
describe "with ambiguous routes and first matching" do
95+
def app
96+
Rack::ReverseProxy.new(dummy_app) do
97+
reverse_proxy_options :matching => :first
98+
reverse_proxy '/test', 'http://example1.com/'
99+
reverse_proxy /^\/test/, 'http://example2.com/'
100+
end
101+
end
102+
103+
it "should throw an exception" do
104+
stub_request(:get, 'http://example1.com/test').to_return({:body => "Proxied App"})
105+
get '/test'
106+
last_response.body.should == "Proxied App"
107+
end
108+
end
109+
93110
describe "with a route as a regular expression" do
94111
def app
95112
Rack::ReverseProxy.new(dummy_app) do

0 commit comments

Comments
 (0)