diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 6f278efca..e99e2b47a 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -163,14 +163,37 @@ def headers=(hash) METHODS_WITH_QUERY.each do |method| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{method}(url = nil, params = nil, headers = nil) - run_request(:#{method}, url, nil, headers) { |request| + run_request(:#{method}, url, nil, headers) do |request| request.params.update(params) if params - yield(request) if block_given? - } + yield request if block_given? + end end RUBY end + # @!method options(url = nil, params = nil, headers = nil) + # Makes an OPTIONS 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.options '/items/1' + # + # @yield [Faraday::Request] for further request customizations + # @return [Faraday::Response] + def options(*args) + return @options if args.size.zero? + url, params, headers = *args + run_request(:options, url, nil, headers) do |request| + request.params.update(params) if params + yield request if block_given? + end + end + # @!method post(url = nil, body = nil, headers = nil) # Makes a POST HTTP request with a body. # @!scope class diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index 6415fb10c..b2842d16a 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -611,6 +611,26 @@ 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 } @@ -642,4 +662,4 @@ def encoder.encode(params) end end end -end \ No newline at end of file +end