faraday 0.15.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +570 -0
- data/LICENSE.md +1 -1
- data/README.md +25 -351
- data/Rakefile +7 -0
- data/examples/client_spec.rb +97 -0
- data/examples/client_test.rb +118 -0
- data/lib/faraday/adapter/test.rb +127 -72
- data/lib/faraday/adapter.rb +69 -22
- data/lib/faraday/adapter_registry.rb +30 -0
- data/lib/faraday/connection.rb +309 -232
- data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
- data/lib/faraday/error.rb +119 -38
- data/lib/faraday/logging/formatter.rb +106 -0
- data/lib/faraday/methods.rb +6 -0
- data/lib/faraday/middleware.rb +18 -25
- data/lib/faraday/middleware_registry.rb +83 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +32 -0
- data/lib/faraday/options/request_options.rb +22 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +41 -195
- data/lib/faraday/parameters.rb +4 -196
- data/lib/faraday/rack_builder.rb +91 -76
- data/lib/faraday/request/authorization.rb +37 -29
- data/lib/faraday/request/instrumentation.rb +47 -27
- data/lib/faraday/request/json.rb +55 -0
- data/lib/faraday/request/url_encoded.rb +45 -23
- data/lib/faraday/request.rb +74 -32
- data/lib/faraday/response/json.rb +54 -0
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +57 -14
- data/lib/faraday/response.rb +26 -33
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +47 -251
- data/lib/faraday/version.rb +5 -0
- data/lib/faraday.rb +108 -199
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/spec/faraday/adapter/test_spec.rb +377 -0
- data/spec/faraday/adapter_registry_spec.rb +28 -0
- data/spec/faraday/adapter_spec.rb +55 -0
- data/spec/faraday/connection_spec.rb +787 -0
- data/spec/faraday/error_spec.rb +60 -0
- data/spec/faraday/middleware_registry_spec.rb +31 -0
- data/spec/faraday/middleware_spec.rb +52 -0
- data/spec/faraday/options/env_spec.rb +70 -0
- data/spec/faraday/options/options_spec.rb +297 -0
- data/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/spec/faraday/options/request_options_spec.rb +19 -0
- data/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/spec/faraday/params_encoders/nested_spec.rb +142 -0
- data/spec/faraday/rack_builder_spec.rb +317 -0
- data/spec/faraday/request/authorization_spec.rb +83 -0
- data/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/spec/faraday/request/json_spec.rb +111 -0
- data/spec/faraday/request/url_encoded_spec.rb +82 -0
- data/spec/faraday/request_spec.rb +109 -0
- data/spec/faraday/response/json_spec.rb +117 -0
- data/spec/faraday/response/logger_spec.rb +220 -0
- data/spec/faraday/response/raise_error_spec.rb +172 -0
- data/spec/faraday/response_spec.rb +75 -0
- data/spec/faraday/utils/headers_spec.rb +82 -0
- data/spec/faraday/utils_spec.rb +117 -0
- data/spec/faraday_spec.rb +37 -0
- data/spec/spec_helper.rb +132 -0
- data/spec/support/disabling_stub.rb +14 -0
- data/spec/support/fake_safe_buffer.rb +15 -0
- data/spec/support/helper_methods.rb +96 -0
- data/spec/support/shared_examples/adapter.rb +104 -0
- data/spec/support/shared_examples/params_encoder.rb +18 -0
- data/spec/support/shared_examples/request_method.rb +249 -0
- data/spec/support/streaming_response_checker.rb +35 -0
- metadata +86 -34
- data/lib/faraday/adapter/em_http.rb +0 -243
- data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
- data/lib/faraday/adapter/em_synchrony.rb +0 -106
- data/lib/faraday/adapter/excon.rb +0 -79
- data/lib/faraday/adapter/httpclient.rb +0 -128
- data/lib/faraday/adapter/net_http.rb +0 -137
- data/lib/faraday/adapter/net_http_persistent.rb +0 -63
- data/lib/faraday/adapter/patron.rb +0 -100
- data/lib/faraday/adapter/rack.rb +0 -58
- data/lib/faraday/adapter/typhoeus.rb +0 -12
- data/lib/faraday/autoload.rb +0 -84
- data/lib/faraday/request/basic_authentication.rb +0 -13
- data/lib/faraday/request/multipart.rb +0 -68
- data/lib/faraday/request/retry.rb +0 -211
- data/lib/faraday/request/token_authentication.rb +0 -15
- data/lib/faraday/upload_io.rb +0 -67
@@ -0,0 +1,172 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Response::RaiseError do
|
4
|
+
let(:conn) do
|
5
|
+
Faraday.new do |b|
|
6
|
+
b.response :raise_error
|
7
|
+
b.adapter :test do |stub|
|
8
|
+
stub.get('ok') { [200, { 'Content-Type' => 'text/html' }, '<body></body>'] }
|
9
|
+
stub.get('bad-request') { [400, { 'X-Reason' => 'because' }, 'keep looking'] }
|
10
|
+
stub.get('unauthorized') { [401, { 'X-Reason' => 'because' }, 'keep looking'] }
|
11
|
+
stub.get('forbidden') { [403, { 'X-Reason' => 'because' }, 'keep looking'] }
|
12
|
+
stub.get('not-found') { [404, { 'X-Reason' => 'because' }, 'keep looking'] }
|
13
|
+
stub.get('proxy-error') { [407, { 'X-Reason' => 'because' }, 'keep looking'] }
|
14
|
+
stub.get('conflict') { [409, { 'X-Reason' => 'because' }, 'keep looking'] }
|
15
|
+
stub.get('unprocessable-entity') { [422, { 'X-Reason' => 'because' }, 'keep looking'] }
|
16
|
+
stub.get('4xx') { [499, { 'X-Reason' => 'because' }, 'keep looking'] }
|
17
|
+
stub.get('nil-status') { [nil, { 'X-Reason' => 'nil' }, 'fail'] }
|
18
|
+
stub.get('server-error') { [500, { 'X-Error' => 'bailout' }, 'fail'] }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'raises no exception for 200 responses' do
|
24
|
+
expect { conn.get('ok') }.not_to raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'raises Faraday::BadRequestError for 400 responses' do
|
28
|
+
expect { conn.get('bad-request') }.to raise_error(Faraday::BadRequestError) do |ex|
|
29
|
+
expect(ex.message).to eq('the server responded with status 400')
|
30
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
31
|
+
expect(ex.response[:status]).to eq(400)
|
32
|
+
expect(ex.response_status).to eq(400)
|
33
|
+
expect(ex.response_body).to eq('keep looking')
|
34
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'raises Faraday::UnauthorizedError for 401 responses' do
|
39
|
+
expect { conn.get('unauthorized') }.to raise_error(Faraday::UnauthorizedError) do |ex|
|
40
|
+
expect(ex.message).to eq('the server responded with status 401')
|
41
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
42
|
+
expect(ex.response[:status]).to eq(401)
|
43
|
+
expect(ex.response_status).to eq(401)
|
44
|
+
expect(ex.response_body).to eq('keep looking')
|
45
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'raises Faraday::ForbiddenError for 403 responses' do
|
50
|
+
expect { conn.get('forbidden') }.to raise_error(Faraday::ForbiddenError) do |ex|
|
51
|
+
expect(ex.message).to eq('the server responded with status 403')
|
52
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
53
|
+
expect(ex.response[:status]).to eq(403)
|
54
|
+
expect(ex.response_status).to eq(403)
|
55
|
+
expect(ex.response_body).to eq('keep looking')
|
56
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'raises Faraday::ResourceNotFound for 404 responses' do
|
61
|
+
expect { conn.get('not-found') }.to raise_error(Faraday::ResourceNotFound) do |ex|
|
62
|
+
expect(ex.message).to eq('the server responded with status 404')
|
63
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
64
|
+
expect(ex.response[:status]).to eq(404)
|
65
|
+
expect(ex.response_status).to eq(404)
|
66
|
+
expect(ex.response_body).to eq('keep looking')
|
67
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'raises Faraday::ProxyAuthError for 407 responses' do
|
72
|
+
expect { conn.get('proxy-error') }.to raise_error(Faraday::ProxyAuthError) do |ex|
|
73
|
+
expect(ex.message).to eq('407 "Proxy Authentication Required"')
|
74
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
75
|
+
expect(ex.response[:status]).to eq(407)
|
76
|
+
expect(ex.response_status).to eq(407)
|
77
|
+
expect(ex.response_body).to eq('keep looking')
|
78
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'raises Faraday::ConflictError for 409 responses' do
|
83
|
+
expect { conn.get('conflict') }.to raise_error(Faraday::ConflictError) do |ex|
|
84
|
+
expect(ex.message).to eq('the server responded with status 409')
|
85
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
86
|
+
expect(ex.response[:status]).to eq(409)
|
87
|
+
expect(ex.response_status).to eq(409)
|
88
|
+
expect(ex.response_body).to eq('keep looking')
|
89
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'raises Faraday::UnprocessableEntityError for 422 responses' do
|
94
|
+
expect { conn.get('unprocessable-entity') }.to raise_error(Faraday::UnprocessableEntityError) do |ex|
|
95
|
+
expect(ex.message).to eq('the server responded with status 422')
|
96
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
97
|
+
expect(ex.response[:status]).to eq(422)
|
98
|
+
expect(ex.response_status).to eq(422)
|
99
|
+
expect(ex.response_body).to eq('keep looking')
|
100
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'raises Faraday::NilStatusError for nil status in response' do
|
105
|
+
expect { conn.get('nil-status') }.to raise_error(Faraday::NilStatusError) do |ex|
|
106
|
+
expect(ex.message).to eq('http status could not be derived from the server response')
|
107
|
+
expect(ex.response[:headers]['X-Reason']).to eq('nil')
|
108
|
+
expect(ex.response[:status]).to be_nil
|
109
|
+
expect(ex.response_status).to be_nil
|
110
|
+
expect(ex.response_body).to eq('fail')
|
111
|
+
expect(ex.response_headers['X-Reason']).to eq('nil')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'raises Faraday::ClientError for other 4xx responses' do
|
116
|
+
expect { conn.get('4xx') }.to raise_error(Faraday::ClientError) do |ex|
|
117
|
+
expect(ex.message).to eq('the server responded with status 499')
|
118
|
+
expect(ex.response[:headers]['X-Reason']).to eq('because')
|
119
|
+
expect(ex.response[:status]).to eq(499)
|
120
|
+
expect(ex.response_status).to eq(499)
|
121
|
+
expect(ex.response_body).to eq('keep looking')
|
122
|
+
expect(ex.response_headers['X-Reason']).to eq('because')
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'raises Faraday::ServerError for 500 responses' do
|
127
|
+
expect { conn.get('server-error') }.to raise_error(Faraday::ServerError) do |ex|
|
128
|
+
expect(ex.message).to eq('the server responded with status 500')
|
129
|
+
expect(ex.response[:headers]['X-Error']).to eq('bailout')
|
130
|
+
expect(ex.response[:status]).to eq(500)
|
131
|
+
expect(ex.response_status).to eq(500)
|
132
|
+
expect(ex.response_body).to eq('fail')
|
133
|
+
expect(ex.response_headers['X-Error']).to eq('bailout')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe 'request info' do
|
138
|
+
let(:conn) do
|
139
|
+
Faraday.new do |b|
|
140
|
+
b.response :raise_error
|
141
|
+
b.adapter :test do |stub|
|
142
|
+
stub.post(url, request_body, request_headers) do
|
143
|
+
[400, { 'X-Reason' => 'because' }, 'keep looking']
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
let(:request_body) { JSON.generate({ 'item' => 'sth' }) }
|
149
|
+
let(:request_headers) { { 'Authorization' => 'Basic 123' } }
|
150
|
+
let(:url_path) { 'request' }
|
151
|
+
let(:query_params) { 'full=true' }
|
152
|
+
let(:url) { "#{url_path}?#{query_params}" }
|
153
|
+
|
154
|
+
subject(:perform_request) do
|
155
|
+
conn.post url do |req|
|
156
|
+
req.headers['Authorization'] = 'Basic 123'
|
157
|
+
req.body = request_body
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'returns the request info in the exception' do
|
162
|
+
expect { perform_request }.to raise_error(Faraday::BadRequestError) do |ex|
|
163
|
+
expect(ex.response[:request][:method]).to eq(:post)
|
164
|
+
expect(ex.response[:request][:url]).to eq(URI("http:/#{url}"))
|
165
|
+
expect(ex.response[:request][:url_path]).to eq("/#{url_path}")
|
166
|
+
expect(ex.response[:request][:params]).to eq({ 'full' => 'true' })
|
167
|
+
expect(ex.response[:request][:headers]).to match(a_hash_including(request_headers))
|
168
|
+
expect(ex.response[:request][:body]).to eq(request_body)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Response do
|
4
|
+
subject { Faraday::Response.new(env) }
|
5
|
+
|
6
|
+
let(:env) do
|
7
|
+
Faraday::Env.from(status: 404, body: 'yikes',
|
8
|
+
response_headers: { 'Content-Type' => 'text/plain' })
|
9
|
+
end
|
10
|
+
|
11
|
+
it { expect(subject.finished?).to be_truthy }
|
12
|
+
it { expect { subject.finish({}) }.to raise_error(RuntimeError) }
|
13
|
+
it { expect(subject.success?).to be_falsey }
|
14
|
+
it { expect(subject.status).to eq(404) }
|
15
|
+
it { expect(subject.body).to eq('yikes') }
|
16
|
+
it { expect(subject.headers['Content-Type']).to eq('text/plain') }
|
17
|
+
it { expect(subject['content-type']).to eq('text/plain') }
|
18
|
+
|
19
|
+
describe '#apply_request' do
|
20
|
+
before { subject.apply_request(body: 'a=b', method: :post) }
|
21
|
+
|
22
|
+
it { expect(subject.body).to eq('yikes') }
|
23
|
+
it { expect(subject.env[:method]).to eq(:post) }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#to_hash' do
|
27
|
+
let(:hash) { subject.to_hash }
|
28
|
+
|
29
|
+
it { expect(hash).to be_a(Hash) }
|
30
|
+
it { expect(hash[:status]).to eq(subject.status) }
|
31
|
+
it { expect(hash[:response_headers]).to eq(subject.headers) }
|
32
|
+
it { expect(hash[:body]).to eq(subject.body) }
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'marshal serialization support' do
|
36
|
+
subject { Faraday::Response.new }
|
37
|
+
let(:loaded) { Marshal.load(Marshal.dump(subject)) }
|
38
|
+
|
39
|
+
before do
|
40
|
+
subject.on_complete {}
|
41
|
+
subject.finish(env.merge(params: 'moo'))
|
42
|
+
end
|
43
|
+
|
44
|
+
it { expect(loaded.env[:params]).to be_nil }
|
45
|
+
it { expect(loaded.env[:body]).to eq(env[:body]) }
|
46
|
+
it { expect(loaded.env[:response_headers]).to eq(env[:response_headers]) }
|
47
|
+
it { expect(loaded.env[:status]).to eq(env[:status]) }
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#on_complete' do
|
51
|
+
subject { Faraday::Response.new }
|
52
|
+
|
53
|
+
it 'parse body on finish' do
|
54
|
+
subject.on_complete { |env| env[:body] = env[:body].upcase }
|
55
|
+
subject.finish(env)
|
56
|
+
|
57
|
+
expect(subject.body).to eq('YIKES')
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'can access response body in on_complete callback' do
|
61
|
+
subject.on_complete { |env| env[:body] = subject.body.upcase }
|
62
|
+
subject.finish(env)
|
63
|
+
|
64
|
+
expect(subject.body).to eq('YIKES')
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'can access response body in on_complete callback' do
|
68
|
+
callback_env = nil
|
69
|
+
subject.on_complete { |env| callback_env = env }
|
70
|
+
subject.finish({})
|
71
|
+
|
72
|
+
expect(subject.env).to eq(callback_env)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Utils::Headers do
|
4
|
+
subject { Faraday::Utils::Headers.new }
|
5
|
+
|
6
|
+
context 'when Content-Type is set to application/json' do
|
7
|
+
before { subject['Content-Type'] = 'application/json' }
|
8
|
+
|
9
|
+
it { expect(subject.keys).to eq(['Content-Type']) }
|
10
|
+
it { expect(subject['Content-Type']).to eq('application/json') }
|
11
|
+
it { expect(subject['CONTENT-TYPE']).to eq('application/json') }
|
12
|
+
it { expect(subject['content-type']).to eq('application/json') }
|
13
|
+
it { is_expected.to include('content-type') }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when Content-Type is set to application/xml' do
|
17
|
+
before { subject['Content-Type'] = 'application/xml' }
|
18
|
+
|
19
|
+
it { expect(subject.keys).to eq(['Content-Type']) }
|
20
|
+
it { expect(subject['Content-Type']).to eq('application/xml') }
|
21
|
+
it { expect(subject['CONTENT-TYPE']).to eq('application/xml') }
|
22
|
+
it { expect(subject['content-type']).to eq('application/xml') }
|
23
|
+
it { is_expected.to include('content-type') }
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#fetch' do
|
27
|
+
before { subject['Content-Type'] = 'application/json' }
|
28
|
+
|
29
|
+
it { expect(subject.fetch('Content-Type')).to eq('application/json') }
|
30
|
+
it { expect(subject.fetch('CONTENT-TYPE')).to eq('application/json') }
|
31
|
+
it { expect(subject.fetch(:content_type)).to eq('application/json') }
|
32
|
+
it { expect(subject.fetch('invalid', 'default')).to eq('default') }
|
33
|
+
it { expect(subject.fetch('invalid', false)).to eq(false) }
|
34
|
+
it { expect(subject.fetch('invalid', nil)).to be_nil }
|
35
|
+
it { expect(subject.fetch('Invalid') { |key| "#{key} key" }).to eq('Invalid key') }
|
36
|
+
it 'calls a block when provided' do
|
37
|
+
block_called = false
|
38
|
+
expect(subject.fetch('content-type') { block_called = true }).to eq('application/json')
|
39
|
+
expect(block_called).to be_falsey
|
40
|
+
end
|
41
|
+
it 'raises an error if key not found' do
|
42
|
+
expected_error = defined?(KeyError) ? KeyError : IndexError
|
43
|
+
expect { subject.fetch('invalid') }.to raise_error(expected_error)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#delete' do
|
48
|
+
before do
|
49
|
+
subject['Content-Type'] = 'application/json'
|
50
|
+
@deleted = subject.delete('content-type')
|
51
|
+
end
|
52
|
+
|
53
|
+
it { expect(@deleted).to eq('application/json') }
|
54
|
+
it { expect(subject.size).to eq(0) }
|
55
|
+
it { is_expected.not_to include('content-type') }
|
56
|
+
it { expect(subject.delete('content-type')).to be_nil }
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#parse' do
|
60
|
+
before { subject.parse(headers) }
|
61
|
+
|
62
|
+
context 'when response headers leave http status line out' do
|
63
|
+
let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n" }
|
64
|
+
|
65
|
+
it { expect(subject.keys).to eq(%w[Content-Type]) }
|
66
|
+
it { expect(subject['Content-Type']).to eq('text/html') }
|
67
|
+
it { expect(subject['content-type']).to eq('text/html') }
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when response headers values include a colon' do
|
71
|
+
let(:headers) { "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nLocation: http://httpbingo.org/\r\n\r\n" }
|
72
|
+
|
73
|
+
it { expect(subject['location']).to eq('http://httpbingo.org/') }
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when response headers include a blank line' do
|
77
|
+
let(:headers) { "HTTP/1.1 200 OK\r\n\r\nContent-Type: text/html\r\n\r\n" }
|
78
|
+
|
79
|
+
it { expect(subject['content-type']).to eq('text/html') }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday::Utils do
|
4
|
+
describe 'headers parsing' do
|
5
|
+
let(:multi_response_headers) do
|
6
|
+
"HTTP/1.x 500 OK\r\nContent-Type: text/html; charset=UTF-8\r\n" \
|
7
|
+
"HTTP/1.x 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n"
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'parse headers for aggregated responses' do
|
11
|
+
headers = Faraday::Utils::Headers.new
|
12
|
+
headers.parse(multi_response_headers)
|
13
|
+
|
14
|
+
result = headers.to_hash
|
15
|
+
|
16
|
+
expect(result['Content-Type']).to eq('application/json; charset=UTF-8')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'URI parsing' do
|
21
|
+
let(:url) { 'http://example.com/abc' }
|
22
|
+
|
23
|
+
it 'escapes safe buffer' do
|
24
|
+
str = FakeSafeBuffer.new('$32,000.00')
|
25
|
+
expect(Faraday::Utils.escape(str)).to eq('%2432%2C000.00')
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'parses with default parser' do
|
29
|
+
with_default_uri_parser(nil) do
|
30
|
+
uri = normalize(url)
|
31
|
+
expect(uri.host).to eq('example.com')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'parses with URI' do
|
36
|
+
with_default_uri_parser(::URI) do
|
37
|
+
uri = normalize(url)
|
38
|
+
expect(uri.host).to eq('example.com')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'parses with block' do
|
43
|
+
with_default_uri_parser(->(u) { "booya#{'!' * u.size}" }) do
|
44
|
+
expect(normalize(url)).to eq('booya!!!!!!!!!!!!!!!!!!!!!!')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'replaces headers hash' do
|
49
|
+
headers = Faraday::Utils::Headers.new('authorization' => 't0ps3cr3t!')
|
50
|
+
expect(headers).to have_key('authorization')
|
51
|
+
|
52
|
+
headers.replace('content-type' => 'text/plain')
|
53
|
+
expect(headers).not_to have_key('authorization')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '.deep_merge!' do
|
58
|
+
let(:connection_options) { Faraday::ConnectionOptions.new }
|
59
|
+
let(:url) do
|
60
|
+
{
|
61
|
+
url: 'http://example.com/abc',
|
62
|
+
headers: { 'Mime-Version' => '1.0' },
|
63
|
+
request: { oauth: { consumer_key: 'anonymous' } },
|
64
|
+
ssl: { version: '2' }
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'recursively merges the headers' do
|
69
|
+
connection_options.headers = { user_agent: 'My Agent 1.0' }
|
70
|
+
deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
|
71
|
+
|
72
|
+
expect(deep_merge.headers).to eq('Mime-Version' => '1.0', user_agent: 'My Agent 1.0')
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'when a target hash has an Options Struct value' do
|
76
|
+
let(:request) do
|
77
|
+
{
|
78
|
+
params_encoder: nil,
|
79
|
+
proxy: nil,
|
80
|
+
bind: nil,
|
81
|
+
timeout: nil,
|
82
|
+
open_timeout: nil,
|
83
|
+
read_timeout: nil,
|
84
|
+
write_timeout: nil,
|
85
|
+
boundary: nil,
|
86
|
+
oauth: { consumer_key: 'anonymous' },
|
87
|
+
context: nil,
|
88
|
+
on_data: nil
|
89
|
+
}
|
90
|
+
end
|
91
|
+
let(:ssl) do
|
92
|
+
{
|
93
|
+
verify: nil,
|
94
|
+
ca_file: nil,
|
95
|
+
ca_path: nil,
|
96
|
+
verify_mode: nil,
|
97
|
+
cert_store: nil,
|
98
|
+
client_cert: nil,
|
99
|
+
client_key: nil,
|
100
|
+
certificate: nil,
|
101
|
+
private_key: nil,
|
102
|
+
verify_depth: nil,
|
103
|
+
version: '2',
|
104
|
+
min_version: nil,
|
105
|
+
max_version: nil
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'does not overwrite an Options Struct value' do
|
110
|
+
deep_merge = Faraday::Utils.deep_merge!(connection_options, url)
|
111
|
+
|
112
|
+
expect(deep_merge.request.to_h).to eq(request)
|
113
|
+
expect(deep_merge.ssl.to_h).to eq(ssl)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe Faraday do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(Faraday::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'proxies to default_connection' do
|
9
|
+
let(:mock_connection) { double('Connection') }
|
10
|
+
before do
|
11
|
+
Faraday.default_connection = mock_connection
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'proxies methods that exist on the default_connection' do
|
15
|
+
expect(mock_connection).to receive(:this_should_be_proxied)
|
16
|
+
|
17
|
+
Faraday.this_should_be_proxied
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'uses method_missing on Faraday if there is no proxyable method' do
|
21
|
+
expect { Faraday.this_method_does_not_exist }.to raise_error(
|
22
|
+
NoMethodError,
|
23
|
+
"undefined method `this_method_does_not_exist' for Faraday:Module"
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'proxied methods can be accessed' do
|
28
|
+
allow(mock_connection).to receive(:this_should_be_proxied)
|
29
|
+
|
30
|
+
expect(Faraday.method(:this_should_be_proxied)).to be_a(Method)
|
31
|
+
end
|
32
|
+
|
33
|
+
after do
|
34
|
+
Faraday.default_connection = nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
6
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
7
|
+
# files.
|
8
|
+
#
|
9
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
10
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
11
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
12
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
13
|
+
# a separate helper file that requires the additional dependencies and performs
|
14
|
+
# the additional setup, and require it from the spec files that actually need
|
15
|
+
# it.
|
16
|
+
#
|
17
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
18
|
+
|
19
|
+
require 'simplecov'
|
20
|
+
require 'coveralls'
|
21
|
+
require 'webmock/rspec'
|
22
|
+
WebMock.disable_net_connect!(allow_localhost: true)
|
23
|
+
|
24
|
+
SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
|
25
|
+
|
26
|
+
SimpleCov.start do
|
27
|
+
add_filter '/spec/'
|
28
|
+
minimum_coverage 84
|
29
|
+
minimum_coverage_by_file 26
|
30
|
+
end
|
31
|
+
|
32
|
+
# Ensure all /lib files are loaded
|
33
|
+
# so they will be included in the test coverage report.
|
34
|
+
Dir['./lib/**/*.rb'].sort.each { |file| require file }
|
35
|
+
|
36
|
+
require 'faraday'
|
37
|
+
require 'pry'
|
38
|
+
|
39
|
+
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
|
40
|
+
|
41
|
+
RSpec.configure do |config|
|
42
|
+
# rspec-expectations config goes here. You can use an alternate
|
43
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
44
|
+
# assertions if you prefer.
|
45
|
+
config.expect_with :rspec do |expectations|
|
46
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
47
|
+
# and `failure_message` of custom matchers include text for helper methods
|
48
|
+
# defined using `chain`, e.g.:
|
49
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
50
|
+
# # => "be bigger than 2 and smaller than 4"
|
51
|
+
# ...rather than:
|
52
|
+
# # => "be bigger than 2"
|
53
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
54
|
+
end
|
55
|
+
|
56
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
57
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
58
|
+
config.mock_with :rspec do |mocks|
|
59
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
60
|
+
# a real object. This is generally recommended, and will default to
|
61
|
+
# `true` in RSpec 4.
|
62
|
+
mocks.verify_partial_doubles = true
|
63
|
+
end
|
64
|
+
|
65
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
66
|
+
# have no way to turn it off -- the option exists only for backwards
|
67
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
68
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
69
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
70
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
71
|
+
|
72
|
+
# This allows you to limit a spec run to individual examples or groups
|
73
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
74
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
75
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
76
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
77
|
+
# config.filter_run_when_matching :focus
|
78
|
+
|
79
|
+
# Allows RSpec to persist some state between runs in order to support
|
80
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
81
|
+
# you configure your source control system to ignore this file.
|
82
|
+
# config.example_status_persistence_file_path = "spec/examples.txt"
|
83
|
+
|
84
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
85
|
+
# recommended. For more details, see:
|
86
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
87
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
88
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
89
|
+
# config.disable_monkey_patching!
|
90
|
+
|
91
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
92
|
+
# be too noisy due to issues in dependencies.
|
93
|
+
# config.warnings = true
|
94
|
+
|
95
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
96
|
+
# file, and it's useful to allow more verbose output when running an
|
97
|
+
# individual spec file.
|
98
|
+
# if config.files_to_run.one?
|
99
|
+
# # Use the documentation formatter for detailed output,
|
100
|
+
# # unless a formatter has already been configured
|
101
|
+
# # (e.g. via a command-line flag).
|
102
|
+
# config.default_formatter = "doc"
|
103
|
+
# end
|
104
|
+
|
105
|
+
# Print the 10 slowest examples and example groups at the
|
106
|
+
# end of the spec run, to help surface which specs are running
|
107
|
+
# particularly slow.
|
108
|
+
# config.profile_examples = 10
|
109
|
+
|
110
|
+
# Run specs in random order to surface order dependencies. If you find an
|
111
|
+
# order dependency and want to debug it, you can fix the order by providing
|
112
|
+
# the seed, which is printed after each run.
|
113
|
+
# --seed 1234
|
114
|
+
config.order = :random
|
115
|
+
|
116
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
117
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
118
|
+
# test failures related to randomization by passing the same `--seed` value
|
119
|
+
# as the one that triggered the failure.
|
120
|
+
Kernel.srand config.seed
|
121
|
+
|
122
|
+
config.include Faraday::HelperMethods
|
123
|
+
end
|
124
|
+
|
125
|
+
# Extends RSpec DocumentationFormatter to hide skipped tests.
|
126
|
+
module FormatterOverrides
|
127
|
+
def example_pending(_arg); end
|
128
|
+
|
129
|
+
def dump_pending(_arg); end
|
130
|
+
|
131
|
+
RSpec::Core::Formatters::DocumentationFormatter.prepend self
|
132
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# emulates ActiveSupport::SafeBuffer#gsub
|
4
|
+
FakeSafeBuffer = Struct.new(:string) do
|
5
|
+
def to_s
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
def gsub(regex)
|
10
|
+
string.gsub(regex) do
|
11
|
+
match, = Regexp.last_match(0), '' =~ /a/ # rubocop:disable Performance/StringInclude
|
12
|
+
yield(match)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|