faraday 0.12.0 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +380 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +21 -308
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +65 -0
  7. data/examples/client_test.rb +79 -0
  8. data/lib/faraday/adapter/httpclient.rb +83 -59
  9. data/lib/faraday/adapter/patron.rb +92 -36
  10. data/lib/faraday/adapter/rack.rb +30 -13
  11. data/lib/faraday/adapter/test.rb +103 -62
  12. data/lib/faraday/adapter/typhoeus.rb +7 -115
  13. data/lib/faraday/adapter.rb +77 -22
  14. data/lib/faraday/adapter_registry.rb +30 -0
  15. data/lib/faraday/autoload.rb +42 -36
  16. data/lib/faraday/connection.rb +345 -179
  17. data/lib/faraday/dependency_loader.rb +37 -0
  18. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  19. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  20. data/lib/faraday/error.rb +127 -38
  21. data/lib/faraday/file_part.rb +128 -0
  22. data/lib/faraday/logging/formatter.rb +105 -0
  23. data/lib/faraday/methods.rb +6 -0
  24. data/lib/faraday/middleware.rb +19 -25
  25. data/lib/faraday/middleware_registry.rb +129 -0
  26. data/lib/faraday/options/connection_options.rb +22 -0
  27. data/lib/faraday/options/env.rb +181 -0
  28. data/lib/faraday/options/proxy_options.rb +32 -0
  29. data/lib/faraday/options/request_options.rb +22 -0
  30. data/lib/faraday/options/ssl_options.rb +59 -0
  31. data/lib/faraday/options.rb +39 -193
  32. data/lib/faraday/param_part.rb +53 -0
  33. data/lib/faraday/parameters.rb +4 -196
  34. data/lib/faraday/rack_builder.rb +84 -48
  35. data/lib/faraday/request/authorization.rb +44 -30
  36. data/lib/faraday/request/basic_authentication.rb +14 -7
  37. data/lib/faraday/request/instrumentation.rb +45 -27
  38. data/lib/faraday/request/multipart.rb +86 -48
  39. data/lib/faraday/request/retry.rb +209 -133
  40. data/lib/faraday/request/token_authentication.rb +15 -10
  41. data/lib/faraday/request/url_encoded.rb +43 -23
  42. data/lib/faraday/request.rb +94 -32
  43. data/lib/faraday/response/logger.rb +22 -69
  44. data/lib/faraday/response/raise_error.rb +49 -14
  45. data/lib/faraday/response.rb +27 -23
  46. data/lib/faraday/utils/headers.rb +139 -0
  47. data/lib/faraday/utils/params_hash.rb +61 -0
  48. data/lib/faraday/utils.rb +38 -238
  49. data/lib/faraday/version.rb +5 -0
  50. data/lib/faraday.rb +124 -187
  51. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  52. data/spec/faraday/adapter/em_http_spec.rb +47 -0
  53. data/spec/faraday/adapter/em_synchrony_spec.rb +16 -0
  54. data/spec/faraday/adapter/excon_spec.rb +49 -0
  55. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  56. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  57. data/spec/faraday/adapter/patron_spec.rb +18 -0
  58. data/spec/faraday/adapter/rack_spec.rb +8 -0
  59. data/spec/faraday/adapter/test_spec.rb +260 -0
  60. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  61. data/spec/faraday/adapter_registry_spec.rb +28 -0
  62. data/spec/faraday/adapter_spec.rb +55 -0
  63. data/spec/faraday/composite_read_io_spec.rb +80 -0
  64. data/spec/faraday/connection_spec.rb +736 -0
  65. data/spec/faraday/error_spec.rb +60 -0
  66. data/spec/faraday/middleware_spec.rb +52 -0
  67. data/spec/faraday/options/env_spec.rb +70 -0
  68. data/spec/faraday/options/options_spec.rb +297 -0
  69. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  70. data/spec/faraday/options/request_options_spec.rb +19 -0
  71. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  72. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  73. data/spec/faraday/rack_builder_spec.rb +345 -0
  74. data/spec/faraday/request/authorization_spec.rb +88 -0
  75. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  76. data/spec/faraday/request/multipart_spec.rb +302 -0
  77. data/spec/faraday/request/retry_spec.rb +242 -0
  78. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  79. data/spec/faraday/request_spec.rb +120 -0
  80. data/spec/faraday/response/logger_spec.rb +220 -0
  81. data/spec/faraday/response/middleware_spec.rb +68 -0
  82. data/spec/faraday/response/raise_error_spec.rb +169 -0
  83. data/spec/faraday/response_spec.rb +75 -0
  84. data/spec/faraday/utils/headers_spec.rb +82 -0
  85. data/spec/faraday/utils_spec.rb +56 -0
  86. data/spec/faraday_spec.rb +37 -0
  87. data/spec/spec_helper.rb +132 -0
  88. data/spec/support/disabling_stub.rb +14 -0
  89. data/spec/support/fake_safe_buffer.rb +15 -0
  90. data/spec/support/helper_methods.rb +133 -0
  91. data/spec/support/shared_examples/adapter.rb +105 -0
  92. data/spec/support/shared_examples/params_encoder.rb +18 -0
  93. data/spec/support/shared_examples/request_method.rb +262 -0
  94. data/spec/support/streaming_response_checker.rb +35 -0
  95. data/spec/support/webmock_rack_app.rb +68 -0
  96. metadata +164 -16
  97. data/lib/faraday/adapter/em_http.rb +0 -243
  98. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  99. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  100. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  101. data/lib/faraday/adapter/excon.rb +0 -80
  102. data/lib/faraday/adapter/net_http.rb +0 -135
  103. data/lib/faraday/adapter/net_http_persistent.rb +0 -50
  104. data/lib/faraday/upload_io.rb +0 -67
