public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Routes may be restricted to lists of HTTP methods instead of a single method or 
:any.

[#407 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
brennandunn (author)
Thu Aug 28 04:53:29 -0700 2008
jeremy (committer)
Thu Aug 28 12:29:49 -0700 2008
commit  9cc8c0a0a18163fb6ae0f66b2513c902d19459dc
tree    1731a3f436612031aec1b812fe1e2fb5b4d7da44
parent  7bdd5b768e51f74fa437d1cc8f3c36643364c4fe
...
1
2
 
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
9
0
@@ -1,5 +1,9 @@
0
 *Edge*
0
 
0
+* Routes may be restricted to lists of HTTP methods instead of a single method or :any.  #407 [Brennan Dunn, Gaius Centus Novus]
0
+    map.resource :posts, :collection => { :search => [:get, :post] }
0
+    map.session 'session', :requirements => { :method => [:get, :post, :delete] }
0
+
0
 * Deprecated implicit local assignments when rendering partials [Josh Peek]
0
 
0
 * Introduce current_cycle helper method to return the current value without bumping the cycle.  #417 [Ken Collins]
...
187
188
189
190
191
192
 
 
 
 
193
194
195
 
 
 
196
197
198
...
187
188
189
 
 
 
190
191
192
193
194
 
 
195
196
197
198
199
200
0
@@ -187,12 +187,14 @@ module ActionController
0
       private
0
         def validate_route_conditions(conditions)
0
           if method = conditions[:method]
0
-            if method == :head
0
-              raise ArgumentError, "HTTP method HEAD is invalid in route conditions. Rails processes HEAD requests the same as GETs, returning just the response headers"
0
-            end
0
+            [method].flatten.each do |m|
0
+              if m == :head
0
+                raise ArgumentError, "HTTP method HEAD is invalid in route conditions. Rails processes HEAD requests the same as GETs, returning just the response headers"
0
+              end
0
 
0
-            unless HTTP_METHODS.include?(method.to_sym)
0
-              raise ArgumentError, "Invalid HTTP method specified in route conditions: #{conditions.inspect}"
0
+              unless HTTP_METHODS.include?(m.to_sym)
0
+                raise ArgumentError, "Invalid HTTP method specified in route conditions: #{conditions.inspect}"
0
+              end
0
             end
0
           end
0
         end
...
201
202
203
204
 
205
206
207
...
201
202
203
 
204
205
206
207
0
@@ -201,7 +201,7 @@ module ActionController
0
         # recognition, not generation.
0
         def recognition_conditions
0
           result = ["(match = #{Regexp.new(recognition_pattern).inspect}.match(path))"]
0
-          result << "conditions[:method] === env[:method]" if conditions[:method]
0
+          result << "[conditions[:method]].flatten.include?(env[:method])" if conditions[:method]
0
           result
0
         end
0
 
...
1297
1298
1299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1300
1301
1302
...
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
0
@@ -1297,6 +1297,31 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
0
       end
0
     end
0
 
0
+    def test_recognize_array_of_methods
0
+      begin
0
+        Object.const_set(:BooksController, Class.new(ActionController::Base))
0
+        rs.draw do |r|
0
+          r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
0
+          r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
0
+        end
0
+
0
+        @request = ActionController::TestRequest.new
0
+        @request.env["REQUEST_METHOD"] = 'POST'
0
+        @request.request_uri = "/match"
0
+        assert_nothing_raised { rs.recognize(@request) }
0
+        assert_equal 'get_or_post', @request.path_parameters[:action]
0
+
0
+        # have to recreate or else the RouteSet uses a cached version:
0
+        @request = ActionController::TestRequest.new
0
+        @request.env["REQUEST_METHOD"] = 'PUT'
0
+        @request.request_uri = "/match"
0
+        assert_nothing_raised { rs.recognize(@request) }
0
+        assert_equal 'not_get_or_post', @request.path_parameters[:action]
0
+        ensure
0
+          Object.send(:remove_const, :BooksController) rescue nil
0
+        end
0
+      end
0
+
0
     def test_subpath_recognized
0
       Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
0
 

Comments