Skip to content

Commit a0138f8

Browse files
committed
Merge pull request jaswope#19 from waterlink/accept-matcher-as-lambda
Add support for lambda as a matcher
2 parents 59e7fe4 + d3a0623 commit a0138f8

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/rack_reverse_proxy/rule.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def matches(path, *args)
4747
def build_matcher(spec)
4848
return /^#{spec}/ if spec.is_a?(String)
4949
return spec if spec.respond_to?(:match)
50+
return spec if spec.respond_to?(:call)
5051
fail ArgumentError, "Invalid Rule for reverse_proxy"
5152
end
5253

@@ -117,7 +118,7 @@ def initialize(spec, url, path, accept_headers, has_custom_url, headers, rackreq
117118
@rackreq = rackreq
118119

119120
@headers = headers if accept_headers
120-
@spec_arity = spec.method(:match).arity
121+
@spec_arity = spec.method(spec_match_method_name).arity
121122
end
122123

123124
def any?
@@ -147,7 +148,7 @@ def found
147148

148149
def find_matches
149150
Array(
150-
spec.match(*spec_params)
151+
spec.send(spec_match_method_name, *spec_params)
151152
)
152153
end
153154

@@ -171,6 +172,11 @@ def _spec_param_count
171172
return 1 if spec_arity == -1
172173
spec_arity
173174
end
175+
176+
def spec_match_method_name
177+
return :match if spec.respond_to?(:match)
178+
:call
179+
end
174180
end
175181
end
176182
end

spec/rack/reverse_proxy_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,27 @@ def app
435435
end
436436
end
437437

438+
describe "with a matching lambda" do
439+
def app
440+
Rack::ReverseProxy.new(dummy_app) do
441+
reverse_proxy lambda { |path| path.match(%r{^/test}) }, "http://lambda.example.org"
442+
end
443+
end
444+
445+
it "forwards requests to the calling app when path is not matched" do
446+
get "/users"
447+
expect(last_response).to be_ok
448+
expect(last_response.body).to eq("Dummy App")
449+
end
450+
451+
it "proxies requests when a pattern is matched" do
452+
stub_request(:get, "http://lambda.example.org/test").to_return(:body => "Proxied App")
453+
454+
get "/test"
455+
expect(last_response.body).to eq("Proxied App")
456+
end
457+
end
458+
438459
describe "with a matching class" do
439460
#:nodoc:
440461
class Matcher

0 commit comments

Comments
 (0)