Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement #options. #857

Merged
merged 1 commit into from Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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