diff --git a/lib/faraday.rb b/lib/faraday.rb index 5cebfa506..f44782d7d 100644 --- a/lib/faraday.rb +++ b/lib/faraday.rb @@ -21,7 +21,7 @@ # module Faraday VERSION = "0.15.3" - METHODS_WITH_QUERY = %w[get head delete] + METHODS_WITH_QUERY = %w[get head delete connect trace] METHODS_WITH_BODY = %w[post put patch] class << self diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index ac602dc5c..fa21cac38 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -14,7 +14,7 @@ module Faraday # class Connection # A Set of allowed HTTP verbs. - METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options] + METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options, :trace, :connect] # @return [Hash] URI query unencoded key/value pairs. attr_reader :params @@ -158,6 +158,36 @@ def headers=(hash) # @yield [Faraday::Request] for further request customizations # @return [Faraday::Response] + # @!method connect(url = nil, params = nil, headers = nil) + # Makes a CONNECT HTTP request without a body. + # @!scope class + # + # @param url [String] The optional String base URL to use as a prefix for all + # requests. Can also be the options Hash. + # @param params [Hash] Hash of URI query unencoded key/value pairs. + # @param headers [Hash] unencoded HTTP header key/value pairs. + # + # @example + # conn.connect '/items/1' + # + # @yield [Faraday::Request] for further request customizations + # @return [Faraday::Response] + + # @!method trace(url = nil, params = nil, headers = nil) + # Makes a TRACE HTTP request without a body. + # @!scope class + # + # @param url [String] The optional String base URL to use as a prefix for all + # requests. Can also be the options Hash. + # @param params [Hash] Hash of URI query unencoded key/value pairs. + # @param headers [Hash] unencoded HTTP header key/value pairs. + # + # @example + # conn.connect '/items/1' + # + # @yield [Faraday::Request] for further request customizations + # @return [Faraday::Response] + # @!visibility private METHODS_WITH_QUERY.each do |method| class_eval <<-RUBY, __FILE__, __LINE__ + 1 diff --git a/lib/faraday/options/env.rb b/lib/faraday/options/env.rb index 8e545330b..4cca6a284 100644 --- a/lib/faraday/options/env.rb +++ b/lib/faraday/options/env.rb @@ -55,7 +55,7 @@ class Env < Options.new(:method, :request_body, :url, :request, :request_headers # A Set of HTTP verbs that typically send a body. If no body is set for # these requests, the Content-Length header is set to 0. - MethodsWithBodies = Set.new [:post, :put, :patch, :options] + MethodsWithBodies = Set.new(Faraday::METHODS_WITH_BODY.map(&:to_sym)) options :request => RequestOptions, :request_headers => Utils::Headers, :response_headers => Utils::Headers @@ -171,4 +171,4 @@ def self.member_set @member_set ||= Set.new(members) end end -end \ No newline at end of file +end diff --git a/spec/faraday/adapter/excon_spec.rb b/spec/faraday/adapter/excon_spec.rb index 1cb2f6baf..b26388025 100644 --- a/spec/faraday/adapter/excon_spec.rb +++ b/spec/faraday/adapter/excon_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe Faraday::Adapter::Excon do - features :body_on_get, :reason_phrase_parse + features :request_body_on_query_methods, :reason_phrase_parse, :trace_method, :connect_method it_behaves_like 'an adapter' @@ -14,4 +14,4 @@ expect(conn.data[:debug_request]).to be_truthy end -end \ No newline at end of file +end diff --git a/spec/faraday/adapter/httpclient_spec.rb b/spec/faraday/adapter/httpclient_spec.rb index 8f05db923..eca27a956 100644 --- a/spec/faraday/adapter/httpclient_spec.rb +++ b/spec/faraday/adapter/httpclient_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe Faraday::Adapter::HTTPClient do - features :body_on_get, :reason_phrase_parse, :compression + features :request_body_on_query_methods, :reason_phrase_parse, :compression, :trace_method, :connect_method it_behaves_like 'an adapter' @@ -32,4 +32,4 @@ expect(conn.options[:bind][:host]).to eq(host) expect(conn.options[:bind][:port]).to eq(port) end -end \ No newline at end of file +end diff --git a/spec/faraday/adapter/net_http_persistent_spec.rb b/spec/faraday/adapter/net_http_persistent_spec.rb index 917717457..541172065 100644 --- a/spec/faraday/adapter/net_http_persistent_spec.rb +++ b/spec/faraday/adapter/net_http_persistent_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe Faraday::Adapter::NetHttpPersistent do - features :body_on_get, :reason_phrase_parse, :compression + features :request_body_on_query_methods, :reason_phrase_parse, :compression, :trace_method, :connect_method it_behaves_like 'an adapter' diff --git a/spec/faraday/adapter/net_http_spec.rb b/spec/faraday/adapter/net_http_spec.rb index 743c654ec..d687fb511 100644 --- a/spec/faraday/adapter/net_http_spec.rb +++ b/spec/faraday/adapter/net_http_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe Faraday::Adapter::NetHttp do - features :body_on_get, :reason_phrase_parse, :compression, :streaming + features :request_body_on_query_methods, :reason_phrase_parse, :compression, :streaming, :trace_method, :connect_method it_behaves_like 'an adapter' @@ -47,4 +47,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/faraday/adapter/typhoeus_spec.rb b/spec/faraday/adapter/typhoeus_spec.rb index 9d25b2c0d..8441e0ed9 100644 --- a/spec/faraday/adapter/typhoeus_spec.rb +++ b/spec/faraday/adapter/typhoeus_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe Faraday::Adapter::Typhoeus do - features :body_on_get, :parallel + features :request_body_on_query_methods, :parallel, :trace_method, :connect_method it_behaves_like 'an adapter' -end \ No newline at end of file +end diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index 063f93e41..3799caecb 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -613,26 +613,6 @@ def encoder.encode(params) end end - context 'with options' do - let(:url) { 'http://example.com' } - - it 'returns connection options with no args' do - expect(conn.options).to be_a(Faraday::Options) - end - - it 'makes request with path' do - stubbed = stub_request(:options, 'http://example.com/a?a=1') - conn.options('/a', a: 1) - expect(stubbed).to have_been_made.once - end - - it 'makes request with nil path' do - stubbed = stub_request(:options, 'http://example.com') - conn.options(nil) - expect(stubbed).to have_been_made.once - end - end - context 'with default params encoder' do let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') } after { expect(stubbed).to have_been_made.once } diff --git a/spec/support/shared_examples/adapter.rb b/spec/support/shared_examples/adapter.rb index 9b3b130bf..8e893de4a 100644 --- a/spec/support/shared_examples/adapter.rb +++ b/spec/support/shared_examples/adapter.rb @@ -52,38 +52,36 @@ expect(request_stub).to have_been_requested unless request_stub.disabled? end - describe '#get' do - let(:http_method) { :get } - - it_behaves_like 'a request method', :get + on_feature :connect_method do + describe '#connect' do + let(:http_method) { :connect } - on_feature :body_on_get do - it 'handles request body' do - body = { bodyrock: 'true' } - request_stub.with(body: body) - conn.get('/') do |req| - req.body = body - end - end + it_behaves_like 'a request method', :connect end end - describe '#post' do - let(:http_method) { :post } + describe '#delete' do + let(:http_method) { :delete } - it_behaves_like 'a request method', :post + it_behaves_like 'a request method', :delete end - describe '#put' do - let(:http_method) { :put } + describe '#get' do + let(:http_method) { :get } - it_behaves_like 'a request method', :put + it_behaves_like 'a request method', :get end - describe '#delete' do - let(:http_method) { :delete } + describe '#head' do + let(:http_method) { :head } - it_behaves_like 'a request method', :delete + it_behaves_like 'a request method', :head + end + + describe '#options' do + let(:http_method) { :options } + + it_behaves_like 'a request method', :options end describe '#patch' do @@ -92,16 +90,23 @@ it_behaves_like 'a request method', :patch end - describe '#head' do - let(:http_method) { :head } + describe '#post' do + let(:http_method) { :post } - it_behaves_like 'a request method', :head + it_behaves_like 'a request method', :post end - # TODO: Enable after adding API for options method - # describe '#options' do - # let(:http_method) { :options } - # - # it_behaves_like 'a request method' - # end -end \ No newline at end of file + describe '#put' do + let(:http_method) { :put } + + it_behaves_like 'a request method', :put + end + + on_feature :trace_method do + describe '#trace' do + let(:http_method) { :trace } + + it_behaves_like 'a request method', :trace + end + end +end diff --git a/spec/support/shared_examples/request_method.rb b/spec/support/shared_examples/request_method.rb index 79b4492c2..1b8be1bd6 100644 --- a/spec/support/shared_examples/request_method.rb +++ b/spec/support/shared_examples/request_method.rb @@ -46,10 +46,29 @@ # end # end + on_feature :request_body_on_query_methods do + it 'sends request body' do + request_stub.with(Hash[:body, "test"]) + res = if query_or_body == :body + conn.public_send(http_method, '/', 'test') + else + conn.public_send(http_method, '/') do |req| + req.body = 'test' + end + end + expect(res.env.request_body).to eq('test') + end + end + it 'sends url encoded parameters' do payload = { name: 'zack' } request_stub.with(Hash[query_or_body, payload]) - conn.public_send(http_method, '/', payload) + res = conn.public_send(http_method, '/', payload) + if query_or_body == :query + expect(res.env.request_body).to be_nil + else + expect(res.env.request_body).to eq('name=zack') + end end it 'sends url encoded nested parameters' do @@ -178,4 +197,4 @@ expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError) end -end \ No newline at end of file +end