Skip to content

Commit

Permalink
Merge pull request jaswope#19 from waterlink/accept-matcher-as-lambda
Browse files Browse the repository at this point in the history
Add support for lambda as a matcher
  • Loading branch information
waterlink committed Sep 30, 2015
2 parents 59e7fe4 + d3a0623 commit a0138f8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rack_reverse_proxy/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def matches(path, *args)
def build_matcher(spec)
return /^#{spec}/ if spec.is_a?(String)
return spec if spec.respond_to?(:match)
return spec if spec.respond_to?(:call)
fail ArgumentError, "Invalid Rule for reverse_proxy"
end

Expand Down Expand Up @@ -117,7 +118,7 @@ def initialize(spec, url, path, accept_headers, has_custom_url, headers, rackreq
@rackreq = rackreq

@headers = headers if accept_headers
@spec_arity = spec.method(:match).arity
@spec_arity = spec.method(spec_match_method_name).arity
end

def any?
Expand Down Expand Up @@ -147,7 +148,7 @@ def found

def find_matches
Array(
spec.match(*spec_params)
spec.send(spec_match_method_name, *spec_params)
)
end

Expand All @@ -171,6 +172,11 @@ def _spec_param_count
return 1 if spec_arity == -1
spec_arity
end

def spec_match_method_name
return :match if spec.respond_to?(:match)
:call
end
end
end
end
21 changes: 21 additions & 0 deletions spec/rack/reverse_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ def app
end
end

describe "with a matching lambda" do
def app
Rack::ReverseProxy.new(dummy_app) do
reverse_proxy lambda { |path| path.match(%r{^/test}) }, "http://lambda.example.org"
end
end

it "forwards requests to the calling app when path is not matched" do
get "/users"
expect(last_response).to be_ok
expect(last_response.body).to eq("Dummy App")
end

it "proxies requests when a pattern is matched" do
stub_request(:get, "http://lambda.example.org/test").to_return(:body => "Proxied App")

get "/test"
expect(last_response.body).to eq("Proxied App")
end
end

describe "with a matching class" do
#:nodoc:
class Matcher
Expand Down

0 comments on commit a0138f8

Please sign in to comment.