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

Connection methods #861

Merged
merged 13 commits into from
Feb 25, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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
Expand Down
32 changes: 31 additions & 1 deletion lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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
Expand Down Expand Up @@ -159,6 +159,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
Expand Down
4 changes: 2 additions & 2 deletions lib/faraday/options/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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
Expand Down Expand Up @@ -169,4 +169,4 @@ def self.member_set
@member_set ||= Set.new(members)
end
end
end
end
4 changes: 2 additions & 2 deletions spec/faraday/adapter/excon_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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'

Expand All @@ -12,4 +12,4 @@

expect(conn.data[:debug_request]).to be_truthy
end
end
end
4 changes: 2 additions & 2 deletions spec/faraday/adapter/httpclient_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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'

Expand Down Expand Up @@ -30,4 +30,4 @@
expect(conn.options[:bind][:host]).to eq(host)
expect(conn.options[:bind][:port]).to eq(port)
end
end
end
2 changes: 1 addition & 1 deletion spec/faraday/adapter/net_http_persistent_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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'

Expand Down
4 changes: 2 additions & 2 deletions spec/faraday/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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'

Expand Down Expand Up @@ -45,4 +45,4 @@
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/faraday/adapter/typhoeus_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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
end
20 changes: 0 additions & 20 deletions spec/faraday/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,26 +611,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 }
Expand Down
67 changes: 36 additions & 31 deletions spec/support/shared_examples/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,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
Expand All @@ -90,16 +88,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
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
23 changes: 21 additions & 2 deletions spec/support/shared_examples/request_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,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
Expand Down Expand Up @@ -176,4 +195,4 @@

expect { conn.public_send(http_method, '/') }.to raise_error(Faraday::ProxyAuthError)
end
end
end