faraday 0.17.3 → 2.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +350 -8
  3. data/LICENSE.md +1 -1
  4. data/README.md +27 -367
  5. data/Rakefile +1 -7
  6. data/examples/client_spec.rb +119 -0
  7. data/examples/client_test.rb +144 -0
  8. data/lib/faraday/adapter/test.rb +170 -72
  9. data/lib/faraday/adapter.rb +69 -23
  10. data/lib/faraday/adapter_registry.rb +30 -0
  11. data/lib/faraday/connection.rb +309 -233
  12. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  13. data/lib/faraday/encoders/nested_params_encoder.rb +183 -0
  14. data/lib/faraday/error.rb +31 -42
  15. data/lib/faraday/logging/formatter.rb +122 -0
  16. data/lib/faraday/methods.rb +6 -0
  17. data/lib/faraday/middleware.rb +21 -25
  18. data/lib/faraday/middleware_registry.rb +83 -0
  19. data/lib/faraday/options/connection_options.rb +22 -0
  20. data/lib/faraday/options/env.rb +205 -0
  21. data/lib/faraday/options/proxy_options.rb +32 -0
  22. data/lib/faraday/options/request_options.rb +22 -0
  23. data/lib/faraday/options/ssl_options.rb +69 -0
  24. data/lib/faraday/options.rb +38 -193
  25. data/lib/faraday/parameters.rb +4 -197
  26. data/lib/faraday/rack_builder.rb +91 -76
  27. data/lib/faraday/request/authorization.rb +42 -29
  28. data/lib/faraday/request/instrumentation.rb +47 -27
  29. data/lib/faraday/request/json.rb +55 -0
  30. data/lib/faraday/request/url_encoded.rb +48 -24
  31. data/lib/faraday/request.rb +64 -42
  32. data/lib/faraday/response/json.rb +54 -0
  33. data/lib/faraday/response/logger.rb +24 -67
  34. data/lib/faraday/response/raise_error.rb +57 -18
  35. data/lib/faraday/response.rb +26 -32
  36. data/lib/faraday/utils/headers.rb +144 -0
  37. data/lib/faraday/utils/params_hash.rb +61 -0
  38. data/lib/faraday/utils.rb +47 -251
  39. data/lib/faraday/version.rb +5 -0
  40. data/lib/faraday.rb +108 -198
  41. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  42. data/spec/faraday/adapter/test_spec.rb +442 -0
  43. data/spec/faraday/adapter_registry_spec.rb +28 -0
  44. data/spec/faraday/adapter_spec.rb +55 -0
  45. data/spec/faraday/connection_spec.rb +808 -0
  46. data/spec/faraday/error_spec.rb +12 -54
  47. data/spec/faraday/middleware_registry_spec.rb +31 -0
  48. data/spec/faraday/middleware_spec.rb +70 -0
  49. data/spec/faraday/options/env_spec.rb +76 -0
  50. data/spec/faraday/options/options_spec.rb +297 -0
  51. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  52. data/spec/faraday/options/request_options_spec.rb +19 -0
  53. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  54. data/spec/faraday/params_encoders/nested_spec.rb +150 -0
  55. data/spec/faraday/rack_builder_spec.rb +317 -0
  56. data/spec/faraday/request/authorization_spec.rb +118 -0
  57. data/spec/faraday/request/instrumentation_spec.rb +74 -0
  58. data/spec/faraday/request/json_spec.rb +111 -0
  59. data/spec/faraday/request/url_encoded_spec.rb +93 -0
  60. data/spec/faraday/request_spec.rb +110 -0
  61. data/spec/faraday/response/json_spec.rb +117 -0
  62. data/spec/faraday/response/logger_spec.rb +248 -0
  63. data/spec/faraday/response/raise_error_spec.rb +81 -15
  64. data/spec/faraday/response_spec.rb +77 -0
  65. data/spec/faraday/utils/headers_spec.rb +100 -0
  66. data/spec/faraday/utils_spec.rb +118 -0
  67. data/spec/faraday_spec.rb +37 -0
  68. data/spec/spec_helper.rb +63 -36
  69. data/spec/support/disabling_stub.rb +14 -0
  70. data/spec/support/fake_safe_buffer.rb +15 -0
  71. data/spec/support/helper_methods.rb +96 -0
  72. data/spec/support/shared_examples/adapter.rb +105 -0
  73. data/spec/support/shared_examples/params_encoder.rb +18 -0
  74. data/spec/support/shared_examples/request_method.rb +263 -0
  75. data/spec/support/streaming_response_checker.rb +35 -0
  76. metadata +76 -59
  77. data/lib/faraday/adapter/em_http.rb +0 -243
  78. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  79. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  80. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  81. data/lib/faraday/adapter/excon.rb +0 -82
  82. data/lib/faraday/adapter/httpclient.rb +0 -128
  83. data/lib/faraday/adapter/net_http.rb +0 -152
  84. data/lib/faraday/adapter/net_http_persistent.rb +0 -68
  85. data/lib/faraday/adapter/patron.rb +0 -95
  86. data/lib/faraday/adapter/rack.rb +0 -58
  87. data/lib/faraday/adapter/typhoeus.rb +0 -12
  88. data/lib/faraday/autoload.rb +0 -84
  89. data/lib/faraday/deprecate.rb +0 -107
  90. data/lib/faraday/request/basic_authentication.rb +0 -13
  91. data/lib/faraday/request/multipart.rb +0 -68
  92. data/lib/faraday/request/retry.rb +0 -213
  93. data/lib/faraday/request/token_authentication.rb +0 -15
  94. data/lib/faraday/upload_io.rb +0 -67
  95. data/spec/faraday/deprecate_spec.rb +0 -69
  96. data/test/adapters/default_test.rb +0 -14
  97. data/test/adapters/em_http_test.rb +0 -30
  98. data/test/adapters/em_synchrony_test.rb +0 -32
  99. data/test/adapters/excon_test.rb +0 -30
  100. data/test/adapters/httpclient_test.rb +0 -34
  101. data/test/adapters/integration.rb +0 -263
  102. data/test/adapters/logger_test.rb +0 -136
  103. data/test/adapters/net_http_persistent_test.rb +0 -114
  104. data/test/adapters/net_http_test.rb +0 -79
  105. data/test/adapters/patron_test.rb +0 -40
  106. data/test/adapters/rack_test.rb +0 -38
  107. data/test/adapters/test_middleware_test.rb +0 -157
  108. data/test/adapters/typhoeus_test.rb +0 -38
  109. data/test/authentication_middleware_test.rb +0 -65
  110. data/test/composite_read_io_test.rb +0 -109
  111. data/test/connection_test.rb +0 -738
  112. data/test/env_test.rb +0 -268
  113. data/test/helper.rb +0 -75
  114. data/test/live_server.rb +0 -67
  115. data/test/middleware/instrumentation_test.rb +0 -88
  116. data/test/middleware/retry_test.rb +0 -282
  117. data/test/middleware_stack_test.rb +0 -260
  118. data/test/multibyte.txt +0 -1
  119. data/test/options_test.rb +0 -333
  120. data/test/parameters_test.rb +0 -157
  121. data/test/request_middleware_test.rb +0 -126
  122. data/test/response_middleware_test.rb +0 -72
  123. data/test/strawberry.rb +0 -2
  124. data/test/utils_test.rb +0 -98
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Request::Json do
4
+ let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
5
+
6
+ def process(body, content_type = nil)
7
+ env = { body: body, request_headers: Faraday::Utils::Headers.new }
8
+ env[:request_headers]['content-type'] = content_type if content_type
9
+ middleware.call(Faraday::Env.from(env)).env
10
+ end
11
+
12
+ def result_body
13
+ result[:body]
14
+ end
15
+
16
+ def result_type
17
+ result[:request_headers]['content-type']
18
+ end
19
+
20
+ context 'no body' do
21
+ let(:result) { process(nil) }
22
+
23
+ it "doesn't change body" do
24
+ expect(result_body).to be_nil
25
+ end
26
+
27
+ it "doesn't add content type" do
28
+ expect(result_type).to be_nil
29
+ end
30
+ end
31
+
32
+ context 'empty body' do
33
+ let(:result) { process('') }
34
+
35
+ it "doesn't change body" do
36
+ expect(result_body).to be_empty
37
+ end
38
+
39
+ it "doesn't add content type" do
40
+ expect(result_type).to be_nil
41
+ end
42
+ end
43
+
44
+ context 'string body' do
45
+ let(:result) { process('{"a":1}') }
46
+
47
+ it "doesn't change body" do
48
+ expect(result_body).to eq('{"a":1}')
49
+ end
50
+
51
+ it 'adds content type' do
52
+ expect(result_type).to eq('application/json')
53
+ end
54
+ end
55
+
56
+ context 'object body' do
57
+ let(:result) { process(a: 1) }
58
+
59
+ it 'encodes body' do
60
+ expect(result_body).to eq('{"a":1}')
61
+ end
62
+
63
+ it 'adds content type' do
64
+ expect(result_type).to eq('application/json')
65
+ end
66
+ end
67
+
68
+ context 'empty object body' do
69
+ let(:result) { process({}) }
70
+
71
+ it 'encodes body' do
72
+ expect(result_body).to eq('{}')
73
+ end
74
+ end
75
+
76
+ context 'object body with json type' do
77
+ let(:result) { process({ a: 1 }, 'application/json; charset=utf-8') }
78
+
79
+ it 'encodes body' do
80
+ expect(result_body).to eq('{"a":1}')
81
+ end
82
+
83
+ it "doesn't change content type" do
84
+ expect(result_type).to eq('application/json; charset=utf-8')
85
+ end
86
+ end
87
+
88
+ context 'object body with vendor json type' do
89
+ let(:result) { process({ a: 1 }, 'application/vnd.myapp.v1+json; charset=utf-8') }
90
+
91
+ it 'encodes body' do
92
+ expect(result_body).to eq('{"a":1}')
93
+ end
94
+
95
+ it "doesn't change content type" do
96
+ expect(result_type).to eq('application/vnd.myapp.v1+json; charset=utf-8')
97
+ end
98
+ end
99
+
100
+ context 'object body with incompatible type' do
101
+ let(:result) { process({ a: 1 }, 'application/xml; charset=utf-8') }
102
+
103
+ it "doesn't change body" do
104
+ expect(result_body).to eq(a: 1)
105
+ end
106
+
107
+ it "doesn't change content type" do
108
+ expect(result_type).to eq('application/xml; charset=utf-8')
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
5
+ RSpec.describe Faraday::Request::UrlEncoded do
6
+ let(:conn) do
7
+ Faraday.new do |b|
8
+ b.request :url_encoded
9
+ b.adapter :test do |stub|
10
+ stub.post('/echo') do |env|
11
+ posted_as = env[:request_headers]['Content-Type']
12
+ body = env[:body]
13
+ if body.respond_to?(:read)
14
+ body = body.read
15
+ end
16
+ [200, { 'Content-Type' => posted_as }, body]
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ it 'does nothing without payload' do
23
+ response = conn.post('/echo')
24
+ expect(response.headers['Content-Type']).to be_nil
25
+ expect(response.body.empty?).to be_truthy
26
+ end
27
+
28
+ it 'ignores custom content type' do
29
+ response = conn.post('/echo', { some: 'data' }, 'content-type' => 'application/x-foo')
30
+ expect(response.headers['Content-Type']).to eq('application/x-foo')
31
+ expect(response.body).to eq(some: 'data')
32
+ end
33
+
34
+ it 'works with no headers' do
35
+ response = conn.post('/echo', fruit: %w[apples oranges])
36
+ expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
37
+ expect(response.body).to eq('fruit%5B%5D=apples&fruit%5B%5D=oranges')
38
+ end
39
+
40
+ it 'works with with headers' do
41
+ response = conn.post('/echo', { 'a' => 123 }, 'content-type' => 'application/x-www-form-urlencoded')
42
+ expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
43
+ expect(response.body).to eq('a=123')
44
+ end
45
+
46
+ it 'works with nested params' do
47
+ response = conn.post('/echo', user: { name: 'Mislav', web: 'mislav.net' })
48
+ expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
49
+ expected = { 'user' => { 'name' => 'Mislav', 'web' => 'mislav.net' } }
50
+ expect(Faraday::Utils.parse_nested_query(response.body)).to eq(expected)
51
+ end
52
+
53
+ it 'works with non nested params' do
54
+ response = conn.post('/echo', dimensions: %w[date location]) do |req|
55
+ req.options.params_encoder = Faraday::FlatParamsEncoder
56
+ end
57
+ expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
58
+ expected = { 'dimensions' => %w[date location] }
59
+ expect(Faraday::Utils.parse_query(response.body)).to eq(expected)
60
+ expect(response.body).to eq('dimensions=date&dimensions=location')
61
+ end
62
+
63
+ it 'works with unicode' do
64
+ err = capture_warnings do
65
+ response = conn.post('/echo', str: 'eé cç aã aâ')
66
+ expect(response.body).to eq('str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2')
67
+ end
68
+ expect(err.empty?).to be_truthy
69
+ end
70
+
71
+ it 'works with nested keys' do
72
+ response = conn.post('/echo', 'a' => { 'b' => { 'c' => ['d'] } })
73
+ expect(response.body).to eq('a%5Bb%5D%5Bc%5D%5B%5D=d')
74
+ end
75
+
76
+ it 'works with files' do
77
+ response = conn.post('/echo', StringIO.new('str=apple'))
78
+ expect(response.body).to eq('str=apple')
79
+ end
80
+
81
+ context 'customising default_space_encoding' do
82
+ around do |example|
83
+ Faraday::Utils.default_space_encoding = '%20'
84
+ example.run
85
+ Faraday::Utils.default_space_encoding = nil
86
+ end
87
+
88
+ it 'uses the custom character to encode spaces' do
89
+ response = conn.post('/echo', str: 'apple banana')
90
+ expect(response.body).to eq('str=apple%20banana')
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Request do
4
+ let(:conn) do
5
+ Faraday.new(url: 'http://httpbingo.org/api',
6
+ headers: { 'Mime-Version' => '1.0' },
7
+ request: { oauth: { consumer_key: 'anonymous' } })
8
+ end
9
+ let(:http_method) { :get }
10
+ let(:block) { nil }
11
+
12
+ subject { conn.build_request(http_method, &block) }
13
+
14
+ context 'when nothing particular is configured' do
15
+ it { expect(subject.http_method).to eq(:get) }
16
+ it { expect(subject.to_env(conn).ssl.verify).to be_falsey }
17
+ it { expect(subject.to_env(conn).ssl.verify_hostname).to be_falsey }
18
+ end
19
+
20
+ context 'when HTTP method is post' do
21
+ let(:http_method) { :post }
22
+
23
+ it { expect(subject.http_method).to eq(:post) }
24
+ end
25
+
26
+ context 'when setting the url on setup with a URI' do
27
+ let(:block) { proc { |req| req.url URI.parse('foo.json?a=1') } }
28
+
29
+ it { expect(subject.path).to eq(URI.parse('foo.json')) }
30
+ it { expect(subject.params).to eq('a' => '1') }
31
+ it { expect(subject.to_env(conn).url.to_s).to eq('http://httpbingo.org/api/foo.json?a=1') }
32
+ end
33
+
34
+ context 'when setting the url on setup with a string path and params' do
35
+ let(:block) { proc { |req| req.url 'foo.json', 'a' => 1 } }
36
+
37
+ it { expect(subject.path).to eq('foo.json') }
38
+ it { expect(subject.params).to eq('a' => 1) }
39
+ it { expect(subject.to_env(conn).url.to_s).to eq('http://httpbingo.org/api/foo.json?a=1') }
40
+ end
41
+
42
+ context 'when setting the url on setup with a path including params' do
43
+ let(:block) { proc { |req| req.url 'foo.json?b=2&a=1#qqq' } }
44
+
45
+ it { expect(subject.path).to eq('foo.json') }
46
+ it { expect(subject.params).to eq('a' => '1', 'b' => '2') }
47
+ it { expect(subject.to_env(conn).url.to_s).to eq('http://httpbingo.org/api/foo.json?a=1&b=2') }
48
+ end
49
+
50
+ context 'when setting a header on setup with []= syntax' do
51
+ let(:block) { proc { |req| req['Server'] = 'Faraday' } }
52
+ let(:headers) { subject.to_env(conn).request_headers }
53
+
54
+ it { expect(subject.headers['Server']).to eq('Faraday') }
55
+ it { expect(headers['mime-version']).to eq('1.0') }
56
+ it { expect(headers['server']).to eq('Faraday') }
57
+ end
58
+
59
+ context 'when setting the body on setup' do
60
+ let(:block) { proc { |req| req.body = 'hi' } }
61
+
62
+ it { expect(subject.body).to eq('hi') }
63
+ it { expect(subject.to_env(conn).body).to eq('hi') }
64
+ end
65
+
66
+ context 'with global request options set' do
67
+ let(:env_request) { subject.to_env(conn).request }
68
+
69
+ before do
70
+ conn.options.timeout = 3
71
+ conn.options.open_timeout = 5
72
+ conn.ssl.verify = false
73
+ conn.proxy = 'http://proxy.com'
74
+ end
75
+
76
+ it { expect(subject.options.timeout).to eq(3) }
77
+ it { expect(subject.options.open_timeout).to eq(5) }
78
+ it { expect(env_request.timeout).to eq(3) }
79
+ it { expect(env_request.open_timeout).to eq(5) }
80
+
81
+ context 'and per-request options set' do
82
+ let(:block) do
83
+ proc do |req|
84
+ req.options.timeout = 10
85
+ req.options.boundary = 'boo'
86
+ req.options.oauth[:consumer_secret] = 'xyz'
87
+ req.options.context = {
88
+ foo: 'foo',
89
+ bar: 'bar'
90
+ }
91
+ end
92
+ end
93
+
94
+ it { expect(subject.options.timeout).to eq(10) }
95
+ it { expect(subject.options.open_timeout).to eq(5) }
96
+ it { expect(env_request.timeout).to eq(10) }
97
+ it { expect(env_request.open_timeout).to eq(5) }
98
+ it { expect(env_request.boundary).to eq('boo') }
99
+ it { expect(env_request.context).to eq(foo: 'foo', bar: 'bar') }
100
+ it do
101
+ oauth_expected = { consumer_secret: 'xyz', consumer_key: 'anonymous' }
102
+ expect(env_request.oauth).to eq(oauth_expected)
103
+ end
104
+ end
105
+ end
106
+
107
+ it 'supports marshal serialization' do
108
+ expect(Marshal.load(Marshal.dump(subject))).to eq(subject)
109
+ end
110
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Faraday::Response::Json, type: :response do
4
+ let(:options) { {} }
5
+ let(:headers) { {} }
6
+ let(:middleware) do
7
+ described_class.new(lambda { |env|
8
+ Faraday::Response.new(env)
9
+ }, **options)
10
+ end
11
+
12
+ def process(body, content_type = 'application/json', options = {})
13
+ env = {
14
+ body: body, request: options,
15
+ request_headers: Faraday::Utils::Headers.new,
16
+ response_headers: Faraday::Utils::Headers.new(headers)
17
+ }
18
+ env[:response_headers]['content-type'] = content_type if content_type
19
+ yield(env) if block_given?
20
+ middleware.call(Faraday::Env.from(env))
21
+ end
22
+
23
+ context 'no type matching' do
24
+ it "doesn't change nil body" do
25
+ expect(process(nil).body).to be_nil
26
+ end
27
+
28
+ it 'nullifies empty body' do
29
+ expect(process('').body).to be_nil
30
+ end
31
+
32
+ it 'parses json body' do
33
+ response = process('{"a":1}')
34
+ expect(response.body).to eq('a' => 1)
35
+ expect(response.env[:raw_body]).to be_nil
36
+ end
37
+ end
38
+
39
+ context 'with preserving raw' do
40
+ let(:options) { { preserve_raw: true } }
41
+
42
+ it 'parses json body' do
43
+ response = process('{"a":1}')
44
+ expect(response.body).to eq('a' => 1)
45
+ expect(response.env[:raw_body]).to eq('{"a":1}')
46
+ end
47
+ end
48
+
49
+ context 'with default regexp type matching' do
50
+ it 'parses json body of correct type' do
51
+ response = process('{"a":1}', 'application/x-json')
52
+ expect(response.body).to eq('a' => 1)
53
+ end
54
+
55
+ it 'ignores json body of incorrect type' do
56
+ response = process('{"a":1}', 'text/json-xml')
57
+ expect(response.body).to eq('{"a":1}')
58
+ end
59
+ end
60
+
61
+ context 'with array type matching' do
62
+ let(:options) { { content_type: %w[a/b c/d] } }
63
+
64
+ it 'parses json body of correct type' do
65
+ expect(process('{"a":1}', 'a/b').body).to be_a(Hash)
66
+ expect(process('{"a":1}', 'c/d').body).to be_a(Hash)
67
+ end
68
+
69
+ it 'ignores json body of incorrect type' do
70
+ expect(process('{"a":1}', 'a/d').body).not_to be_a(Hash)
71
+ end
72
+ end
73
+
74
+ it 'chokes on invalid json' do
75
+ expect { process('{!') }.to raise_error(Faraday::ParsingError)
76
+ end
77
+
78
+ it 'includes the response on the ParsingError instance' do
79
+ process('{') { |env| env[:response] = Faraday::Response.new }
80
+ raise 'Parsing should have failed.'
81
+ rescue Faraday::ParsingError => e
82
+ expect(e.response).to be_a(Faraday::Response)
83
+ end
84
+
85
+ context 'HEAD responses' do
86
+ it "nullifies the body if it's only one space" do
87
+ response = process(' ')
88
+ expect(response.body).to be_nil
89
+ end
90
+
91
+ it "nullifies the body if it's two spaces" do
92
+ response = process(' ')
93
+ expect(response.body).to be_nil
94
+ end
95
+ end
96
+
97
+ context 'JSON options' do
98
+ let(:body) { '{"a": 1}' }
99
+ let(:result) { { a: 1 } }
100
+ let(:options) do
101
+ {
102
+ parser_options: {
103
+ symbolize_names: true
104
+ }
105
+ }
106
+ end
107
+
108
+ it 'passes relevant options to JSON parse' do
109
+ expect(::JSON).to receive(:parse)
110
+ .with(body, options[:parser_options])
111
+ .and_return(result)
112
+
113
+ response = process(body)
114
+ expect(response.body).to eq(result)
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,248 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+ require 'logger'
5
+
6
+ RSpec.describe Faraday::Response::Logger do
7
+ let(:string_io) { StringIO.new }
8
+ let(:logger) { Logger.new(string_io) }
9
+ let(:logger_options) { {} }
10
+ let(:conn) do
11
+ rubbles = ['Barney', 'Betty', 'Bam Bam']
12
+
13
+ Faraday.new do |b|
14
+ b.response :logger, logger, logger_options do |logger|
15
+ logger.filter(/(soylent green is) (.+)/, '\1 tasty')
16
+ logger.filter(/(api_key:).*"(.+)."/, '\1[API_KEY]')
17
+ logger.filter(/(password)=(.+)/, '\1=[HIDDEN]')
18
+ end
19
+ b.adapter :test do |stubs|
20
+ stubs.get('/hello') { [200, { 'Content-Type' => 'text/html' }, 'hello'] }
21
+ stubs.post('/ohai') { [200, { 'Content-Type' => 'text/html' }, 'fred'] }
22
+ stubs.post('/ohyes') { [200, { 'Content-Type' => 'text/html' }, 'pebbles'] }
23
+ stubs.get('/rubbles') { [200, { 'Content-Type' => 'application/json' }, rubbles] }
24
+ stubs.get('/filtered_body') { [200, { 'Content-Type' => 'text/html' }, 'soylent green is people'] }
25
+ stubs.get('/filtered_headers') { [200, { 'Content-Type' => 'text/html' }, 'headers response'] }
26
+ stubs.get('/filtered_params') { [200, { 'Content-Type' => 'text/html' }, 'params response'] }
27
+ stubs.get('/filtered_url') { [200, { 'Content-Type' => 'text/html' }, 'url response'] }
28
+ end
29
+ end
30
+ end
31
+
32
+ before do
33
+ logger.level = Logger::DEBUG
34
+ end
35
+
36
+ it 'still returns output' do
37
+ resp = conn.get '/hello', nil, accept: 'text/html'
38
+ expect(resp.body).to eq('hello')
39
+ end
40
+
41
+ context 'without configuration' do
42
+ let(:conn) do
43
+ Faraday.new do |b|
44
+ b.response :logger
45
+ b.adapter :test do |stubs|
46
+ stubs.get('/hello') { [200, { 'Content-Type' => 'text/html' }, 'hello'] }
47
+ end
48
+ end
49
+ end
50
+
51
+ it 'defaults to stdout' do
52
+ expect(Logger).to receive(:new).with($stdout).and_return(Logger.new(nil))
53
+ conn.get('/hello')
54
+ end
55
+ end
56
+
57
+ context 'with default formatter' do
58
+ let(:formatter) { instance_double(Faraday::Logging::Formatter, request: true, response: true, filter: []) }
59
+
60
+ before { allow(Faraday::Logging::Formatter).to receive(:new).and_return(formatter) }
61
+
62
+ it 'delegates logging to the formatter' do
63
+ expect(formatter).to receive(:request).with(an_instance_of(Faraday::Env))
64
+ expect(formatter).to receive(:response).with(an_instance_of(Faraday::Env))
65
+ conn.get '/hello'
66
+ end
67
+
68
+ context 'when no route' do
69
+ it 'delegates logging to the formatter' do
70
+ expect(formatter).to receive(:request).with(an_instance_of(Faraday::Env))
71
+ expect(formatter).to receive(:exception).with(an_instance_of(Faraday::Adapter::Test::Stubs::NotFound))
72
+
73
+ expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound)
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'with custom formatter' do
79
+ let(:formatter_class) do
80
+ Class.new(Faraday::Logging::Formatter) do
81
+ def request(_env)
82
+ info 'Custom log formatter request'
83
+ end
84
+
85
+ def response(_env)
86
+ info 'Custom log formatter response'
87
+ end
88
+ end
89
+ end
90
+
91
+ let(:logger_options) { { formatter: formatter_class } }
92
+
93
+ it 'logs with custom formatter' do
94
+ conn.get '/hello'
95
+
96
+ expect(string_io.string).to match('Custom log formatter request')
97
+ expect(string_io.string).to match('Custom log formatter response')
98
+ end
99
+ end
100
+
101
+ it 'logs method and url' do
102
+ conn.get '/hello', nil, accept: 'text/html'
103
+ expect(string_io.string).to match('GET http:/hello')
104
+ end
105
+
106
+ it 'logs status' do
107
+ conn.get '/hello', nil, accept: 'text/html'
108
+ expect(string_io.string).to match('Status 200')
109
+ end
110
+
111
+ it 'does not log error message by default' do
112
+ expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound)
113
+ expect(string_io.string).not_to match(%(no stubbed request for get http:/noroute))
114
+ end
115
+
116
+ it 'logs request headers by default' do
117
+ conn.get '/hello', nil, accept: 'text/html'
118
+ expect(string_io.string).to match(%(Accept: "text/html))
119
+ end
120
+
121
+ it 'logs response headers by default' do
122
+ conn.get '/hello', nil, accept: 'text/html'
123
+ expect(string_io.string).to match(%(Content-Type: "text/html))
124
+ end
125
+
126
+ it 'does not log request body by default' do
127
+ conn.post '/ohai', 'name=Unagi', accept: 'text/html'
128
+ expect(string_io.string).not_to match(%(name=Unagi))
129
+ end
130
+
131
+ it 'does not log response body by default' do
132
+ conn.post '/ohai', 'name=Toro', accept: 'text/html'
133
+ expect(string_io.string).not_to match(%(fred))
134
+ end
135
+
136
+ it 'logs filter headers' do
137
+ conn.headers = { 'api_key' => 'ABC123' }
138
+ conn.get '/filtered_headers', nil, accept: 'text/html'
139
+ expect(string_io.string).to match(%(api_key:))
140
+ expect(string_io.string).to match(%([API_KEY]))
141
+ expect(string_io.string).not_to match(%(ABC123))
142
+ end
143
+
144
+ it 'logs filter url' do
145
+ conn.get '/filtered_url?password=hunter2', nil, accept: 'text/html'
146
+ expect(string_io.string).to match(%([HIDDEN]))
147
+ expect(string_io.string).not_to match(%(hunter2))
148
+ end
149
+
150
+ context 'when not logging request headers' do
151
+ let(:logger_options) { { headers: { request: false } } }
152
+
153
+ it 'does not log request headers if option is false' do
154
+ conn.get '/hello', nil, accept: 'text/html'
155
+ expect(string_io.string).not_to match(%(Accept: "text/html))
156
+ end
157
+ end
158
+
159
+ context 'when not logging response headers' do
160
+ let(:logger_options) { { headers: { response: false } } }
161
+
162
+ it 'does not log response headers if option is false' do
163
+ conn.get '/hello', nil, accept: 'text/html'
164
+ expect(string_io.string).not_to match(%(Content-Type: "text/html))
165
+ end
166
+ end
167
+
168
+ context 'when logging request body' do
169
+ let(:logger_options) { { bodies: { request: true } } }
170
+
171
+ it 'log only request body' do
172
+ conn.post '/ohyes', 'name=Tamago', accept: 'text/html'
173
+ expect(string_io.string).to match(%(name=Tamago))
174
+ expect(string_io.string).not_to match(%(pebbles))
175
+ end
176
+ end
177
+
178
+ context 'when logging response body' do
179
+ let(:logger_options) { { bodies: { response: true } } }
180
+
181
+ it 'log only response body' do
182
+ conn.post '/ohyes', 'name=Hamachi', accept: 'text/html'
183
+ expect(string_io.string).to match(%(pebbles))
184
+ expect(string_io.string).not_to match(%(name=Hamachi))
185
+ end
186
+ end
187
+
188
+ context 'when logging request and response bodies' do
189
+ let(:logger_options) { { bodies: true } }
190
+
191
+ it 'log request and response body' do
192
+ conn.post '/ohyes', 'name=Ebi', accept: 'text/html'
193
+ expect(string_io.string).to match(%(name=Ebi))
194
+ expect(string_io.string).to match(%(pebbles))
195
+ end
196
+
197
+ it 'log response body object' do
198
+ conn.get '/rubbles', nil, accept: 'text/html'
199
+ expect(string_io.string).to match(%([\"Barney\", \"Betty\", \"Bam Bam\"]\n))
200
+ end
201
+
202
+ it 'logs filter body' do
203
+ conn.get '/filtered_body', nil, accept: 'text/html'
204
+ expect(string_io.string).to match(%(soylent green is))
205
+ expect(string_io.string).to match(%(tasty))
206
+ expect(string_io.string).not_to match(%(people))
207
+ end
208
+ end
209
+
210
+ context 'when logging errors' do
211
+ let(:logger_options) { { errors: true } }
212
+
213
+ it 'logs error message' do
214
+ expect { conn.get '/noroute' }.to raise_error(Faraday::Adapter::Test::Stubs::NotFound)
215
+ expect(string_io.string).to match(%(no stubbed request for get http:/noroute))
216
+ end
217
+ end
218
+
219
+ context 'when using log_level' do
220
+ let(:logger_options) { { bodies: true, log_level: :debug } }
221
+
222
+ it 'logs request/request body on the specified level (debug)' do
223
+ logger.level = Logger::DEBUG
224
+ conn.post '/ohyes', 'name=Ebi', accept: 'text/html'
225
+ expect(string_io.string).to match(%(name=Ebi))
226
+ expect(string_io.string).to match(%(pebbles))
227
+ end
228
+
229
+ it 'logs headers on the debug level' do
230
+ logger.level = Logger::DEBUG
231
+ conn.get '/hello', nil, accept: 'text/html'
232
+ expect(string_io.string).to match(%(Content-Type: "text/html))
233
+ end
234
+
235
+ it 'does not log request/response body on the info level' do
236
+ logger.level = Logger::INFO
237
+ conn.post '/ohyes', 'name=Ebi', accept: 'text/html'
238
+ expect(string_io.string).not_to match(%(name=Ebi))
239
+ expect(string_io.string).not_to match(%(pebbles))
240
+ end
241
+
242
+ it 'does not log headers on the info level' do
243
+ logger.level = Logger::INFO
244
+ conn.get '/hello', nil, accept: 'text/html'
245
+ expect(string_io.string).not_to match(%(Content-Type: "text/html))
246
+ end
247
+ end
248
+ end