faraday 0.17.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +52 -8
  3. data/LICENSE.md +1 -1
  4. data/README.md +18 -358
  5. data/Rakefile +1 -7
  6. data/UPGRADING.md +55 -0
  7. data/examples/client_spec.rb +65 -0
  8. data/examples/client_test.rb +79 -0
  9. data/lib/faraday/adapter/em_http.rb +141 -99
  10. data/lib/faraday/adapter/em_http_ssl_patch.rb +24 -18
  11. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
  12. data/lib/faraday/adapter/em_synchrony.rb +104 -60
  13. data/lib/faraday/adapter/excon.rb +98 -56
  14. data/lib/faraday/adapter/httpclient.rb +82 -59
  15. data/lib/faraday/adapter/net_http.rb +119 -62
  16. data/lib/faraday/adapter/net_http_persistent.rb +50 -27
  17. data/lib/faraday/adapter/patron.rb +80 -43
  18. data/lib/faraday/adapter/rack.rb +30 -13
  19. data/lib/faraday/adapter/test.rb +86 -53
  20. data/lib/faraday/adapter/typhoeus.rb +4 -1
  21. data/lib/faraday/adapter.rb +82 -22
  22. data/lib/faraday/adapter_registry.rb +28 -0
  23. data/lib/faraday/autoload.rb +47 -36
  24. data/lib/faraday/connection.rb +312 -182
  25. data/lib/faraday/dependency_loader.rb +37 -0
  26. data/lib/faraday/encoders/flat_params_encoder.rb +98 -0
  27. data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
  28. data/lib/faraday/error.rb +9 -35
  29. data/lib/faraday/file_part.rb +128 -0
  30. data/lib/faraday/logging/formatter.rb +105 -0
  31. data/lib/faraday/middleware.rb +12 -28
  32. data/lib/faraday/middleware_registry.rb +129 -0
  33. data/lib/faraday/options/connection_options.rb +22 -0
  34. data/lib/faraday/options/env.rb +181 -0
  35. data/lib/faraday/options/proxy_options.rb +28 -0
  36. data/lib/faraday/options/request_options.rb +22 -0
  37. data/lib/faraday/options/ssl_options.rb +59 -0
  38. data/lib/faraday/options.rb +32 -183
  39. data/lib/faraday/param_part.rb +53 -0
  40. data/lib/faraday/parameters.rb +4 -197
  41. data/lib/faraday/rack_builder.rb +66 -55
  42. data/lib/faraday/request/authorization.rb +42 -30
  43. data/lib/faraday/request/basic_authentication.rb +14 -7
  44. data/lib/faraday/request/instrumentation.rb +45 -27
  45. data/lib/faraday/request/multipart.rb +79 -48
  46. data/lib/faraday/request/retry.rb +197 -171
  47. data/lib/faraday/request/token_authentication.rb +15 -10
  48. data/lib/faraday/request/url_encoded.rb +41 -23
  49. data/lib/faraday/request.rb +68 -36
  50. data/lib/faraday/response/logger.rb +22 -69
  51. data/lib/faraday/response/raise_error.rb +38 -18
  52. data/lib/faraday/response.rb +20 -13
  53. data/lib/faraday/utils/headers.rb +139 -0
  54. data/lib/faraday/utils/params_hash.rb +61 -0
  55. data/lib/faraday/utils.rb +28 -245
  56. data/lib/faraday.rb +93 -174
  57. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  58. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  59. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  60. data/spec/faraday/adapter/excon_spec.rb +49 -0
  61. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  62. data/spec/faraday/adapter/net_http_persistent_spec.rb +57 -0
  63. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  64. data/spec/faraday/adapter/patron_spec.rb +18 -0
  65. data/spec/faraday/adapter/rack_spec.rb +8 -0
  66. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  67. data/spec/faraday/adapter_registry_spec.rb +28 -0
  68. data/spec/faraday/adapter_spec.rb +55 -0
  69. data/spec/faraday/composite_read_io_spec.rb +80 -0
  70. data/spec/faraday/connection_spec.rb +691 -0
  71. data/spec/faraday/error_spec.rb +0 -57
  72. data/spec/faraday/middleware_spec.rb +26 -0
  73. data/spec/faraday/options/env_spec.rb +70 -0
  74. data/spec/faraday/options/options_spec.rb +297 -0
  75. data/spec/faraday/options/proxy_options_spec.rb +37 -0
  76. data/spec/faraday/options/request_options_spec.rb +19 -0
  77. data/spec/faraday/params_encoders/flat_spec.rb +34 -0
  78. data/spec/faraday/params_encoders/nested_spec.rb +134 -0
  79. data/spec/faraday/rack_builder_spec.rb +196 -0
  80. data/spec/faraday/request/authorization_spec.rb +88 -0
  81. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  82. data/spec/faraday/request/multipart_spec.rb +274 -0
  83. data/spec/faraday/request/retry_spec.rb +242 -0
  84. data/spec/faraday/request/url_encoded_spec.rb +70 -0
  85. data/spec/faraday/request_spec.rb +109 -0
  86. data/spec/faraday/response/logger_spec.rb +220 -0
  87. data/spec/faraday/response/middleware_spec.rb +52 -0
  88. data/spec/faraday/response/raise_error_spec.rb +15 -15
  89. data/spec/faraday/response_spec.rb +75 -0
  90. data/spec/faraday/utils/headers_spec.rb +82 -0
  91. data/spec/faraday/utils_spec.rb +56 -0
  92. data/spec/faraday_spec.rb +37 -0
  93. data/spec/spec_helper.rb +63 -36
  94. data/spec/support/disabling_stub.rb +14 -0
  95. data/spec/support/fake_safe_buffer.rb +15 -0
  96. data/spec/support/helper_methods.rb +133 -0
  97. data/spec/support/shared_examples/adapter.rb +104 -0
  98. data/spec/support/shared_examples/params_encoder.rb +18 -0
  99. data/spec/support/shared_examples/request_method.rb +234 -0
  100. data/spec/support/streaming_response_checker.rb +35 -0
  101. data/spec/support/webmock_rack_app.rb +68 -0
  102. metadata +65 -37
  103. data/lib/faraday/deprecate.rb +0 -107
  104. data/lib/faraday/upload_io.rb +0 -67
  105. data/spec/faraday/deprecate_spec.rb +0 -69
  106. data/test/adapters/default_test.rb +0 -14
  107. data/test/adapters/em_http_test.rb +0 -30
  108. data/test/adapters/em_synchrony_test.rb +0 -32
  109. data/test/adapters/excon_test.rb +0 -30
  110. data/test/adapters/httpclient_test.rb +0 -34
  111. data/test/adapters/integration.rb +0 -263
  112. data/test/adapters/logger_test.rb +0 -136
  113. data/test/adapters/net_http_persistent_test.rb +0 -114
  114. data/test/adapters/net_http_test.rb +0 -79
  115. data/test/adapters/patron_test.rb +0 -40
  116. data/test/adapters/rack_test.rb +0 -38
  117. data/test/adapters/test_middleware_test.rb +0 -157
  118. data/test/adapters/typhoeus_test.rb +0 -38
  119. data/test/authentication_middleware_test.rb +0 -65
  120. data/test/composite_read_io_test.rb +0 -109
  121. data/test/connection_test.rb +0 -738
  122. data/test/env_test.rb +0 -268
  123. data/test/helper.rb +0 -75
  124. data/test/live_server.rb +0 -67
  125. data/test/middleware/instrumentation_test.rb +0 -88
  126. data/test/middleware/retry_test.rb +0 -282
  127. data/test/middleware_stack_test.rb +0 -260
  128. data/test/multibyte.txt +0 -1
  129. data/test/options_test.rb +0 -333
  130. data/test/parameters_test.rb +0 -157
  131. data/test/request_middleware_test.rb +0 -126
  132. data/test/response_middleware_test.rb +0 -72
  133. data/test/strawberry.rb +0 -2
  134. data/test/utils_test.rb +0 -98
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Faraday
4
- # @param new_klass [Class] new Klass to use
5
- #
6
- # @return [Class] A modified version of new_klass that warns on
7
- # usage about deprecation.
8
- # @see Faraday::Deprecate
9
- module DeprecatedClass
10
- def self.proxy_class(origclass, ver = '1.0')
11
- proxy = Class.new(origclass) do
12
- class << self
13
- extend Faraday::Deprecate
14
-
15
- def ===(other)
16
- other.is_a?(superclass) || super
17
- end
18
- end
19
- end
20
- proxy.singleton_class.send(:deprecate, :new, "#{origclass}.new", ver)
21
- proxy.singleton_class.send(:deprecate, :inherited, origclass.name, ver)
22
- proxy
23
- end
24
- end
25
-
26
- # Deprecation using semver instead of date, based on Gem::Deprecate
27
- # Provides a single method +deprecate+ to be used to declare when
28
- # something is going away.
29
- #
30
- # class Legacy
31
- # def self.klass_method
32
- # # ...
33
- # end
34
- #
35
- # def instance_method
36
- # # ...
37
- # end
38
- #
39
- # extend Faraday::Deprecate
40
- # deprecate :instance_method, "X.z", '1.0'
41
- #
42
- # class << self
43
- # extend Faraday::Deprecate
44
- # deprecate :klass_method, :none, '1.0'
45
- # end
46
- # end
47
- module Deprecate
48
- def self.skip # :nodoc:
49
- @skip ||= begin
50
- case ENV['FARADAY_DEPRECATE'].to_s.downcase
51
- when '1', 'warn' then :warn
52
- else :skip
53
- end
54
- end
55
- @skip == :skip
56
- end
57
-
58
- def self.skip=(value) # :nodoc:
59
- @skip = value ? :skip : :warn
60
- end
61
-
62
- # Temporarily turn off warnings. Intended for tests only.
63
- def skip_during
64
- original = Faraday::Deprecate.skip
65
- Faraday::Deprecate.skip, = true
66
- yield
67
- ensure
68
- Faraday::Deprecate.skip = original
69
- end
70
-
71
- # Simple deprecation method that deprecates +name+ by wrapping it up
72
- # in a dummy method. It warns on each call to the dummy method
73
- # telling the user of +repl+ (unless +repl+ is :none) and the
74
- # semver that it is planned to go away.
75
- # @param name [Symbol] the method symbol to deprecate
76
- # @param repl [#to_s, :none] the replacement to use, when `:none` it will
77
- # alert the user that no replacemtent is present.
78
- # @param ver [String] the semver the method will be removed.
79
- def deprecate(name, repl, ver)
80
- class_eval do
81
- gem_ver = Gem::Version.new(ver)
82
- old = "_deprecated_#{name}"
83
- alias_method old, name
84
- define_method name do |*args, &block|
85
- mod = is_a? Module
86
- target = mod ? "#{self}." : "#{self.class}#"
87
- target_message = if name == :inherited
88
- "Inheriting #{self}"
89
- else
90
- "#{target}#{name}"
91
- end
92
-
93
- msg = [
94
- "NOTE: #{target_message} is deprecated",
95
- repl == :none ? ' with no replacement' : "; use #{repl} instead. ",
96
- "It will be removed in or after version #{gem_ver}",
97
- "\n#{target}#{name} called from #{Gem.location_of_caller.join(':')}"
98
- ]
99
- warn "#{msg.join}." unless Faraday::Deprecate.skip
100
- send old, *args, &block
101
- end
102
- end
103
- end
104
-
105
- module_function :deprecate, :skip_during
106
- end
107
- end
@@ -1,67 +0,0 @@
1
- begin
2
- require 'composite_io'
3
- require 'parts'
4
- require 'stringio'
5
- rescue LoadError
6
- $stderr.puts "Install the multipart-post gem."
7
- raise
8
- end
9
-
10
- module Faraday
11
- # Similar but not compatible with ::CompositeReadIO provided by multipart-post.
12
- class CompositeReadIO
13
- def initialize(*parts)
14
- @parts = parts.flatten
15
- @ios = @parts.map { |part| part.to_io }
16
- @index = 0
17
- end
18
-
19
- def length
20
- @parts.inject(0) { |sum, part| sum + part.length }
21
- end
22
-
23
- def rewind
24
- @ios.each { |io| io.rewind }
25
- @index = 0
26
- end
27
-
28
- # Read from IOs in order until `length` bytes have been received.
29
- def read(length = nil, outbuf = nil)
30
- got_result = false
31
- outbuf = outbuf ? outbuf.replace("") : ""
32
-
33
- while io = current_io
34
- if result = io.read(length)
35
- got_result ||= !result.nil?
36
- result.force_encoding("BINARY") if result.respond_to?(:force_encoding)
37
- outbuf << result
38
- length -= result.length if length
39
- break if length == 0
40
- end
41
- advance_io
42
- end
43
- (!got_result && length) ? nil : outbuf
44
- end
45
-
46
- def close
47
- @ios.each { |io| io.close }
48
- end
49
-
50
- def ensure_open_and_readable
51
- # Rubinius compatibility
52
- end
53
-
54
- private
55
-
56
- def current_io
57
- @ios[@index]
58
- end
59
-
60
- def advance_io
61
- @index += 1
62
- end
63
- end
64
-
65
- UploadIO = ::UploadIO
66
- Parts = ::Parts
67
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe Faraday::DeprecatedClass do
4
- class SampleClass < StandardError
5
- attr_accessor :foo
6
-
7
- def initialize(foo = nil)
8
- @foo = foo || :foo
9
- end
10
- end
11
-
12
- SampleDeprecatedClass = Faraday::DeprecatedClass.proxy_class(SampleClass)
13
-
14
- it 'does not raise error for deprecated classes but prints an error message' do
15
- error_message, foobar = with_warn_squelching { SampleDeprecatedClass.new(:foo_bar) }
16
- expect(foobar).to be_a(SampleClass)
17
- expect(foobar.foo).to eq(:foo_bar)
18
- expect(error_message).to match(
19
- Regexp.new(
20
- 'NOTE: SampleDeprecatedClass.new is deprecated; '\
21
- 'use SampleClass.new instead. It will be removed in or after version 1.0'
22
- )
23
- )
24
- end
25
-
26
- it 'does not raise an error for inherited error-namespaced classes but prints an error message' do
27
- error_message, = with_warn_squelching { Class.new(SampleDeprecatedClass) }
28
-
29
- expect(error_message).to match(
30
- Regexp.new(
31
- 'NOTE: Inheriting SampleDeprecatedClass is deprecated; '\
32
- 'use SampleClass instead. It will be removed in or after version 1.0'
33
- )
34
- )
35
- end
36
-
37
- it 'allows backward-compatible class to be subclassed' do
38
- expect {
39
- with_warn_squelching { Class.new(SampleDeprecatedClass) }
40
- }.not_to raise_error
41
- end
42
-
43
- it 'allows rescuing of a current error with a deprecated error' do
44
- expect { raise SampleClass, nil }.to raise_error(SampleDeprecatedClass)
45
- end
46
-
47
- it 'allows rescuing of a current error with a current error' do
48
- expect { raise SampleClass, nil }.to raise_error(SampleClass)
49
- end
50
-
51
- it 'allows rescuing of a deprecated error with a deprecated error' do
52
- expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleDeprecatedClass)
53
- end
54
-
55
- it 'allows rescuing of a deprecated error with a current error' do
56
- expect { raise SampleDeprecatedClass, nil }.to raise_error(SampleClass)
57
- end
58
-
59
-
60
- def with_warn_squelching
61
- stderr_catcher = StringIO.new
62
- original_stderr = $stderr
63
- $stderr = stderr_catcher
64
- result = yield if block_given?
65
- [stderr_catcher.tap(&:rewind).string, result]
66
- ensure
67
- $stderr = original_stderr
68
- end
69
- end
@@ -1,14 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class DefaultTest < Faraday::TestCase
5
-
6
- def adapter() :default end
7
-
8
- Integration.apply(self, :NonParallel) do
9
- # default stack is not configured with Multipart
10
- undef :test_POST_sends_files
11
- end
12
-
13
- end
14
- end
@@ -1,30 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class EMHttpTest < Faraday::TestCase
5
-
6
- def adapter() :em_http end
7
-
8
- Integration.apply(self, :Parallel) do
9
- # https://github.com/eventmachine/eventmachine/pull/289
10
- undef :test_timeout
11
-
12
- def test_binds_local_socket
13
- host = '1.2.3.4'
14
- conn = create_connection :request => { :bind => { :host => host } }
15
- assert_equal host, conn.options[:bind][:host]
16
- end
17
- end unless jruby? and ssl_mode?
18
- # https://github.com/eventmachine/eventmachine/issues/180
19
-
20
- def test_custom_adapter_config
21
- url = URI('https://example.com:1234')
22
-
23
- adapter = Faraday::Adapter::EMHttp.new nil, inactivity_timeout: 20
24
-
25
- req = adapter.create_request(url: url, request: {})
26
-
27
- assert_equal 20, req.connopts.inactivity_timeout
28
- end
29
- end
30
- end
@@ -1,32 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class EMSynchronyTest < Faraday::TestCase
5
-
6
- def adapter() :em_synchrony end
7
-
8
- unless jruby?
9
- Integration.apply(self, :Parallel) do
10
- # https://github.com/eventmachine/eventmachine/pull/289
11
- undef :test_timeout
12
-
13
- def test_binds_local_socket
14
- host = '1.2.3.4'
15
- conn = create_connection :request => { :bind => { :host => host } }
16
- #put conn.get('/who-am-i').body
17
- assert_equal host, conn.options[:bind][:host]
18
- end
19
- end
20
- end
21
-
22
- def test_custom_adapter_config
23
- url = URI('https://example.com:1234')
24
-
25
- adapter = Faraday::Adapter::EMSynchrony.new nil, inactivity_timeout: 20
26
-
27
- req = adapter.create_request(url: url, request: {})
28
-
29
- assert_equal 20, req.connopts.inactivity_timeout
30
- end
31
- end
32
- end
@@ -1,30 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class ExconTest < Faraday::TestCase
5
-
6
- def adapter() :excon end
7
-
8
- Integration.apply(self, :NonParallel) do
9
- # https://github.com/geemus/excon/issues/126 ?
10
- undef :test_timeout if ssl_mode?
11
-
12
- # Excon lets OpenSSL::SSL::SSLError be raised without any way to
13
- # distinguish whether it happened because of a 407 proxy response
14
- undef :test_proxy_auth_fail if ssl_mode?
15
-
16
- # https://github.com/geemus/excon/issues/358
17
- undef :test_connection_error if RUBY_VERSION >= '2.1.0'
18
- end
19
-
20
- def test_custom_adapter_config
21
- url = URI('https://example.com:1234')
22
-
23
- adapter = Faraday::Adapter::Excon.new nil, debug_request: true
24
-
25
- conn = adapter.create_connection({url: url}, {})
26
-
27
- assert_equal true, conn.data[:debug_request]
28
- end
29
- end
30
- end
@@ -1,34 +0,0 @@
1
- require File.expand_path('../integration', __FILE__)
2
-
3
- module Adapters
4
- class HttpclientTest < Faraday::TestCase
5
-
6
- def adapter() :httpclient end
7
-
8
- Integration.apply(self, :NonParallel, :Compression) do
9
- def setup
10
- require 'httpclient' unless defined?(HTTPClient)
11
- HTTPClient::NO_PROXY_HOSTS.delete('localhost')
12
- end
13
-
14
- def test_binds_local_socket
15
- host = '1.2.3.4'
16
- conn = create_connection :request => { :bind => { :host => host } }
17
- assert_equal host, conn.options[:bind][:host]
18
- end
19
-
20
- def test_custom_adapter_config
21
- adapter = Faraday::Adapter::HTTPClient.new do |client|
22
- client.keep_alive_timeout = 20
23
- client.ssl_config.timeout = 25
24
- end
25
-
26
- client = adapter.client
27
- adapter.configure_client
28
-
29
- assert_equal 20, client.keep_alive_timeout
30
- assert_equal 25, client.ssl_config.timeout
31
- end
32
- end
33
- end
34
- end
@@ -1,263 +0,0 @@
1
- require 'forwardable'
2
- require File.expand_path("../../helper", __FILE__)
3
- Faraday.require_lib 'autoload'
4
-
5
- module Adapters
6
- # Adapter integration tests. To use, implement two methods:
7
- #
8
- # `#adapter` required. returns a symbol for the adapter middleware name
9
- # `#adapter_options` optional. extra arguments for building an adapter
10
- module Integration
11
- def self.apply(base, *extra_features)
12
- if base.live_server
13
- features = [:Common]
14
- features.concat extra_features
15
- features << :SSL if base.ssl_mode?
16
- features.each {|name| base.send(:include, self.const_get(name)) }
17
- yield if block_given?
18
- elsif !defined? @warned
19
- warn "Warning: Not running integration tests against a live server."
20
- warn "Start the server `ruby test/live_server.rb` and set the LIVE=1 env variable."
21
- warn "See CONTRIBUTING for usage."
22
- @warned = true
23
- end
24
- end
25
-
26
- module Parallel
27
- def test_in_parallel
28
- resp1, resp2 = nil, nil
29
-
30
- connection = create_connection
31
- connection.in_parallel do
32
- resp1 = connection.get('echo?a=1')
33
- resp2 = connection.get('echo?b=2')
34
- assert connection.in_parallel?
35
- assert_nil resp1.body
36
- assert_nil resp2.body
37
- end
38
- assert !connection.in_parallel?
39
- assert_equal 'get ?{"a"=>"1"}', resp1.body
40
- assert_equal 'get ?{"b"=>"2"}', resp2.body
41
- end
42
- end
43
-
44
- module NonParallel
45
- def test_no_parallel_support
46
- connection = create_connection
47
- response = nil
48
-
49
- err = capture_warnings do
50
- connection.in_parallel do
51
- response = connection.get('echo').body
52
- end
53
- end
54
- assert response
55
- assert_match "no parallel-capable adapter on Faraday stack", err
56
- assert_match __FILE__, err
57
- end
58
- end
59
-
60
- module Compression
61
- def test_GET_handles_compression
62
- res = get('echo_header', :name => 'accept-encoding')
63
- assert_match(/\bgzip\b/, res.body)
64
- assert_match(/\bdeflate\b/, res.body)
65
- end
66
- end
67
-
68
- module SSL
69
- def test_GET_ssl_fails_with_bad_cert
70
- ca_file = 'tmp/faraday-different-ca-cert.crt'
71
- conn = create_connection(:ssl => {:ca_file => ca_file})
72
- err = assert_raises Faraday::SSLError do
73
- conn.get('/ssl')
74
- end
75
- assert_includes err.message, "certificate"
76
- end
77
- end
78
-
79
- module Common
80
- extend Forwardable
81
- def_delegators :create_connection, :get, :head, :put, :post, :patch, :delete, :run_request
82
-
83
- def test_GET_retrieves_the_response_body
84
- assert_equal 'get', get('echo').body
85
- end
86
-
87
- def test_GET_send_url_encoded_params
88
- assert_equal %(get ?{"name"=>"zack"}), get('echo', :name => 'zack').body
89
- end
90
-
91
- def test_GET_retrieves_the_response_headers
92
- response = get('echo')
93
- assert_match(/text\/plain/, response.headers['Content-Type'], 'original case fail')
94
- assert_match(/text\/plain/, response.headers['content-type'], 'lowercase fail')
95
- end
96
-
97
- def test_GET_handles_headers_with_multiple_values
98
- assert_equal 'one, two', get('multi').headers['set-cookie']
99
- end
100
-
101
- def test_GET_with_body
102
- response = get('echo') do |req|
103
- req.body = {'bodyrock' => true}
104
- end
105
- assert_equal %(get {"bodyrock"=>"true"}), response.body
106
- end
107
-
108
- def test_GET_sends_user_agent
109
- response = get('echo_header', {:name => 'user-agent'}, :user_agent => 'Agent Faraday')
110
- assert_equal 'Agent Faraday', response.body
111
- end
112
-
113
- def test_GET_ssl
114
- expected = self.class.ssl_mode?.to_s
115
- assert_equal expected, get('ssl').body
116
- end
117
-
118
- def test_GET_reason_phrase
119
- response = get('echo')
120
- assert_equal "OK", response.reason_phrase
121
- end
122
-
123
- def test_POST_send_url_encoded_params
124
- assert_equal %(post {"name"=>"zack"}), post('echo', :name => 'zack').body
125
- end
126
-
127
- def test_POST_send_url_encoded_nested_params
128
- resp = post('echo', 'name' => {'first' => 'zack'})
129
- assert_equal %(post {"name"=>{"first"=>"zack"}}), resp.body
130
- end
131
-
132
- def test_POST_retrieves_the_response_headers
133
- assert_match(/text\/plain/, post('echo').headers['content-type'])
134
- end
135
-
136
- def test_POST_sends_files
137
- resp = post('file') do |req|
138
- req.body = {'uploaded_file' => Faraday::UploadIO.new(__FILE__, 'text/x-ruby')}
139
- end
140
- assert_equal "file integration.rb text/x-ruby #{File.size(__FILE__)}", resp.body
141
- end
142
-
143
- def test_PUT_send_url_encoded_params
144
- assert_equal %(put {"name"=>"zack"}), put('echo', :name => 'zack').body
145
- end
146
-
147
- def test_PUT_send_url_encoded_nested_params
148
- resp = put('echo', 'name' => {'first' => 'zack'})
149
- assert_equal %(put {"name"=>{"first"=>"zack"}}), resp.body
150
- end
151
-
152
- def test_PUT_retrieves_the_response_headers
153
- assert_match(/text\/plain/, put('echo').headers['content-type'])
154
- end
155
-
156
- def test_PATCH_send_url_encoded_params
157
- assert_equal %(patch {"name"=>"zack"}), patch('echo', :name => 'zack').body
158
- end
159
-
160
- def test_OPTIONS
161
- resp = run_request(:options, 'echo', nil, {})
162
- assert_equal 'options', resp.body
163
- end
164
-
165
- def test_HEAD_retrieves_no_response_body
166
- assert_equal '', head('echo').body
167
- end
168
-
169
- def test_HEAD_retrieves_the_response_headers
170
- assert_match(/text\/plain/, head('echo').headers['content-type'])
171
- end
172
-
173
- def test_DELETE_retrieves_the_response_headers
174
- assert_match(/text\/plain/, delete('echo').headers['content-type'])
175
- end
176
-
177
- def test_DELETE_retrieves_the_body
178
- assert_equal %(delete), delete('echo').body
179
- end
180
-
181
- def test_timeout
182
- conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
183
- assert_raises Faraday::TimeoutError do
184
- conn.get '/slow'
185
- end
186
- end
187
-
188
- def test_connection_error
189
- assert_raises Faraday::ConnectionFailed do
190
- get 'http://localhost:4'
191
- end
192
- end
193
-
194
- def test_proxy
195
- proxy_uri = URI(ENV['LIVE_PROXY'])
196
- conn = create_connection(:proxy => proxy_uri)
197
-
198
- res = conn.get '/echo'
199
- assert_equal 'get', res.body
200
-
201
- unless self.class.ssl_mode?
202
- # proxy can't append "Via" header for HTTPS responses
203
- assert_match(/:#{proxy_uri.port}$/, res['via'])
204
- end
205
- end
206
-
207
- def test_proxy_auth_fail
208
- proxy_uri = URI(ENV['LIVE_PROXY'])
209
- proxy_uri.password = 'WRONG'
210
- conn = create_connection(:proxy => proxy_uri)
211
-
212
- err = assert_raises Faraday::ConnectionFailed do
213
- conn.get '/echo'
214
- end
215
-
216
- unless self.class.ssl_mode? && (self.class.jruby? ||
217
- adapter == :em_http || adapter == :em_synchrony)
218
- # JRuby raises "End of file reached" which cannot be distinguished from a 407
219
- # EM raises "connection closed by server" due to https://github.com/igrigorik/em-socksify/pull/19
220
- assert_equal %{407 "Proxy Authentication Required "}, err.message
221
- end
222
- end
223
-
224
- def test_empty_body_response_represented_as_blank_string
225
- response = get('204')
226
- assert_equal '', response.body
227
- end
228
-
229
- def adapter
230
- raise NotImplementedError.new("Need to override #adapter")
231
- end
232
-
233
- # extra options to pass when building the adapter
234
- def adapter_options
235
- []
236
- end
237
-
238
- def create_connection(options = {}, &optional_connection_config_blk)
239
- if adapter == :default
240
- builder_block = nil
241
- else
242
- builder_block = Proc.new do |b|
243
- b.request :multipart
244
- b.request :url_encoded
245
- b.adapter adapter, *adapter_options, &optional_connection_config_blk
246
- end
247
- end
248
-
249
- server = self.class.live_server
250
- url = '%s://%s:%d' % [server.scheme, server.host, server.port]
251
-
252
- options[:ssl] ||= {}
253
- options[:ssl][:ca_file] ||= ENV['SSL_FILE']
254
-
255
- Faraday::Connection.new(url, options, &builder_block).tap do |conn|
256
- conn.headers['X-Faraday-Adapter'] = adapter.to_s
257
- adapter_handler = conn.builder.handlers.last
258
- conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
259
- end
260
- end
261
- end
262
- end
263
- end