Skip to content

Commit

Permalink
PUT and DELETE working
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Mizerany committed Oct 3, 2007
1 parent f0bc231 commit 4953825
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/sinatra.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
require File.dirname(__FILE__) + '/sinatra/loader' 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/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 + '/lib/sinatra/*.rb')
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb') Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb')


Expand Down
15 changes: 15 additions & 0 deletions lib/sinatra/rack_ext/request.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
15 changes: 13 additions & 2 deletions test/sinatra/dispatcher_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -76,5 +76,16 @@
get_it '/test/blake' get_it '/test/blake'
body.should.equal 'blake' body.should.equal 'blake'
end 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
21 changes: 21 additions & 0 deletions test/sinatra/request_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -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.