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

Deprecate the string version of the route splat. #181

Merged
merged 1 commit into from Jul 21, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/webmachine/dispatcher/route.rb
Expand Up @@ -22,7 +22,10 @@ class Route

# When used in a path specification, will match all remaining
# segments
MATCH_ALL = '*'.freeze
MATCH_ALL = :*

# String version of MATCH_ALL, deprecated. Use the symbol instead.
MATCH_ALL_STR = '*'.freeze

# Creates a new Route that will associate a pattern to a
# {Resource}.
Expand Down Expand Up @@ -65,6 +68,8 @@ def initialize(path_spec, *args)
guards = args
guards << Proc.new if block_given?

warn t('match_all_symbol') if path_spec.include? MATCH_ALL_STR

@path_spec = path_spec
@guards = guards
@resource = resource
Expand Down Expand Up @@ -107,6 +112,8 @@ def bind(tokens, bindings)
case
when spec.empty? && tokens.empty?
return depth
when spec == [MATCH_ALL_STR]
return [depth, tokens]
when spec == [MATCH_ALL]
return [depth, tokens]
when tokens.empty?
Expand Down
1 change: 1 addition & 0 deletions lib/webmachine/locale/en.yml
Expand Up @@ -27,3 +27,4 @@ en:
invalid_media_type: "Invalid media type: %{type}"
not_resource_class: "%{class} is not a subclass of Webmachine::Resource"
process_post_invalid: "process_post returned %{result}"
match_all_symbol: '"*" as a path segment is deprecated and will be removed in a future release. Please use :*'
37 changes: 33 additions & 4 deletions spec/webmachine/dispatcher/route_spec.rb
@@ -1,13 +1,17 @@
require 'spec_helper'

Webmachine::Dispatcher::Route.class_eval do
def warn(*msgs); end # silence warnings for tests
end

describe Webmachine::Dispatcher::Route do
let(:method) { "GET" }
let(:uri) { URI.parse("http://localhost:8080/") }
let(:request){ Webmachine::Request.new(method, uri, Webmachine::Headers.new, "") }
let(:resource){ Class.new(Webmachine::Resource) }

describe '#apply' do
let(:route) {
let(:route) {
Webmachine::Dispatcher::Route.new ['hello', :string], resource, {}
}

Expand Down Expand Up @@ -38,11 +42,20 @@
end
end

it "warns about the deprecated string splat when initializing" do
[["*"],["foo", "*"],["foo", :bar, "*"]].each do |path|
route = described_class.allocate
expect(route).to receive(:warn)
route.send :initialize, path, resource, {}
end
end

context "matching a request" do
context "on the root path" do
subject { "/" }
it { is_expected.to match_route([]) }
it { is_expected.to match_route ['*'] }
it { is_expected.to match_route [:*] }
it { is_expected.not_to match_route %w{foo} }
it { is_expected.not_to match_route [:id] }
end
Expand All @@ -51,10 +64,10 @@
subject { "/foo/bar/baz" }
it { is_expected.to match_route %w{foo bar baz} }
it { is_expected.to match_route ['foo', :id, "baz"] }
it { is_expected.to match_route %w{foo *} }
it { is_expected.to match_route [:id, '*'] }
it { is_expected.to match_route ['foo', :*] }
it { is_expected.to match_route [:id, :*] }
it { is_expected.not_to match_route [] }
it { is_expected.not_to match_route %w{bar *} }
it { is_expected.not_to match_route ['bar', :*] }
end

context "with a guard on the request method" do
Expand Down Expand Up @@ -139,6 +152,14 @@ def call(request)
end

context "with a splat" do
subject { described_class.new([:*], resource) }

it "should assign empty path tokens" do
expect(request.path_tokens).to eq([])
end
end

context "with a deprecated splat string" do
subject { described_class.new(['*'], resource) }

it "should assign empty path tokens" do
Expand Down Expand Up @@ -172,6 +193,14 @@ def call(request)
end

context "with a splat" do
subject { described_class.new(['foo', :*], resource) }

it "should capture the path tokens matched by the splat" do
expect(request.path_tokens).to eq(%w{ bar baz })
end
end

context "with a deprecated splat string" do
subject { described_class.new(%w{foo *}, resource) }

it "should capture the path tokens matched by the splat" do
Expand Down