Skip to content

Commit

Permalink
implement Connection#options. (#857)
Browse files Browse the repository at this point in the history
fixes #305
  • Loading branch information
technoweenie authored and iMacTia committed Feb 20, 2019
1 parent b2857e6 commit 5cad588
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
29 changes: 26 additions & 3 deletions lib/faraday/connection.rb
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion spec/faraday/connection_spec.rb
Expand Up @@ -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 }
Expand Down Expand Up @@ -642,4 +662,4 @@ def encoder.encode(params)
end
end
end
end
end

0 comments on commit 5cad588

Please sign in to comment.