faraday 1.9.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +193 -3
  3. data/README.md +11 -9
  4. data/examples/client_spec.rb +19 -19
  5. data/examples/client_test.rb +22 -22
  6. data/lib/faraday/adapter/test.rb +10 -4
  7. data/lib/faraday/adapter.rb +2 -5
  8. data/lib/faraday/connection.rb +12 -93
  9. data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
  10. data/lib/faraday/error.rb +3 -2
  11. data/lib/faraday/logging/formatter.rb +1 -0
  12. data/lib/faraday/middleware.rb +0 -1
  13. data/lib/faraday/middleware_registry.rb +17 -63
  14. data/lib/faraday/options.rb +3 -3
  15. data/lib/faraday/rack_builder.rb +23 -20
  16. data/lib/faraday/request/authorization.rb +28 -41
  17. data/lib/faraday/request/instrumentation.rb +2 -0
  18. data/lib/faraday/request/json.rb +55 -0
  19. data/lib/faraday/request/url_encoded.rb +2 -0
  20. data/lib/faraday/request.rb +11 -29
  21. data/lib/faraday/response/json.rb +54 -0
  22. data/lib/faraday/response/logger.rb +4 -4
  23. data/lib/faraday/response/raise_error.rb +9 -1
  24. data/lib/faraday/response.rb +8 -19
  25. data/lib/faraday/utils/headers.rb +1 -1
  26. data/lib/faraday/utils.rb +10 -5
  27. data/lib/faraday/version.rb +1 -1
  28. data/lib/faraday.rb +11 -45
  29. data/spec/faraday/connection_spec.rb +136 -85
  30. data/spec/faraday/middleware_registry_spec.rb +31 -0
  31. data/spec/faraday/options/env_spec.rb +2 -2
  32. data/spec/faraday/rack_builder_spec.rb +26 -54
  33. data/spec/faraday/request/authorization_spec.rb +19 -32
  34. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  35. data/spec/faraday/request/json_spec.rb +111 -0
  36. data/spec/faraday/request/url_encoded_spec.rb +0 -1
  37. data/spec/faraday/request_spec.rb +4 -15
  38. data/spec/faraday/response/json_spec.rb +117 -0
  39. data/spec/faraday/response/raise_error_spec.rb +7 -4
  40. data/spec/faraday/utils/headers_spec.rb +2 -2
  41. data/spec/faraday/utils_spec.rb +62 -1
  42. data/spec/support/fake_safe_buffer.rb +1 -1
  43. data/spec/support/helper_methods.rb +0 -37
  44. data/spec/support/shared_examples/adapter.rb +0 -1
  45. data/spec/support/shared_examples/request_method.rb +5 -18
  46. metadata +12 -149
  47. data/lib/faraday/adapter/typhoeus.rb +0 -15
  48. data/lib/faraday/autoload.rb +0 -87
  49. data/lib/faraday/dependency_loader.rb +0 -37
  50. data/lib/faraday/request/basic_authentication.rb +0 -20
  51. data/lib/faraday/request/token_authentication.rb +0 -20
  52. data/spec/faraday/adapter/em_http_spec.rb +0 -49
  53. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
  54. data/spec/faraday/adapter/excon_spec.rb +0 -49
  55. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  56. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  57. data/spec/faraday/adapter/patron_spec.rb +0 -18
  58. data/spec/faraday/adapter/rack_spec.rb +0 -8
  59. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  60. data/spec/faraday/composite_read_io_spec.rb +0 -80
  61. data/spec/faraday/response/middleware_spec.rb +0 -68
  62. data/spec/support/webmock_rack_app.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54fc3007a99cab5f45291408bfb7e2b1f6d243abcae060f5d485731328acb031
4
- data.tar.gz: 0fc0eb9ab470bca83b7b6fbc8cafcd9d141dd97eda575f63cf4b2bc6dcb36a65
3
+ metadata.gz: eaa5f5a3ea91b21a40ad7872a8eda558226e1d5f4d8f9875ecf9ece44e0181e4
4
+ data.tar.gz: 51af319bffd649b4b828cfed91b2a419211b1c2b171a3016949388b1a749c1ac
5
5
  SHA512:
