When I run this in practice, it works, but I cannot write a working test to limit the route using rspec.
When the test starts, the restriction starts, but the request parameters are empty, so it does not check and the test fails.
I am running Rails 3.0.9, rspec-rails 2.6.1 and rspec 2.6.0.
configurations /routes.rb
match ":param1-unique-:param2" => "controller#index", :constraints => ParamConstraint.new
Library / param _constraint.rb
class ParamConstraint def matches?(request) @request ||= request valid_param1? && valid_param2? end def valid_param1? @request.params[:param1] == "lorem" end def valid_param2? @request.params[:param2] == "ipsum" end end
specifications / routing / param_constraint_spec.rb
require 'spec_helper' describe "param constraint routing" do it "recognizes route for param1 and param2" do { :get => "/lorem-unique-ipsum" }. should route_to( :controller => "controller", :action => "index", :param1 => "lorem", :param2 => "ipsum" ) end end
Update
If I check the request in the constraint, I get the following output:
#<ActionDispatch::Request:0x007fee140ff910 @env={ "rack.version"=>[1, 1], "rack.input"=>#<StringIO:0x007fee1446da48>, "rack.errors"=>#<StringIO:0x007fee1446e768>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "PATH_INFO"=>"/lorem-unique-ipsum", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0" }>