Permalink
Browse files

PUT and DELETE working

  • Loading branch information...
1 parent f0bc231 commit 49538259f21550e266b04f2b83ca96697dcae2a0 Blake Mizerany committed Oct 3, 2007
Showing with 50 additions and 2 deletions.
  1. +1 −0 lib/sinatra.rb
  2. +15 −0 lib/sinatra/rack_ext/request.rb
  3. +13 −2 test/sinatra/dispatcher_test.rb
  4. +21 −0 test/sinatra/request_test.rb
View
@@ -34,6 +34,7 @@
require File.dirname(__FILE__) + '/sinatra/loader'
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/core_ext/*.rb')
+Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/rack_ext/*.rb')
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/*.rb')
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb')
@@ -0,0 +1,15 @@
+module Rack
+
+ class Request
+
+ def request_method
+ if @env['REQUEST_METHOD'] == 'POST' && %w(PUT DELETE).include?(params['_method'])
+ params['_method'].upcase
+ else
+ @env['REQUEST_METHOD']
+ end
+ end
+
+ end
+
+end
@@ -76,5 +76,16 @@
get_it '/test/blake'
body.should.equal 'blake'
end
-
-end
+
+ it "should respond to DELETE and PUT" do
+ Sinatra::Event.new(:delete, '/') do
+ request.request_method
+ end
+
+ # Browser only know GET and POST. DELETE and PUT are signaled by passing in a _method paramater
+ post_it '/', :_method => 'DELETE'
+ status.should.equal 200
+ text.should.equal 'DELETE'
+ end
+
+end
@@ -0,0 +1,21 @@
+require File.dirname(__FILE__) + '/../helper'
+
+describe "Rack::Request" do
+ it "should return PUT and DELETE based on _method param" do
+ env = {'REQUEST_METHOD' => 'POST', 'rack.input' => StringIO.new('_method=DELETE')}
+ Rack::Request.new(env).request_method.should.equal 'DELETE'
+
+ env = {'REQUEST_METHOD' => 'POST', 'rack.input' => StringIO.new('_method=PUT')}
+ Rack::Request.new(env).request_method.should.equal 'PUT'
+ end
+
+ it "should not allow faking" do
+ env = {'REQUEST_METHOD' => 'POST', 'rack.input' => StringIO.new('_method=GET')}
+ Rack::Request.new(env).request_method.should.equal 'POST'
+
+ env = {'REQUEST_METHOD' => 'GET', 'rack.input' => StringIO.new('_method=POST')}
+ Rack::Request.new(env).request_method.should.equal 'GET'
+ end
+end
+
+

0 comments on commit 4953825

Please sign in to comment.