6
- metadata.gz: ed9fee068b4cbbe13bacfa7eb01d02b919c0ffd72345a4449127f9ad11c7a7e585fa891c8d7791861fa91c8ebe47b9d7e78f760680eaf856db3c029c5f5cc1e0
7
- data.tar.gz: 47d4550ed834bdd5938312aafd9ea9ca4be061a12cd564fa83d08e7c67a44445a8870d12ef0030b6d9d84796813e97ff7ca859722ca786e449cfc25c66ebdb23
6
+ metadata.gz: b68ff300b45dbe3a7f85ab40f7a23c86b2c373a1672841a96230179f415da3f3bcf2123251235c4399575826043f7221fe81615171fbfd3af4d33a21410f8670
7
+ data.tar.gz: 41cd332afaf70ea6e306247b3f2ea9d9e38ed248e381aef798f914773b1dd7c7473ab9a6fcb5e03cb8e7163c0f81f26e813877bb5f7412964e55ddb9866ffe20
data/CHANGELOG.md CHANGED
@@ -1,9 +1,192 @@
1
1
  # Faraday Changelog
2
2
 
3
+ ## The changelog has moved!
4
+
5
+ This file is not being updated anymore. Instead, please check the [Releases](https://github.com/lostisland/faraday/releases) page.
6
+
7
+ ## [2.1.0](https://github.com/lostisland/faraday/compare/v2.0.1...v2.1.0) (2022-01-15)
8
+
9
+ * Fix test adapter thread safety by @iMacTia in [#1380](https://github.com/lostisland/faraday/pull/1380)
10
+ * Add default adapter options by @hirasawayuki in [#1382](https://github.com/lostisland/faraday/pull/1382)
11
+ * CI: Add Ruby 3.1 to matrix by @petergoldstein in [#1374](https://github.com/lostisland/faraday/pull/1374)
12
+ * docs: fix regex pattern in logger.md examples by @hirasawayuki in [#1378](https://github.com/lostisland/faraday/pull/1378)
13
+
14
+ ## [2.0.1](https://github.com/lostisland/faraday/compare/v2.0.0...v2.0.1) (2022-01-05)
15
+
16
+ * Re-add `faraday-net_http` as default adapter by @iMacTia in [#1366](https://github.com/lostisland/faraday/pull/1366)
17
+ * Updated sample format in UPGRADING.md by @vimutter in [#1361](https://github.com/lostisland/faraday/pull/1361)
18
+ * docs: Make UPGRADING examples more copyable by @olleolleolle in [#1363](https://github.com/lostisland/faraday/pull/1363)
19
+
20
+ ## [2.0.0](https://github.com/lostisland/faraday/compare/v1.8.0...v2.0.0) (2022-01-04)
21
+
22
+ The next major release is here, and it comes almost 2 years after the release of v1.0!
23
+
24
+ This release changes the way you use Faraday and embraces a new paradigm of Faraday as an ecosystem, rather than a library.
25
+
26
+ What does that mean? It means that Faraday is less of a bundled tool and more of a framework for the community to build on top of.
27
+
28
+ As a result, all adapters and some middleware have moved out and are now shipped as standalone gems 🙌!
29
+
30
+ But this doesn't mean that upgrading from Faraday 1.x to Faraday 2.0 should be hard, in fact we've listed everything you need to do in the [UPGRADING.md](https://github.com/lostisland/faraday/blob/main/UPGRADING.md) doc.
31
+
32
+ Moreover, we've setup a new [awesome-faraday](https://github.com/lostisland/awesome-faraday) repository that will showcase a curated list of adapters and middleware 😎.
33
+
34
+ This release was the result of the efforts of the core team and all the contributors, new and old, that have helped achieve this milestone 👏.
35
+
36
+ ## What's Changed
37
+
38
+ * Autoloading, dependency loading and middleware registry cleanup by @iMacTia in [#1301](https://github.com/lostisland/faraday/pull/1301)
39
+ * Move JSON middleware (request and response) from faraday_middleware by @iMacTia in [#1300](https://github.com/lostisland/faraday/pull/1300)
40
+ * Remove deprecated `Faraday::Request#method` by @olleolleolle in [#1303](https://github.com/lostisland/faraday/pull/1303)
41
+ * Remove deprecated `Faraday::UploadIO` by @iMacTia in [#1307](https://github.com/lostisland/faraday/pull/1307)
42
+ * [1.x] Deprecate Authorization helpers in `Faraday::Connection` by @iMacTia in [#1306](https://github.com/lostisland/faraday/pull/1306)
43
+ * Drop deprecated auth helpers from Connection and refactor auth middleware by @iMacTia in [#1308](https://github.com/lostisland/faraday/pull/1308)
44
+ * Add Faraday 1.x examples in authentication.md docs by @iMacTia in [#1320](https://github.com/lostisland/faraday/pull/1320)
45
+ * Fix passing a URL with embedded basic auth by @iMacTia in [#1324](https://github.com/lostisland/faraday/pull/1324)
46
+ * Register JSON middleware by @mollerhoj in [#1331](https://github.com/lostisland/faraday/pull/1331)
47
+ * Retry middleware should handle string exception class name consistently by @jrochkind in [#1334](https://github.com/lostisland/faraday/pull/1334)
48
+ * Improve request info in exceptions raised by RaiseError Middleware by @willianzocolau in [#1335](https://github.com/lostisland/faraday/pull/1335)
49
+ * Remove net-http adapter and update docs by @iMacTia in [#1336](https://github.com/lostisland/faraday/pull/1336)
50
+ * Explain plan for faraday_middleware in UPGRADING.md by @iMacTia in [#1339](https://github.com/lostisland/faraday/pull/1339)
51
+ * Scripts folder cleanup by @iMacTia in [#1340](https://github.com/lostisland/faraday/pull/1340)
52
+ * Replace `Hash#merge` with `Utils#deep_merge` for connection options by @xkwd in [#1343](https://github.com/lostisland/faraday/pull/1343)
53
+ * Callable authorizers by @sled in [#1345](https://github.com/lostisland/faraday/pull/1345)
54
+ * Default value for exc error by @DariuszMusielak in [#1351](https://github.com/lostisland/faraday/pull/1351)
55
+ * Don't call `retry_block` unless a retry is going to happen by @jrochkind in [#1350](https://github.com/lostisland/faraday/pull/1350)
56
+ * Improve documentation for v2 by @iMacTia in [#1353](https://github.com/lostisland/faraday/pull/1353)
57
+ * Remove default `default_adapter` (yes, you read that right) by @iMacTia in [#1354](https://github.com/lostisland/faraday/pull/1354)
58
+ * Remove retry middleware by @iMacTia in [#1356](https://github.com/lostisland/faraday/pull/1356)
59
+ * Remove multipart middleware and all its documentation and tests by @iMacTia in [#1357](https://github.com/lostisland/faraday/pull/1357)
60
+
61
+ ## [1.9.3](https://github.com/lostisland/faraday/compare/v1.9.2...v1.9.3) (2022-01-06)
62
+
63
+ * Re-add support for Ruby 2.4+ by @iMacTia in [#1371](https://github.com/lostisland/faraday/pull/1371)
64
+
65
+ ## [1.9.2](https://github.com/lostisland/faraday/compare/v1.9.1...v1.9.2) (2022-01-06)
66
+
67
+ * Add alias with legacy name to gemified middleware by @iMacTia in [#1372](https://github.com/lostisland/faraday/pull/1372)
68
+
69
+ ## [1.9.1](https://github.com/lostisland/faraday/compare/v1.9.0...v1.9.1) (2022-01-06)
70
+
71
+ * Update adapter dependencies in Gemspec by @iMacTia in [#1370](https://github.com/lostisland/faraday/pull/1370)
72
+
73
+ ## [1.9.0](https://github.com/lostisland/faraday/compare/v1.8.0...v1.9.0) (2022-01-06)
74
+
75
+ * Use external multipart and retry middleware by @iMacTia in [#1367](https://github.com/lostisland/faraday/pull/1367)
76
+
77
+ ## [1.8.0](https://github.com/lostisland/faraday/releases/tag/v1.8.0) (2021-09-18)
78
+
79
+ ### Features
80
+
81
+ * Backport authorization procs (#1322, @jarl-dk)
82
+
83
+ ## [v1.7.0](https://github.com/lostisland/faraday/releases/tag/v1.7.0) (2021-08-09)
84
+
85
+ ### Features
86
+
87
+ * Add strict_mode to Test::Stubs (#1298, @yykamei)
88
+
89
+ ## [v1.6.0](https://github.com/lostisland/faraday/releases/tag/v1.6.0) (2021-08-01)
90
+
91
+ ### Misc
92
+
93
+ * Use external Rack adapter (#1296, @iMacTia)
94
+
95
+ ## [v1.5.1](https://github.com/lostisland/faraday/releases/tag/v1.5.1) (2021-07-11)
96
+
97
+ ### Fixes
98
+
99
+ * Fix JRuby incompatibility after moving out EM adapters (#1294, @ahorek)
100
+
101
+ ### Documentation
102
+
103
+ * Update YARD to follow RackBuilder (#1292, @kachick)
104
+
105
+ ## [v1.5.0](https://github.com/lostisland/faraday/releases/tag/v1.5.0) (2021-07-04)
106
+
107
+ ### Misc
108
+
109
+ * Use external httpclient adapter (#1289, @iMacTia)
110
+ * Use external patron adapter (#1290, @iMacTia)
111
+
112
+ ## [v1.4.3](https://github.com/lostisland/faraday/releases/tag/v1.4.3) (2021-06-24)
113
+
114
+ ### Fixes
115
+
116
+ * Silence warning (#1286, @gurgeous)
117
+ * Always dup url_prefix in Connection#build_exclusive_url (#1288, @alexeyds)
118
+
119
+ ## [v1.4.2](https://github.com/lostisland/faraday/releases/tag/v1.4.2) (2021-05-22)
120
+
121
+ ### Fixes
122
+ * Add proxy setting when url_prefix is changed (#1276, @ci)
123
+ * Default proxy scheme to http:// if necessary, fixes #1282 (#1283, @gurgeous)
124
+
125
+ ### Documentation
126
+ * Improve introduction page (#1273, @gurgeous)
127
+ * Docs: add more middleware examples (#1277, @gurgeous)
128
+
129
+ ### Misc
130
+ * Use external `em_http` and `em_synchrony` adapters (#1274, @iMacTia)
131
+
132
+ ## [v1.4.1](https://github.com/lostisland/faraday/releases/tag/v1.4.1) (2021-04-18)
133
+
134
+ ### Fixes
135
+
136
+ * Fix dependencies from external adapter gems (#1269, @iMacTia)
137
+
138
+ ## [v1.4.0](https://github.com/lostisland/faraday/releases/tag/v1.4.0) (2021-04-16)
139
+
140
+ ### Highlights
141
+
142
+ With this release, we continue the work of gradually moving out adapters into their own gems 🎉
143
+ Thanks to @MikeRogers0 for helping the Faraday team in progressing with this quest 👏
144
+
145
+ And thanks to @olleolleolle efforts, Faraday is becoming more inclusive than ever 🤗
146
+ Faraday's `master` branch has been renamed into `main`, we have an official policy on inclusive language and even a rubocop plugin to check for non-inclusive words ❤️!
147
+ Checkout the "Misc" section below for more details 🙌 !
148
+
149
+ ### Fixes
150
+
151
+ * Fix NoMethodError undefined method 'coverage' (#1255, @Maroo-b)
152
+
153
+ ### Documentation
154
+
155
+ * Some docs on EventMachine adapters. (#1232, @damau)
156
+ * CONTRIBUTING: Fix grammar and layout (#1261, @olleolleolle)
157
+
158
+ ### Misc
159
+
160
+ * Replacing Net::HTTP::Persistent with faraday-net_http_persistent (#1250, @MikeRogers0)
161
+ * CI: Configure the regenerated Coveralls token (#1256, @olleolleolle)
162
+ * Replace Excon adapter with Faraday::Excon gem, and fix autoloading issue with Faraday::NetHttpPersistent (#1257, @iMacTia)
163
+ * Drop CodeClimate (#1259, @olleolleolle)
164
+ * CI: Rename default branch to main (#1263, @olleolleolle)
165
+ * Drop RDoc support file .document (#1264, @olleolleolle, @iMacTia)
166
+ * CONTRIBUTING: add a policy on inclusive language (#1262, @olleolleolle)
167
+ * Add rubocop-inclusivity (#1267, @olleolleolle, @iMacTia)
168
+
169
+ ## [v1.3.1](https://github.com/lostisland/faraday/releases/tag/v1.3.1) (2021-04-16)
170
+
171
+ ### Fixes
172
+
173
+ * Escape colon in path segment (#1237, @yarafan)
174
+ * Handle IPv6 address String on Faraday::Connection#proxy_from_env (#1252, @cosmo0920)
175
+
176
+ ### Documentation
177
+
178
+ * Fix broken Rubydoc.info links (#1236, @nickcampbell18)
179
+ * Add httpx to list of external adapters (#1246, @HoneyryderChuck)
180
+
181
+ ### Misc
182
+
183
+ * Refactor CI to remove duplicated line (#1230, @tricknotes)
184
+ * Gemspec: Pick a good ruby2_keywords release (#1241, @olleolleolle)
185
+
3
186
  ## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)
4
187
 
5
188
  ### Highlights
6
- Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!
189
+ Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!
7
190
 
8
191
  This is also the first release with a previously "included" adapter (Net::HTTP) being isolated into a [separate gem](https://github.com/lostisland/faraday-net_http) 🎊!
9
192
  The new adapter is added to Faraday as a dependency for now, so that means full backwards-compatibility, but just to be safe be careful when upgrading!
@@ -94,7 +277,7 @@ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS
94
277
  * Allows `parse` method to be private/protected in response middleware (#1123)
95
278
  * Encode Spaces in Query Strings as '%20' Instead of '+' (#1125)
96
279
  * Limits rack to v2.0.x (#1127)
97
- * Adapter Registry reads also use mutex (#1136)
280
+ * Adapter Registry reads also use mutex (#1136)
98
281
 
99
282
  ### Documentation
100
283
 
@@ -104,7 +287,7 @@ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS
104
287
  * Website: add search bar (#1116)
105
288
  * Fix request/response mix-up in docs text (#1132)
106
289
 
107
- ## v1.0
290
+ ## [v1.0](https://github.com/lostisland/faraday/releases/tag/v1.0.0) (2020-01-22)
108
291
 
109
292
  Features:
110
293
 
@@ -148,6 +331,13 @@ Misc:
148
331
  * Describe clearing cached stubs #1045 (@viraptor)
149
332
  * Add project metadata to the gemspec #1046 (@orien)
150
333
 
334
+ ## v0.17.4
335
+
336
+ Fixes:
337
+
338
+ * NetHttp adapter: wrap Errno::EADDRNOTAVAIL (#1114, @embs)
339
+ * Fix === for subclasses of deprecated classes (#1243, @mervync)
340
+
151
341
  ## v0.17.3
152
342
 
153
343
  Fixes:
data/README.md CHANGED
@@ -2,12 +2,13 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/faraday.svg)](https://rubygems.org/gems/faraday)
4
4
  [![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
5
- [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
5
+ [![GitHub Discussions](https://img.shields.io/github/discussions/lostisland/faraday?logo=github)](https://github.com/lostisland/faraday/discussions)
6
6
 
7
7
 
8
- Faraday is an HTTP client library that provides a common interface over many
9
- adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
10
- processing the request/response cycle.
8
+ Faraday is an HTTP client library abstraction layer that provides a common interface over many
9
+ adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle.
10
+ You probably don't want to use Faraday directly in your project, as it will lack an actual client library to perform
11
+ requests. Instead, you probably want to have a look at [Awesome Faraday][awesome] for a list of available adapters.
11
12
 
12
13
  ## Getting Started
13
14
 
@@ -16,10 +17,10 @@ Need more details? See the [Faraday API Documentation][apidoc] to see how it wor
16
17
 
17
18
  ## Supported Ruby versions
18
19
 
19
- This library aims to support and is [tested against][actions] the following Ruby
20
- implementations:
21
-
22
- * Ruby 2.4+
20
+ This library aims to support and is [tested against][actions] the currently officially supported Ruby
21
+ implementations. This means that, even without a major release, we could add or drop support for Ruby versions,
22
+ following their [EOL](https://endoflife.date/ruby).
23
+ Currently that means we support Ruby 2.6+
23
24
 
24
25
  If something doesn't work on one of these Ruby versions, it's a bug.
25
26
 
@@ -41,8 +42,9 @@ Open the issues page and check for the `help wanted` label!
41
42
  But before you start coding, please read our [Contributing Guide][contributing]
42
43
 
43
44
  ## Copyright
44
- © 2009 - 2020, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
45
+ © 2009 - 2021, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
45
46
 
47
+ [awesome]: https://github.com/lostisland/awesome-faraday/#adapters
46
48
  [website]: https://lostisland.github.io/faraday
47
49
  [faraday_team]: https://lostisland.github.io/faraday/team
48
50
  [contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
@@ -12,10 +12,10 @@ class Client
12
12
  @conn = conn
13
13
  end
14
14
 
15
- def sushi(jname, params: {})
15
+ def httpbingo(jname, params: {})
16
16
  res = @conn.get("/#{jname}", params)
17
17
  data = JSON.parse(res.body)
18
- data['name']
18
+ data['origin']
19
19
  end
20
20
  end
21
21
 
@@ -24,42 +24,42 @@ RSpec.describe Client do
24
24
  let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
25
25
  let(:client) { Client.new(conn) }
26
26
 
27
- it 'parses name' do
28
- stubs.get('/ebi') do |env|
27
+ it 'parses origin' do
28
+ stubs.get('/ip') do |env|
29
29
  # optional: you can inspect the Faraday::Env
30
- expect(env.url.path).to eq('/ebi')
30
+ expect(env.url.path).to eq('/ip')
31
31
  [
32
32
  200,
33
33
  { 'Content-Type': 'application/javascript' },
34
- '{"name": "shrimp"}'
34
+ '{"origin": "127.0.0.1"}'
35
35
  ]
36
36
  end
37
37
 
38
38
  # uncomment to trigger stubs.verify_stubbed_calls failure
39
39
  # stubs.get('/unused') { [404, {}, ''] }
40
40
 
41
- expect(client.sushi('ebi')).to eq('shrimp')
41
+ expect(client.httpbingo('ip')).to eq('127.0.0.1')
42
42
  stubs.verify_stubbed_calls
43
43
  end
44
44
 
45
45
  it 'handles 404' do
46
- stubs.get('/ebi') do
46
+ stubs.get('/api') do
47
47
  [
48
48
  404,
49
49
  { 'Content-Type': 'application/javascript' },
50
50
  '{}'
51
51
  ]
52
52
  end
53
- expect(client.sushi('ebi')).to be_nil
53
+ expect(client.httpbingo('api')).to be_nil
54
54
  stubs.verify_stubbed_calls
55
55
  end
56
56
 
57
57
  it 'handles exception' do
58
- stubs.get('/ebi') do
59
- raise Faraday::ConnectionFailed, nil
58
+ stubs.get('/api') do
59
+ raise Faraday::ConnectionFailed
60
60
  end
61
61
 
62
- expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
62
+ expect { client.httpbingo('api') }.to raise_error(Faraday::ConnectionFailed)
63
63
  stubs.verify_stubbed_calls
64
64
  end
65
65
 
@@ -67,17 +67,17 @@ RSpec.describe Client do
67
67
  let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }
68
68
 
69
69
  it 'verifies the all parameter values are identical' do
70
- stubs.get('/ebi?abc=123') do
70
+ stubs.get('/api?abc=123') do
71
71
  [
72
72
  200,
73
73
  { 'Content-Type': 'application/javascript' },
74
- '{"name": "shrimp"}'
74
+ '{"origin": "127.0.0.1"}'
75
75
  ]
76
76
  end
77
77
 
78
78
  # uncomment to raise Stubs::NotFound
79
- # expect(client.sushi('ebi', params: { abc: 123, foo: 'Kappa' })).to eq('shrimp')
80
- expect(client.sushi('ebi', params: { abc: 123 })).to eq('shrimp')
79
+ # expect(client.httpbingo('api', params: { abc: 123, foo: 'Kappa' })).to eq('127.0.0.1')
80
+ expect(client.httpbingo('api', params: { abc: 123 })).to eq('127.0.0.1')
81
81
  stubs.verify_stubbed_calls
82
82
  end
83
83
  end
@@ -86,11 +86,11 @@ RSpec.describe Client do
86
86
  let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }
87
87
 
88
88
  it 'handles the same multiple URL parameters' do
89
- stubs.get('/ebi?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"name": "shrimp"}'] }
89
+ stubs.get('/api?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"origin": "127.0.0.1"}'] }
90
90
 
91
91
  # uncomment to raise Stubs::NotFound
92
- # expect(client.sushi('ebi', params: { a: %w[x y] })).to eq('shrimp')
93
- expect(client.sushi('ebi', params: { a: %w[x y z] })).to eq('shrimp')
92
+ # expect(client.httpbingo('api', params: { a: %w[x y] })).to eq('127.0.0.1')
93
+ expect(client.httpbingo('api', params: { a: %w[x y z] })).to eq('127.0.0.1')
94
94
  stubs.verify_stubbed_calls
95
95
  end
96
96
  end
@@ -13,24 +13,24 @@ class Client
13
13
  @conn = conn
14
14
  end
15
15
 
16
- def sushi(jname, params: {})
16
+ def httpbingo(jname, params: {})
17
17
  res = @conn.get("/#{jname}", params)
18
18
  data = JSON.parse(res.body)
19
- data['name']
19
+ data['origin']
20
20
  end
21
21
  end
22
22
 
23
23
  # Example API client test
24
24
  class ClientTest < Test::Unit::TestCase
25
- def test_sushi_name
25
+ def test_httpbingo_name
26
26
  stubs = Faraday::Adapter::Test::Stubs.new
27
- stubs.get('/ebi') do |env|
27
+ stubs.get('/api') do |env|
28
28
  # optional: you can inspect the Faraday::Env
29
- assert_equal '/ebi', env.url.path
29
+ assert_equal '/api', env.url.path
30
30
  [
31
31
  200,
32
32
  { 'Content-Type': 'application/javascript' },
33
- '{"name": "shrimp"}'
33
+ '{"origin": "127.0.0.1"}'
34
34
  ]
35
35
  end
36
36
 
@@ -38,13 +38,13 @@ class ClientTest < Test::Unit::TestCase
38
38
  # stubs.get('/unused') { [404, {}, ''] }
39
39
 
40
40
  cli = client(stubs)
41
- assert_equal 'shrimp', cli.sushi('ebi')
41
+ assert_equal '127.0.0.1', cli.httpbingo('api')
42
42
  stubs.verify_stubbed_calls
43
43
  end
44
44
 
45
- def test_sushi_404
45
+ def test_httpbingo_not_found
46
46
  stubs = Faraday::Adapter::Test::Stubs.new
47
- stubs.get('/ebi') do
47
+ stubs.get('/api') do
48
48
  [
49
49
  404,
50
50
  { 'Content-Type': 'application/javascript' },
@@ -53,48 +53,48 @@ class ClientTest < Test::Unit::TestCase
53
53
  end
54
54
 
55
55
  cli = client(stubs)
56
- assert_nil cli.sushi('ebi')
56
+ assert_nil cli.httpbingo('api')
57
57
  stubs.verify_stubbed_calls
58
58
  end
59
59
 
60
- def test_sushi_exception
60
+ def test_httpbingo_exception
61
61
  stubs = Faraday::Adapter::Test::Stubs.new
62
- stubs.get('/ebi') do
63
- raise Faraday::ConnectionFailed, nil
62
+ stubs.get('/api') do
63
+ raise Faraday::ConnectionFailed
64
64
  end
65
65
 
66
66
  cli = client(stubs)
67
67
  assert_raise Faraday::ConnectionFailed do
68
- cli.sushi('ebi')
68
+ cli.httpbingo('api')
69
69
  end
70
70
  stubs.verify_stubbed_calls
71
71
  end
72
72
 
73
73
  def test_strict_mode
74
74
  stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
75
- stubs.get('/ebi?abc=123') do
75
+ stubs.get('/api?abc=123') do
76
76
  [
77
77
  200,
78
78
  { 'Content-Type': 'application/javascript' },
79
- '{"name": "shrimp"}'
79
+ '{"origin": "127.0.0.1"}'
80
80
  ]
81
81
  end
82
82
 
83
83
  cli = client(stubs)
84
- assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
84
+ assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123 })
85
85
 
86
86
  # uncomment to raise Stubs::NotFound
87
- # assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
87
+ # assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123, foo: 'Kappa' })
88
88
  stubs.verify_stubbed_calls
89
89
  end
90
90
 
91
91
  def test_non_default_params_encoder
92
92
  stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
93
- stubs.get('/ebi?a=x&a=y&a=z') do
93
+ stubs.get('/api?a=x&a=y&a=z') do
94
94
  [
95
95
  200,
96
96
  { 'Content-Type': 'application/javascript' },
97
- '{"name": "shrimp"}'
97
+ '{"origin": "127.0.0.1"}'
98
98
  ]
99
99
  end
100
100
  conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
@@ -102,10 +102,10 @@ class ClientTest < Test::Unit::TestCase
102
102
  end
103
103
 
104
104
  cli = Client.new(conn)
105
- assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
105
+ assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y z] })
106
106
 
107
107
  # uncomment to raise Stubs::NotFound
108
- # assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
108
+ # assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y] })
109
109
  stubs.verify_stubbed_calls
110
110
  end
111
111
 
@@ -55,6 +55,7 @@ module Faraday
55
55
  @stack = {}
56
56
  @consumed = {}
57
57
  @strict_mode = strict_mode
58
+ @stubs_mutex = Monitor.new
58
59
  yield(self) if block_given?
59
60
  end
60
61
 
@@ -70,10 +71,13 @@ module Faraday
70
71
  stack = @stack[request_method]
71
72
  consumed = (@consumed[request_method] ||= [])
72
73
 
73
- stub, meta = matches?(stack, env)
74
- if stub
75
- consumed << stack.delete(stub)
76
- return stub, meta
74
+ @stubs_mutex.synchronize do
75
+ stub, meta = matches?(stack, env)
76
+ if stub
77
+ removed = stack.delete(stub)
78
+ consumed << removed unless removed.nil?
79
+ return stub, meta
80
+ end
77
81
  end
78
82
  matches?(consumed, env)
79
83
  end
@@ -260,3 +264,5 @@ module Faraday
260
264
  end
261
265
  end
262
266
  end
267
+
268
+ Faraday::Adapter.register_middleware(test: Faraday::Adapter::Test)
@@ -5,14 +5,9 @@ module Faraday
5
5
  # responsible for fulfilling a Faraday request.
6
6
  class Adapter
7
7
  extend MiddlewareRegistry
8
- extend DependencyLoader
9
8
 
10
9
  CONTENT_LENGTH = 'Content-Length'
11
10
 
12
- register_middleware File.expand_path('adapter', __dir__),
13
- test: [:Test, 'test'],
14
- typhoeus: [:Typhoeus, 'typhoeus']
15
-
16
11
  # This module marks an Adapter as supporting parallel requests.
17
12
  module Parallelism
18
13
  attr_writer :supports_parallel
@@ -103,3 +98,5 @@ module Faraday
103
98
  }.freeze
104
99
  end
105
100
  end
101
+
102
+ require 'faraday/adapter/test'