Skip to content

Commit

Permalink
Merge 95125f6 into 5cad588
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Feb 21, 2019
2 parents 5cad588 + 95125f6 commit 2d22c53
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 66 deletions.
2 changes: 1 addition & 1 deletion lib/faraday.rb
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
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
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 [:post, :put, :patch]

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
@@ -1,5 +1,5 @@
RSpec.describe Faraday::Adapter::Excon do
features :body_on_get, :reason_phrase_parse
features :body_on_get, :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
@@ -1,5 +1,5 @@
RSpec.describe Faraday::Adapter::HTTPClient do
features :body_on_get, :reason_phrase_parse, :compression
features :body_on_get, :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
@@ -1,5 +1,5 @@
RSpec.describe Faraday::Adapter::NetHttpPersistent do
features :body_on_get, :reason_phrase_parse, :compression
features :body_on_get, :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,5 +1,5 @@
RSpec.describe Faraday::Adapter::NetHttp do
features :body_on_get, :reason_phrase_parse, :compression, :streaming
features :body_on_get, :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
@@ -1,5 +1,5 @@
RSpec.describe Faraday::Adapter::Typhoeus do
features :body_on_get, :parallel
features :body_on_get, :parallel, :trace_method, :connect_method

it_behaves_like 'an adapter'
end
end
102 changes: 102 additions & 0 deletions spec/faraday/connection_methods_spec.rb
@@ -0,0 +1,102 @@
describe 'making requests with' do
let(:conn) do
Faraday::Connection.new(
url: "http://example.com",
headers: { 'faraday-conn': '1' },
)
end

let(:method) { nil }

shared_examples 'method with query' do
it 'makes request with path' do
stubbed = stub_request(method, 'http://example.com/a?a=1')
res = conn.send(method, '/a', a: 1)
expect(res.env.request_body).to be_nil
expect(stubbed).to have_been_made.once
end

it 'makes request with block' do
stubbed = stub_request(method, 'http://example.com/a?a=1')
res = conn.send(method, '/a') do |req|
req.params[:a] = 1
req.body = 'body'
end
expect(res.env.request_body).to eq('body')
expect(stubbed).to have_been_made.once
end
end

shared_examples 'method with body' do
it 'makes request with path' do
stubbed = stub_request(method, 'http://example.com/a')
res = conn.send(method, '/a', a: 1)
expect(res.env.request_body).to eq('a=1')
expect(stubbed).to have_been_made.once
end

it 'makes request with block' do
stubbed = stub_request(method, 'http://example.com/a?a=1')
res = conn.send(method, '/a', a: 1) do |req|
req.params[:a] = 1
end
expect(res.env.request_body).to eq('a=1')
expect(stubbed).to have_been_made.once
end
end

context '#connect' do
let(:method) { :connect }
it_behaves_like 'method with query'
end

context '#delete' do
let(:method) { :delete }
it_behaves_like 'method with query'
end

context '#get' do
let(:method) { :get }
it_behaves_like 'method with query'
end

context '#head' do
let(:method) { :head }
it_behaves_like 'method with query'
end

context '#options' do
let(:method) { :options }
it_behaves_like 'method with query'

it 'returns connection options with no args' do
expect(conn.options).to be_a(Faraday::Options)
end

it 'makes request with nil path' do
stubbed = stub_request(method, 'http://example.com')
conn.send(method, nil)
expect(stubbed).to have_been_made.once
end
end

context '#patch' do
let(:method) { :patch }
it_behaves_like 'method with body'
end

context '#post' do
let(:method) { :post }
it_behaves_like 'method with body'
end

context '#put' do
let(:method) { :put }
it_behaves_like 'method with body'
end

context '#trace' do
let(:method) { :trace }
it_behaves_like 'method with query'
end
end
20 changes: 0 additions & 20 deletions spec/faraday/connection_spec.rb
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
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
Expand Up @@ -44,10 +44,29 @@
# end
# end

on_feature :body_on_get 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

0 comments on commit 2d22c53

Please sign in to comment.