Skip to content

Commit

Permalink
Connection methods (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie authored and iMacTia committed Feb 25, 2019
1 parent de0e960 commit e00e900
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/faraday.rb
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion lib/faraday/connection.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/faraday/options/env.rb
Expand Up @@ -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
Expand Down Expand Up @@ -171,4 +171,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
@@ -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'

Expand All @@ -14,4 +14,4 @@

expect(conn.data[:debug_request]).to be_truthy
end
end
end
4 changes: 2 additions & 2 deletions 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'

Expand Down Expand Up @@ -32,4 +32,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
@@ -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'

Expand Down
4 changes: 2 additions & 2 deletions 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'

Expand Down Expand Up @@ -47,4 +47,4 @@
end
end
end
end
end
4 changes: 2 additions & 2 deletions 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
end
20 changes: 0 additions & 20 deletions spec/faraday/connection_spec.rb
Expand Up @@ -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 }
Expand Down
67 changes: 36 additions & 31 deletions spec/support/shared_examples/adapter.rb
Expand Up @@ -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
Expand All @@ -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
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
Expand Up @@ -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
Expand Down Expand Up @@ -178,4 +197,4 @@

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

0 comments on commit e00e900

Please sign in to comment.