Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for regex in path spec. #235

Merged
merged 2 commits into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/webmachine/dispatcher/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ def bind(tokens, bindings)
return [depth, tokens]
when tokens.empty?
return false
when Regexp === spec.first
matches = spec.first.match URI.decode(tokens.first)
if matches
bindings[:captures] = (bindings[:captures] || []) + matches.captures
else
return false
end
when Symbol === spec.first
bindings[spec.first] = URI.decode(tokens.first)
when spec.first == tokens.first
Expand Down
16 changes: 15 additions & 1 deletion spec/webmachine/dispatcher/route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ def call(request)
end
end
end

context "on a deep path" do
subject { described_class.new(%w{foo bar baz}, resource) }
let(:uri) { URI.parse("http://localhost:8080/foo/bar/baz") }
Expand All @@ -205,6 +204,21 @@ def call(request)
expect(request.path_info).to eq({:id => "bar"})
end
end
context "with regex" do
subject { described_class.new([/foo/, /(.*)/, 'baz'], resource) }

it "should assign the captures path variables" do
expect(request.path_info).to eq({:captures => ["bar"]})
end
end
context "with multi-capture regex" do
subject { described_class.new([/foo/, /(.*)/, /baz\.(.*)/], resource) }
let(:uri) { URI.parse("http://localhost:8080/foo/bar/baz.json") }

it "should assign the captures path variables" do
expect(request.path_info).to eq({:captures => ["bar", "json"]})
end
end

context "with a splat" do
subject { described_class.new(['foo', :*], resource) }
Expand Down
15 changes: 15 additions & 0 deletions spec/webmachine/dispatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe Webmachine::Dispatcher do
let(:dispatcher) { Webmachine.application.dispatcher }
let(:request) { Webmachine::Request.new("GET", URI.parse("http://localhost:8080/"), Webmachine::Headers["accept" => "*/*"], "") }
let(:request2) { Webmachine::Request.new("GET", URI.parse("http://localhost:8080/hello/bob.html"), Webmachine::Headers["accept" => "*/*"], "") }
let(:response) { Webmachine::Response.new }
let(:resource) do
Class.new(Webmachine::Resource) do
Expand All @@ -14,6 +15,14 @@ def to_html; "hello world!"; end
def to_html; "goodbye, cruel world"; end
end
end
let(:resource3) do
Class.new(Webmachine::Resource) do
def to_html
name, format = request.path_info[:captures]
"Hello #{name} with #{format}"
end
end
end
let(:fsm){ double }

before { dispatcher.reset }
Expand Down Expand Up @@ -44,6 +53,12 @@ def to_html; "goodbye, cruel world"; end
expect(fsm).to receive(:run)
dispatcher.dispatch(request, response)
end
it "should handle regex path segments in route definition" do
dispatcher.add_route ["hello", /(.*)\.(.*)/], resource3
expect(Webmachine::Decision::FSM).to receive(:new).with(instance_of(resource3), request2, response).and_return(fsm)
expect(fsm).to receive(:run)
dispatcher.dispatch(request2, response)
end

it "should apply route to request before creating the resource" do
route = dispatcher.add_route [:*], resource
Expand Down