support PATCH and OPTIONS methods

Added a `patch` helper method, but OPTIONS requests need to be done manually.
This commit is contained in:
Mislav Marohnić 2011-05-09 01:07:32 -07:00
parent 9ae912b2c8
commit 2343a4e98f
4 changed files with 39 additions and 12 deletions

View File

@ -12,11 +12,8 @@ module Faraday
session = ::Patron::Session.new
response = begin
if Connection::METHODS_WITH_BODIES.include? env[:method]
session.send(env[:method], env[:url].to_s, env[:body].to_s, env[:request_headers])
else
session.send(env[:method], env[:url].to_s, env[:request_headers])
end
data = Connection::METHODS_WITH_BODIES.include?(env[:method]) ? env[:body].to_s : nil
session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
rescue Errno::ECONNREFUSED
raise Error::ConnectionFailed, $!
end
@ -26,5 +23,11 @@ module Faraday
@app.call env
end
end
# HAX: helps but doesn't work completely
# https://github.com/toland/patron/issues/34
valid_actions = ::Patron::Request::VALID_ACTIONS
valid_actions << :patch unless valid_actions.include? :patch
valid_actions << :options unless valid_actions.include? :options
end
end

View File

@ -6,8 +6,8 @@ module Faraday
class Connection
include Addressable
METHODS = Set.new [:get, :post, :put, :delete, :head]
METHODS_WITH_BODIES = Set.new [:post, :put]
METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
METHODS_WITH_BODIES = Set.new [:post, :put, :patch, :options]
attr_accessor :host, :port, :scheme, :params, :headers, :parallel_manager
attr_reader :path_prefix, :builder, :options, :ssl
@ -76,6 +76,11 @@ module Faraday
run_request(:put, url, body, headers, &block)
end
def patch(url = nil, body = nil, headers = nil)
block = block_given? ? Proc.new : nil
run_request(:patch, url, body, headers, &block)
end
def head(url = nil, headers = nil)
block = block_given? ? Proc.new : nil
run_request(:head, url, nil, headers, &block)

View File

@ -67,8 +67,8 @@ else
assert_equal "file live_test.rb text/x-ruby", resp.body
end unless :default == adapter # isn't configured for multipart
# http://github.com/toland/patron/issues/#issue/9
if ENV['FORCE'] || adapter != Faraday::Adapter::Patron
# https://github.com/toland/patron/issues/9
if ENV['FORCE'] || %[Faraday::Adapter::Patron] != adapter.to_s
define_method "test_#{adapter}_PUT_send_url_encoded_params" do
resp = create_connection(adapter).put do |req|
req.url 'echo_name'
@ -90,6 +90,21 @@ else
end
end
# https://github.com/toland/patron/issues/34
unless %w[Faraday::Adapter::Patron Faraday::Adapter::EMSynchrony].include? adapter.to_s
define_method "test_#{adapter}_PATCH_send_url_encoded_params" do
resp = create_connection(adapter).patch('echo_name', 'name' => 'zack')
assert_equal %("zack"), resp.body
end
end
unless %[Faraday::Adapter::EMSynchrony] == adapter.to_s
define_method "test_#{adapter}_OPTIONS" do
resp = create_connection(adapter).run_request(:options, '/options', nil, {})
assert_equal "hi", resp.body
end
end
define_method "test_#{adapter}_HEAD_send_url_encoded_params" do
resp = create_connection(adapter).head do |req|
req.url 'hello', 'name' => 'zack'

View File

@ -20,18 +20,22 @@ post '/file' do
end
end
%w[get post].each do |method|
[:get, :post].each do |method|
send(method, '/hello') do
"hello #{params[:name]}"
end
end
%w[post put].each do |method|
send(method, '/echo_name') do
%w[POST PUT PATCH].each do |http_method|
settings.send(:route, http_method, '/echo_name') do
params[:name].inspect
end
end
options '/options' do
'hi'
end
delete '/delete_with_json' do
%/{"deleted":true}/
end