data/README.md CHANGED
@@ -1,319 +1,30 @@
1
- # Faraday
1
+ # [![Faraday](./docs/assets/img/repo-card-slim.png)][website]
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
- [![Build Status](https://travis-ci.org/lostisland/faraday.svg)](https://travis-ci.org/lostisland/faraday)
4
+ [![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
5
5
  [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
6
6
 
7
7
 
8
- Faraday is an HTTP client lib that provides a common interface over many
8
+ Faraday is an HTTP client library that provides a common interface over many
9
9
  adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
10
10
  processing the request/response cycle.
11
11
 
12
- Faraday supports these adapters:
12
+ ## Getting Started
13
13
 
14
- * [Net::HTTP][net_http] _(default)_
15
- * [Net::HTTP::Persistent][persistent]
16
- * [Excon][]
17
- * [Patron][]
18
- * [EventMachine][]
19
- * [HTTPClient][]
20
-
21
- It also includes a Rack adapter for hitting loaded Rack applications through
22
- Rack::Test, and a Test adapter for stubbing requests by hand.
23
-
24
- ## API documentation
25
-
26
- Available at [rubydoc.info](http://www.rubydoc.info/gems/faraday).
27
-
28
- ## Usage
29
-
30
- ### Basic Use
31
-
32
- ```ruby
33
- response = Faraday.get 'http://sushi.com/nigiri/sake.json'
34
- ```
35
- A simple `get` request can be performed by using the syntax described above. This works if you don't need to set up anything; you can roll with just the default middleware
36
- stack and default adapter (see [Faraday::RackBuilder#initialize](https://github.com/lostisland/faraday/blob/master/lib/faraday/rack_builder.rb)).
37
-
38
- A more flexible way to use Faraday is to start with a Connection object. If you want to keep the same defaults, you can use this syntax:
39
-
40
- ```ruby
41
- conn = Faraday.new(:url => 'http://www.example.com')
42
- response = conn.get '/users' # GET http://www.example.com/users'
43
- ```
44
-
45
- Connections can also take an options hash as a parameter or be configured by using a block. Checkout the section called [Advanced middleware usage](#advanced-middleware-usage) for more details about how to use this block for configurations.
46
-
47
- ```ruby
48
- conn = Faraday.new(:url => 'http://sushi.com') do |faraday|
49
- faraday.request :url_encoded # form-encode POST params
50
- faraday.response :logger # log requests to STDOUT
51
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
52
- end
53
-
54
- # Filter sensitive information from logs with a regex matcher
55
-
56
- conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
57
- faraday.response :logger do | logger |
58
- logger.filter(/(api_key=)(\w+)/,'\1[REMOVED]')
59
- end
60
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
61
- end
62
- ```
63
-
64
- Once you have the connection object, use it to make HTTP requests. You can pass paramters to it in a few different ways:
65
-
66
- ```ruby
67
- ## GET ##
68
-
69
- response = conn.get '/nigiri/sake.json' # GET http://sushi.com/nigiri/sake.json
70
- response.body
71
-
72
- conn.get '/nigiri', { :name => 'Maguro' } # GET http://sushi.com/nigiri?name=Maguro
73
-
74
- conn.get do |req| # GET http://sushi.com/search?page=2&limit=100
75
- req.url '/search', :page => 2
76
- req.params['limit'] = 100
77
- end
78
-
79
- ## POST ##
80
-
81
- conn.post '/nigiri', { :name => 'Maguro' } # POST "name=maguro" to http://sushi.com/nigiri
82
- ```
83
-
84
- Some configuration options can be adjusted per request:
85
-
86
- ```ruby
87
- # post payload as JSON instead of "www-form-urlencoded" encoding:
88
- conn.post do |req|
89
- req.url '/nigiri'
90
- req.headers['Content-Type'] = 'application/json'
91
- req.body = '{ "name": "Unagi" }'
92
- end
93
-
94
- ## Per-request options ##
95
-
96
- conn.get do |req|
97
- req.url '/search'
98
- req.options.timeout = 5 # open/read timeout in seconds
99
- req.options.open_timeout = 2 # connection open timeout in seconds
100
- end
101
- ```
102
-
103
- And you can inject arbitrary data into the request using the `context` option:
104
-
105
- ```ruby
106
- # Anything you inject using context option will be available in the env on all middlewares
107
-
108
- conn.get do |req|
109
- req.url '/search'
110
- req.options.context = {
111
- foo: 'foo',
112
- bar: 'bar'
113
- }
114
- end
115
- ```
116
-
117
- ### Changing how parameters are serialized
118
-
119
- Sometimes you need to send the same URL parameter multiple times with different
120
- values. This requires manually setting the parameter encoder and can be done on
121
- either per-connection or per-request basis.
122
-
123
- ```ruby
124
- # per-connection setting
125
- conn = Faraday.new :request => { :params_encoder => Faraday::FlatParamsEncoder }
126
-
127
- conn.get do |req|
128
- # per-request setting:
129
- # req.options.params_encoder = my_encoder
130
- req.params['roll'] = ['california', 'philadelphia']
131
- end
132
- # GET 'http://sushi.com?roll=california&roll=philadelphia'
133
- ```
134
-
135
- The value of Faraday `params_encoder` can be any object that responds to:
136
-
137
- * `encode(hash) #=> String`
138
- * `decode(string) #=> Hash`
139
-
140
- The encoder will affect both how query strings are processed and how POST bodies
141
- get serialized. The default encoder is Faraday::NestedParamsEncoder.
142
-
143
- ## Advanced middleware usage
144
-
145
- The order in which middleware is stacked is important. Like with Rack, the
146
- first middleware on the list wraps all others, while the last middleware is the
147
- innermost one, so that must be the adapter.
148
-
149
- ```ruby
150
- Faraday.new(...) do |conn|
151
- # POST/PUT params encoders:
152
- conn.request :multipart
153
- conn.request :url_encoded
154
-
155
- conn.adapter :net_http
156
- end
157
- ```
158
-
159
- This request middleware setup affects POST/PUT requests in the following way:
160
-
161
- 1. `Request::Multipart` checks for files in the payload, otherwise leaves
162
- everything untouched;
163
- 2. `Request::UrlEncoded` encodes as "application/x-www-form-urlencoded" if not
164
- already encoded or of another type
165
-
166
- Swapping middleware means giving the other priority. Specifying the
167
- "Content-Type" for the request is explicitly stating which middleware should
168
- process it.
169
-
170
- Examples:
171
-
172
- ```ruby
173
- # uploading a file:
174
- payload[:profile_pic] = Faraday::UploadIO.new('/path/to/avatar.jpg', 'image/jpeg')
175
-
176
- # "Multipart" middleware detects files and encodes with "multipart/form-data":
177
- conn.put '/profile', payload
178
- ```
179
-
180
- ## Writing middleware
181
-
182
- Middleware are classes that implement a `call` instance method. They hook into
183
- the request/response cycle.
184
-
185
- ```ruby
186
- def call(request_env)
187
- # do something with the request
188
- # request_env[:request_headers].merge!(...)
189
-
190
- @app.call(request_env).on_complete do |response_env|
191
- # do something with the response
192
- # response_env[:response_headers].merge!(...)
193
- end
194
- end
195
- ```
196
-
197
- It's important to do all processing of the response only in the `on_complete`
198
- block. This enables middleware to work in parallel mode where requests are
199
- asynchronous.
200
-
201
- The `env` is a hash with symbol keys that contains info about the request and,
202
- later, response. Some keys are:
203
-
204
- ```
205
- # request phase
206
- :method - :get, :post, ...
207
- :url - URI for the current request; also contains GET parameters
208
- :body - POST parameters for :post/:put requests
209
- :request_headers
210
-
211
- # response phase
212
- :status - HTTP response status code, such as 200
213
- :body - the response body
214
- :response_headers
215
- ```
216
-
217
- ## Ad-hoc adapters customization
218
-
219
- Faraday is intended to be a generic interface between your code and the adapter. However, sometimes you need to access a feature specific to one of the adapters that is not covered in Faraday's interface.
220
-
221
- When that happens, you can pass a block when specifying the adapter to customize it. The block parameter will change based on the adapter you're using. See below for some examples.
222
-
223
- ### NetHttp
224
- ```ruby
225
- conn = Faraday.new(...) do |f|
226
- f.adapter :net_http do |http| # yields Net::HTTP
227
- http.idle_timeout = 100
228
- http.verify_callback = lambda do | preverify_ok, cert_store |
229
- # do something here...
230
- end
231
- end
232
- end
233
- ```
234
-
235
- ### NetHttpPersistent
236
- ```ruby
237
- conn = Faraday.new(...) do |f|
238
- f.adapter :net_http_persistent do |http| # yields Net::HTTP::Persistent
239
- http.idle_timeout = 100
240
- http.retry_change_requests = true
241
- end
242
- end
243
- ```
244
-
245
- ### Patron
246
- ```ruby
247
- conn = Faraday.new(...) do |f|
248
- f.adapter :patron do |session| # yields Patron::Session
249
- session.max_redirects = 10
250
- end
251
- end
252
- ```
253
-
254
- ### HTTPClient
255
- ```ruby
256
- conn = Faraday.new(...) do |f|
257
- f.adapter :httpclient do |client| # yields HTTPClient
258
- client.keep_alive_timeout = 20
259
- client.ssl_config.timeout = 25
260
- end
261
- end
262
- ```
263
-
264
- ## Using Faraday for testing
265
-
266
- ```ruby
267
- # It's possible to define stubbed request outside a test adapter block.
268
- stubs = Faraday::Adapter::Test::Stubs.new do |stub|
269
- stub.get('/tamago') { |env| [200, {}, 'egg'] }
270
- end
271
-
272
- # You can pass stubbed request to the test adapter or define them in a block
273
- # or a combination of the two.
274
- test = Faraday.new do |builder|
275
- builder.adapter :test, stubs do |stub|
276
- stub.get('/ebi') { |env| [ 200, {}, 'shrimp' ]}
277
- end
278
- end
279
-
280
- # It's also possible to stub additional requests after the connection has
281
- # been initialized. This is useful for testing.
282
- stubs.get('/uni') { |env| [ 200, {}, 'urchin' ]}
283
-
284
- resp = test.get '/tamago'
285
- resp.body # => 'egg'
286
- resp = test.get '/ebi'
287
- resp.body # => 'shrimp'
288
- resp = test.get '/uni'
289
- resp.body # => 'urchin'
290
- resp = test.get '/else' #=> raises "no such stub" error
291
-
292
- # If you like, you can treat your stubs as mocks by verifying that all of
293
- # the stubbed calls were made. NOTE that this feature is still fairly
294
- # experimental: It will not verify the order or count of any stub, only that
295
- # it was called once during the course of the test.
296
- stubs.verify_stubbed_calls
297
- ```
298
-
299
- ## TODO
300
-
301
- * support streaming requests/responses
302
- * better stubbing API
14
+ The best starting point is the [Faraday Website][website], with its introduction and explanation.
15
+ Need more details? See the [Faraday API Documentation][apidoc] to see how it works internally.
303
16
 
304
17
  ## Supported Ruby versions
305
18
 
306
- This library aims to support and is [tested against][travis] the following Ruby
19
+ This library aims to support and is [tested against][actions] the following Ruby
307
20
  implementations:
308
21
 
309
- * Ruby 1.9.3+
310
- * [JRuby][] 1.7+
311
- * [Rubinius][] 2+
22
+ * Ruby 2.4+
312
23
 
313
24
  If something doesn't work on one of these Ruby versions, it's a bug.
314
25
 
315
26
  This library may inadvertently work (or seem to work) on other Ruby
316
- implementations, however support will only be provided for the versions listed
27
+ implementations and versions, however support will only be provided for the versions listed
317
28
  above.
318
29
 
319
30
  If you would like this library to support another Ruby version, you may
@@ -323,18 +34,20 @@ implementation, you will be responsible for providing patches in a timely
323
34
  fashion. If critical issues for a particular implementation exist at the time
324
35
  of a major release, support for that Ruby version may be dropped.
325
36
 
326
- ## Copyright
37
+ ## Contribute
327
38
 
328
- Copyright (c) 2009-2013 [Rick Olson](mailto:technoweenie@gmail.com), Zack Hobson.
329
- See [LICENSE][] for details.
39
+ Do you want to contribute to Faraday?
40
+ Open the issues page and check for the `help wanted` label!
41
+ But before you start coding, please read our [Contributing Guide][contributing]
42
+
43
+ ## Copyright
44
+ © 2009 - 2020, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
330
45
 
331
- [net_http]: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/Net/HTTP.html
332
- [persistent]: https://github.com/drbrain/net-http-persistent
333
- [travis]: https://travis-ci.org/lostisland/faraday
334
- [excon]: https://github.com/excon/excon#readme
335
- [patron]: http://toland.github.io/patron/
336
- [eventmachine]: https://github.com/igrigorik/em-http-request#readme
337
- [httpclient]: https://github.com/nahi/httpclient
46
+ [website]: https://lostisland.github.io/faraday
47
+ [faraday_team]: https://lostisland.github.io/faraday/team
48
+ [contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
49
+ [apidoc]: https://www.rubydoc.info/github/lostisland/faraday
50
+ [actions]: https://github.com/lostisland/faraday/actions
338
51
  [jruby]: http://jruby.org/
339
52
  [rubinius]: http://rubini.us/
340
53
  [license]: LICENSE.md
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Requires Ruby with rspec and faraday gems.
4
+ # rspec client_spec.rb
5
+
6
+ require 'faraday'
7
+ require 'json'
8
+
9
+ # Example API client
10
+ class Client
11
+ def initialize(conn)
12
+ @conn = conn
13
+ end
14
+
15
+ def sushi(jname)
16
+ res = @conn.get("/#{jname}")
17
+ data = JSON.parse(res.body)
18
+ data['name']
19
+ end
20
+ end
21
+
22
+ RSpec.describe Client do
23
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new }
24
+ let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
25
+ let(:client) { Client.new(conn) }
26
+
27
+ it 'parses name' do
28
+ stubs.get('/ebi') do |env|
29
+ # optional: you can inspect the Faraday::Env
30
+ expect(env.url.path).to eq('/ebi')
31
+ [
32
+ 200,
33
+ { 'Content-Type': 'application/javascript' },
34
+ '{"name": "shrimp"}'
35
+ ]
36
+ end
37
+
38
+ # uncomment to trigger stubs.verify_stubbed_calls failure
39
+ # stubs.get('/unused') { [404, {}, ''] }
40
+
41
+ expect(client.sushi('ebi')).to eq('shrimp')
42
+ stubs.verify_stubbed_calls
43
+ end
44
+
45
+ it 'handles 404' do
46
+ stubs.get('/ebi') do
47
+ [
48
+ 404,
49
+ { 'Content-Type': 'application/javascript' },
50
+ '{}'
51
+ ]
52
+ end
53
+ expect(client.sushi('ebi')).to be_nil
54
+ stubs.verify_stubbed_calls
55
+ end
56
+
57
+ it 'handles exception' do
58
+ stubs.get('/ebi') do
59
+ raise Faraday::ConnectionFailed, nil
60
+ end
61
+
62
+ expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
63
+ stubs.verify_stubbed_calls
64
+ end
65
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Requires Ruby with test-unit and faraday gems.
4
+ # ruby client_test.rb
5
+
6
+ require 'faraday'
7
+ require 'json'
8
+ require 'test/unit'
9
+
10
+ # Example API client
11
+ class Client
12
+ def initialize(conn)
13
+ @conn = conn
14
+ end
15
+
16
+ def sushi(jname)
17
+ res = @conn.get("/#{jname}")
18
+ data = JSON.parse(res.body)
19
+ data['name']
20
+ end
21
+ end
22
+
23
+ # Example API client test
24
+ class ClientTest < Test::Unit::TestCase
25
+ def test_sushi_name
26
+ stubs = Faraday::Adapter::Test::Stubs.new
27
+ stubs.get('/ebi') do |env|
28
+ # optional: you can inspect the Faraday::Env
29
+ assert_equal '/ebi', env.url.path
30
+ [
31
+ 200,
32
+ { 'Content-Type': 'application/javascript' },
33
+ '{"name": "shrimp"}'
34
+ ]
35
+ end
36
+
37
+ # uncomment to trigger stubs.verify_stubbed_calls failure
38
+ # stubs.get('/unused') { [404, {}, ''] }
39
+
40
+ cli = client(stubs)
41
+ assert_equal 'shrimp', cli.sushi('ebi')
42
+ stubs.verify_stubbed_calls
43
+ end
44
+
45
+ def test_sushi_404
46
+ stubs = Faraday::Adapter::Test::Stubs.new
47
+ stubs.get('/ebi') do
48
+ [
49
+ 404,
50
+ { 'Content-Type': 'application/javascript' },
51
+ '{}'
52
+ ]
53
+ end
54
+
55
+ cli = client(stubs)
56
+ assert_nil cli.sushi('ebi')
57
+ stubs.verify_stubbed_calls
58
+ end
59
+
60
+ def test_sushi_exception
61
+ stubs = Faraday::Adapter::Test::Stubs.new
62
+ stubs.get('/ebi') do
63
+ raise Faraday::ConnectionFailed, nil
64
+ end
65
+
66
+ cli = client(stubs)
67
+ assert_raise Faraday::ConnectionFailed do
68
+ cli.sushi('ebi')
69
+ end
70
+ stubs.verify_stubbed_calls
71
+ end
72
+
73
+ def client(stubs)
74
+ conn = Faraday.new do |builder|
75
+ builder.adapter :test, stubs
76
+ end
77
+ Client.new(conn)
78
+ end
79
+ end