faraday 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +221 -1
  3. data/LICENSE.md +1 -1
  4. data/README.md +19 -14
  5. data/examples/client_spec.rb +36 -4
  6. data/examples/client_test.rb +43 -4
  7. data/lib/faraday/adapter/test.rb +61 -43
  8. data/lib/faraday/adapter.rb +3 -16
  9. data/lib/faraday/adapter_registry.rb +3 -1
  10. data/lib/faraday/connection.rb +25 -78
  11. data/lib/faraday/encoders/flat_params_encoder.rb +9 -2
  12. data/lib/faraday/encoders/nested_params_encoder.rb +9 -4
  13. data/lib/faraday/error.rb +23 -8
  14. data/lib/faraday/logging/formatter.rb +1 -0
  15. data/lib/faraday/methods.rb +6 -0
  16. data/lib/faraday/middleware.rb +14 -5
  17. data/lib/faraday/middleware_registry.rb +15 -79
  18. data/lib/faraday/options/proxy_options.rb +4 -0
  19. data/lib/faraday/options.rb +7 -11
  20. data/lib/faraday/rack_builder.rb +34 -30
  21. data/lib/faraday/request/authorization.rb +32 -36
  22. data/lib/faraday/request/instrumentation.rb +2 -0
  23. data/lib/faraday/request/json.rb +55 -0
  24. data/lib/faraday/request/url_encoded.rb +5 -1
  25. data/lib/faraday/request.rb +13 -23
  26. data/lib/faraday/response/json.rb +54 -0
  27. data/lib/faraday/response/logger.rb +4 -4
  28. data/lib/faraday/response/raise_error.rb +20 -1
  29. data/lib/faraday/response.rb +8 -22
  30. data/lib/faraday/utils/headers.rb +3 -3
  31. data/lib/faraday/utils.rb +21 -8
  32. data/lib/faraday/version.rb +5 -0
  33. data/lib/faraday.rb +44 -59
  34. data/spec/faraday/adapter/test_spec.rb +377 -0
  35. data/spec/faraday/connection_spec.rb +147 -51
  36. data/spec/faraday/error_spec.rb +15 -0
  37. data/spec/faraday/middleware_spec.rb +32 -6
  38. data/spec/faraday/options/env_spec.rb +2 -2
  39. data/spec/faraday/options/proxy_options_spec.rb +7 -0
  40. data/spec/faraday/params_encoders/flat_spec.rb +8 -0
  41. data/spec/faraday/params_encoders/nested_spec.rb +8 -0
  42. data/spec/faraday/rack_builder_spec.rb +144 -38
  43. data/spec/faraday/request/authorization_spec.rb +19 -24
  44. data/spec/faraday/request/instrumentation_spec.rb +5 -7
  45. data/spec/faraday/request/json_spec.rb +111 -0
  46. data/spec/faraday/request/url_encoded_spec.rb +13 -1
  47. data/spec/faraday/request_spec.rb +6 -6
  48. data/spec/faraday/response/json_spec.rb +117 -0
  49. data/spec/faraday/response/raise_error_spec.rb +66 -0
  50. data/spec/faraday/utils_spec.rb +62 -1
  51. data/spec/support/fake_safe_buffer.rb +1 -1
  52. data/spec/support/helper_methods.rb +0 -37
  53. data/spec/support/shared_examples/adapter.rb +2 -2
  54. data/spec/support/shared_examples/request_method.rb +43 -28
  55. metadata +16 -48
  56. data/UPGRADING.md +0 -55
  57. data/lib/faraday/adapter/em_http.rb +0 -285
  58. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -62
  59. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -69
  60. data/lib/faraday/adapter/em_synchrony.rb +0 -150
  61. data/lib/faraday/adapter/excon.rb +0 -124
  62. data/lib/faraday/adapter/httpclient.rb +0 -151
  63. data/lib/faraday/adapter/net_http.rb +0 -209
  64. data/lib/faraday/adapter/net_http_persistent.rb +0 -91
  65. data/lib/faraday/adapter/patron.rb +0 -132
  66. data/lib/faraday/adapter/rack.rb +0 -75
  67. data/lib/faraday/adapter/typhoeus.rb +0 -15
  68. data/lib/faraday/autoload.rb +0 -95
  69. data/lib/faraday/dependency_loader.rb +0 -37
  70. data/lib/faraday/file_part.rb +0 -128
  71. data/lib/faraday/param_part.rb +0 -53
  72. data/lib/faraday/request/basic_authentication.rb +0 -20
  73. data/lib/faraday/request/multipart.rb +0 -99
  74. data/lib/faraday/request/retry.rb +0 -239
  75. data/lib/faraday/request/token_authentication.rb +0 -20
  76. data/spec/faraday/adapter/em_http_spec.rb +0 -47
  77. data/spec/faraday/adapter/em_synchrony_spec.rb +0 -16
  78. data/spec/faraday/adapter/excon_spec.rb +0 -49
  79. data/spec/faraday/adapter/httpclient_spec.rb +0 -73
  80. data/spec/faraday/adapter/net_http_persistent_spec.rb +0 -57
  81. data/spec/faraday/adapter/net_http_spec.rb +0 -64
  82. data/spec/faraday/adapter/patron_spec.rb +0 -18
  83. data/spec/faraday/adapter/rack_spec.rb +0 -8
  84. data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
  85. data/spec/faraday/composite_read_io_spec.rb +0 -80
  86. data/spec/faraday/request/multipart_spec.rb +0 -274
  87. data/spec/faraday/request/retry_spec.rb +0 -242
  88. data/spec/faraday/response/middleware_spec.rb +0 -52
  89. data/spec/support/webmock_rack_app.rb +0 -68
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9d3d650c5d465a0862cddd7ab2ed27fbfb4a6e5f2cfcbb54ba753bcc1ce9e40e
4
- data.tar.gz: 953336deeb3dd2e0228368209bdffc7815b376772269c439e21374bc22b70484
3
+ metadata.gz: 367d509be94da297eac6ece76f5561772218a5982696eba047ef8599d0ff05e1
4
+ data.tar.gz: 779c66227b778b17661428c17ab65567e23f13ea2824b92251eba67cd32c6280
5
5
  SHA512:
6
- metadata.gz: b80c084a8714d995dec61c918891a63720dbcdddf2eb3ecb7106f40a03bb341c4f03ffe976365c5450b2dcffd0b1f52c1cd4320ea24591dfaef7d5fdbbe12562
7
- data.tar.gz: 67f8134fae885d3bb2e621afa701d6d535409bbb0141ad699f4d290059d61bf856915f622e214251f05e8b893a69bf68439118074712a22e75e3705b249dbb85
6
+ metadata.gz: 73d405ce87ac4a0917806b16ab11693b68966334656689a95bc06764501c44965832b08fa7bf36beeb71c2144a6661775df4f22b18e8281f664a0605fc2ece49
7
+ data.tar.gz: 22701db3eb1bb1668aaf6429e711acad05099577b39ccc792d32c42a82043f628483973a993c5ce91acd4207a9f0a01f47a51d6bffc4212047e4150fbb63e1e1
data/CHANGELOG.md CHANGED
@@ -1,6 +1,219 @@
1
1
  # Faraday Changelog
2
2
 
3
- ## v1.0
3
+ ## [1.8.0](https://github.com/lostisland/faraday/releases/tag/v1.8.0) (2021-09-18)
4
+
5
+ ### Features
6
+
7
+ * Backport authorization procs (#1322, @jarl-dk)
8
+
9
+ ## [v1.7.0](https://github.com/lostisland/faraday/releases/tag/v1.7.0) (2021-08-09)
10
+
11
+ ### Features
12
+
13
+ * Add strict_mode to Test::Stubs (#1298, @yykamei)
14
+
15
+ ## [v1.6.0](https://github.com/lostisland/faraday/releases/tag/v1.6.0) (2021-08-01)
16
+
17
+ ### Misc
18
+
19
+ * Use external Rack adapter (#1296, @iMacTia)
20
+
21
+ ## [v1.5.1](https://github.com/lostisland/faraday/releases/tag/v1.5.1) (2021-07-11)
22
+
23
+ ### Fixes
24
+
25
+ * Fix JRuby incompatibility after moving out EM adapters (#1294, @ahorek)
26
+
27
+ ### Documentation
28
+
29
+ * Update YARD to follow RackBuilder (#1292, @kachick)
30
+
31
+ ## [v1.5.0](https://github.com/lostisland/faraday/releases/tag/v1.5.0) (2021-07-04)
32
+
33
+ ### Misc
34
+
35
+ * Use external httpclient adapter (#1289, @iMacTia)
36
+ * Use external patron adapter (#1290, @iMacTia)
37
+
38
+ ## [v1.4.3](https://github.com/lostisland/faraday/releases/tag/v1.4.3) (2021-06-24)
39
+
40
+ ### Fixes
41
+
42
+ * Silence warning (#1286, @gurgeous)
43
+ * Always dup url_prefix in Connection#build_exclusive_url (#1288, @alexeyds)
44
+
45
+ ## [v1.4.2](https://github.com/lostisland/faraday/releases/tag/v1.4.2) (2021-05-22)
46
+
47
+ ### Fixes
48
+ * Add proxy setting when url_prefix is changed (#1276, @ci)
49
+ * Default proxy scheme to http:// if necessary, fixes #1282 (#1283, @gurgeous)
50
+
51
+ ### Documentation
52
+ * Improve introduction page (#1273, @gurgeous)
53
+ * Docs: add more middleware examples (#1277, @gurgeous)
54
+
55
+ ### Misc
56
+ * Use external `em_http` and `em_synchrony` adapters (#1274, @iMacTia)
57
+
58
+ ## [v1.4.1](https://github.com/lostisland/faraday/releases/tag/v1.4.1) (2021-04-18)
59
+
60
+ ### Fixes
61
+
62
+ * Fix dependencies from external adapter gems (#1269, @iMacTia)
63
+
64
+ ## [v1.4.0](https://github.com/lostisland/faraday/releases/tag/v1.4.0) (2021-04-16)
65
+
66
+ ### Highlights
67
+
68
+ With this release, we continue the work of gradually moving out adapters into their own gems 🎉
69
+ Thanks to @MikeRogers0 for helping the Faraday team in progressing with this quest 👏
70
+
71
+ And thanks to @olleolleolle efforts, Faraday is becoming more inclusive than ever 🤗
72
+ 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 ❤️!
73
+ Checkout the "Misc" section below for more details 🙌 !
74
+
75
+ ### Fixes
76
+
77
+ * Fix NoMethodError undefined method 'coverage' (#1255, @Maroo-b)
78
+
79
+ ### Documentation
80
+
81
+ * Some docs on EventMachine adapters. (#1232, @damau)
82
+ * CONTRIBUTING: Fix grammar and layout (#1261, @olleolleolle)
83
+
84
+ ### Misc
85
+
86
+ * Replacing Net::HTTP::Persistent with faraday-net_http_persistent (#1250, @MikeRogers0)
87
+ * CI: Configure the regenerated Coveralls token (#1256, @olleolleolle)
88
+ * Replace Excon adapter with Faraday::Excon gem, and fix autoloading issue with Faraday::NetHttpPersistent (#1257, @iMacTia)
89
+ * Drop CodeClimate (#1259, @olleolleolle)
90
+ * CI: Rename default branch to main (#1263, @olleolleolle)
91
+ * Drop RDoc support file .document (#1264, @olleolleolle, @iMacTia)
92
+ * CONTRIBUTING: add a policy on inclusive language (#1262, @olleolleolle)
93
+ * Add rubocop-inclusivity (#1267, @olleolleolle, @iMacTia)
94
+
95
+ ## [v1.3.1](https://github.com/lostisland/faraday/releases/tag/v1.3.1) (2021-04-16)
96
+
97
+ ### Fixes
98
+
99
+ * Escape colon in path segment (#1237, @yarafan)
100
+ * Handle IPv6 address String on Faraday::Connection#proxy_from_env (#1252, @cosmo0920)
101
+
102
+ ### Documentation
103
+
104
+ * Fix broken Rubydoc.info links (#1236, @nickcampbell18)
105
+ * Add httpx to list of external adapters (#1246, @HoneyryderChuck)
106
+
107
+ ### Misc
108
+
109
+ * Refactor CI to remove duplicated line (#1230, @tricknotes)
110
+ * Gemspec: Pick a good ruby2_keywords release (#1241, @olleolleolle)
111
+
112
+ ## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)
113
+
114
+ ### Highlights
115
+ Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!
116
+
117
+ 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) 🎊!
118
+ 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!
119
+
120
+ This is a huge step towards are Faraday v2.0 objective of pushing adapters and middleware into separate gems.
121
+ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS Conf remote event](https://www.rossconf.io/event/remote/)
122
+
123
+ ### Features
124
+
125
+ * Improves consistency with Faraday::Error and Faraday::RaiseError (#1229, @qsona, @iMacTia)
126
+
127
+ ### Fixes
128
+
129
+ * Don't assign to global ::Timer (#1227, @bpo)
130
+
131
+ ### Documentation
132
+
133
+ * CHANGELOG: add releases after 1.0 (#1225, @olleolleolle)
134
+ * Improves retry middleware documentation. (#1228, @iMacTia)
135
+
136
+ ### Misc
137
+
138
+ * Move out Net::HTTP adapter (#1222, @JanDintel, @iMacTia)
139
+ * Adds Ruby 3.0 to CI Matrix (#1226, @iMacTia)
140
+
141
+
142
+ ## [v1.2.0](https://github.com/lostisland/faraday/releases/tag/v1.2.0) (2020-12-23)
143
+
144
+ ### Features
145
+
146
+ * Introduces `on_request` and `on_complete` methods in `Faraday::Middleware`. (#1194, @iMacTia)
147
+
148
+ ### Fixes
149
+
150
+ * Require 'date' to avoid retry exception (#1206, @rustygeldmacher)
151
+ * Fix rdebug recursion issue (#1205, @native-api)
152
+ * Update call to `em_http_ssl_patch` (#1202, @kylekeesling)
153
+ * `EmHttp` adapter: drop superfluous loaded? check (#1213, @olleolleolle)
154
+ * Avoid 1 use of keyword hackery (#1211, @grosser)
155
+ * Fix #1219 `Net::HTTP` still uses env proxy (#1221, @iMacTia)
156
+
157
+ ### Documentation
158
+
159
+ * Add comment in gemspec to explain exposure of `examples` and `spec` folders. (#1192, @iMacTia)
160
+ * Adapters, how to create them (#1193, @olleolleolle)
161
+ * Update documentation on using the logger (#1196, @tijmenb)
162
+ * Adjust the retry documentation and spec to align with implementation (#1198, @nbeyer)
163
+
164
+ ### Misc
165
+
166
+ * Test against ruby head (#1208, @grosser)
167
+
168
+ ## [v1.1.0](https://github.com/lostisland/faraday/releases/tag/v1.1.0) (2020-10-17)
169
+
170
+ ### Features
171
+
172
+ * Makes parameters sorting configurable (#1162 @wishdev)
173
+ * Introduces `flat_encode` option for multipart adapter. (#1163 @iMacTia)
174
+ * Include request info in exceptions raised by RaiseError Middleware (#1181 @SandroDamilano)
175
+
176
+ ### Fixes
177
+
178
+ * Avoid `last arg as keyword param` warning when building user middleware on Ruby 2.7 (#1153 @dgholz)
179
+ * Limits net-http-persistent version to < 4.0 (#1156 @iMacTia)
180
+ * Update `typhoeus` to new stable version (`1.4`) (#1159 @AlexWayfer)
181
+ * Properly fix test failure with Rack 2.1+. (#1171 @voxik)
182
+
183
+ ### Documentation
184
+
185
+ * Improves documentation on how to contribute to the site by using Docker. (#1175 @iMacTia)
186
+ * Remove retry_change_requests from documentation (#1185 @stim371)
187
+
188
+ ### Misc
189
+
190
+ * Link from GitHub Actions badge to CI workflow (#1141 @olleolleolle)
191
+ * Return tests of `Test` adapter (#1147 @AlexWayfer)
192
+ * Add 1.0 release to wording in CONTRIBUTING (#1155 @olleolleolle)
193
+ * Fix linting bumping Rubocop to 0.90.0 (#1182 @iMacTia)
194
+ * Drop `git ls-files` in gemspec (#1183 @utkarsh2102)
195
+ * Upgrade CI to ruby/setup-ruby (#1187 @gogainda)
196
+
197
+ ## [v1.0.1](https://github.com/lostisland/faraday/releases/tag/v1.0.1) (2020-03-29)
198
+
199
+ ### Fixes
200
+
201
+ * Use Net::HTTP#start(&block) to ensure closed TCP connections (#1117)
202
+ * Fully qualify constants to be checked (#1122)
203
+ * Allows `parse` method to be private/protected in response middleware (#1123)
204
+ * Encode Spaces in Query Strings as '%20' Instead of '+' (#1125)
205
+ * Limits rack to v2.0.x (#1127)
206
+ * Adapter Registry reads also use mutex (#1136)
207
+
208
+ ### Documentation
209
+
210
+ * Retry middleware documentation fix (#1109)
211
+ * Docs(retry): precise usage of retry-after (#1111)
212
+ * README: Link the logo to the website (#1112)
213
+ * Website: add search bar (#1116)
214
+ * Fix request/response mix-up in docs text (#1132)
215
+
216
+ ## [v1.0](https://github.com/lostisland/faraday/releases/tag/v1.0.0) (2020-01-22)
4
217
 
5
218
  Features:
6
219
 
@@ -44,6 +257,13 @@ Misc:
44
257
  * Describe clearing cached stubs #1045 (@viraptor)
45
258
  * Add project metadata to the gemspec #1046 (@orien)
46
259
 
260
+ ## v0.17.4
261
+
262
+ Fixes:
263
+
264
+ * NetHttp adapter: wrap Errno::EADDRNOTAVAIL (#1114, @embs)
265
+ * Fix === for subclasses of deprecated classes (#1243, @mervync)
266
+
47
267
  ## v0.17.3
48
268
 
49
269
  Fixes:
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2019 Rick Olson, Zack Hobson
1
+ Copyright (c) 2009-2020 Rick Olson, Zack Hobson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,15 +1,19 @@
1
- # ![Faraday](./docs/assets/img/repo-card-slim.png)
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
- ![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/test_coverage)](https://codeclimate.com/github/lostisland/faraday/test_coverage)
6
- [![Maintainability](https://api.codeclimate.com/v1/badges/f869daab091ceef1da73/maintainability)](https://codeclimate.com/github/lostisland/faraday/maintainability)
7
- [![Gitter](https://badges.gitter.im/lostisland/faraday.svg)](https://gitter.im/lostisland/faraday?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
4
+ [![GitHub Actions CI](https://github.com/lostisland/faraday/workflows/CI/badge.svg)](https://github.com/lostisland/faraday/actions?query=workflow%3ACI)
5
+ [![GitHub Discussions](https://img.shields.io/github/discussions/lostisland/faraday?logo=github)](https://github.com/lostisland/faraday/discussions)
8
6
 
9
7
 
10
- Faraday is an HTTP client library that provides a common interface over many
11
- adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
12
- 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.
12
+
13
+ ## FARADAY 2.0
14
+
15
+ You're reading the README and looking at the code of our upcoming v2.0 release (the `main` branch, currently in alpha).
16
+ If you're here to read about our latest v1.x release, then please head over to the [1.x branch](https://github.com/lostisland/faraday/tree/1.x).
13
17
 
14
18
  ## Getting Started
15
19
 
@@ -18,10 +22,10 @@ Need more details? See the [Faraday API Documentation][apidoc] to see how it wor
18
22
 
19
23
  ## Supported Ruby versions
20
24
 
21
- This library aims to support and is [tested against][actions] the following Ruby
22
- implementations:
23
-
24
- * Ruby 2.4+
25
+ This library aims to support and is [tested against][actions] the currently officially supported Ruby
26
+ implementations. This means that, even without a major release, we could add or drop support for Ruby versions,
27
+ following their [EOL](https://endoflife.date/ruby).
28
+ Currently that means we support Ruby 2.6+
25
29
 
26
30
  If something doesn't work on one of these Ruby versions, it's a bug.
27
31
 
@@ -43,12 +47,13 @@ Open the issues page and check for the `help wanted` label!
43
47
  But before you start coding, please read our [Contributing Guide][contributing]
44
48
 
45
49
  ## Copyright
46
- &copy; 2009 - 2019, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
50
+ &copy; 2009 - 2021, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
47
51
 
52
+ [awesome]: https://github.com/lostisland/awesome-faraday/#adapters
48
53
  [website]: https://lostisland.github.io/faraday
49
54
  [faraday_team]: https://lostisland.github.io/faraday/team
50
55
  [contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
51
- [apidoc]: http://www.rubydoc.info/gems/faraday
56
+ [apidoc]: https://www.rubydoc.info/github/lostisland/faraday
52
57
  [actions]: https://github.com/lostisland/faraday/actions
53
58
  [jruby]: http://jruby.org/
54
59
  [rubinius]: http://rubini.us/
@@ -12,14 +12,14 @@ class Client
12
12
  @conn = conn
13
13
  end
14
14
 
15
- def sushi(jname)
16
- res = @conn.get("/#{jname}")
15
+ def sushi(jname, params: {})
16
+ res = @conn.get("/#{jname}", params)
17
17
  data = JSON.parse(res.body)
18
18
  data['name']
19
19
  end
20
20
  end
21
21
 
22
- Rspec.describe Client do
22
+ RSpec.describe Client do
23
23
  let(:stubs) { Faraday::Adapter::Test::Stubs.new }
24
24
  let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
25
25
  let(:client) { Client.new(conn) }
@@ -56,10 +56,42 @@ Rspec.describe Client do
56
56
 
57
57
  it 'handles exception' do
58
58
  stubs.get('/ebi') do
59
- raise Faraday::ConnectionFailed, nil
59
+ raise Faraday::ConnectionFailed
60
60
  end
61
61
 
62
62
  expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
63
63
  stubs.verify_stubbed_calls
64
64
  end
65
+
66
+ context 'When the test stub is run in strict_mode' do
67
+ let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }
68
+
69
+ it 'verifies the all parameter values are identical' do
70
+ stubs.get('/ebi?abc=123') do
71
+ [
72
+ 200,
73
+ { 'Content-Type': 'application/javascript' },
74
+ '{"name": "shrimp"}'
75
+ ]
76
+ end
77
+
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')
81
+ stubs.verify_stubbed_calls
82
+ end
83
+ end
84
+
85
+ context 'When the Faraday connection is configured with FlatParamsEncoder' do
86
+ let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }
87
+
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"}'] }
90
+
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')
94
+ stubs.verify_stubbed_calls
95
+ end
96
+ end
65
97
  end
@@ -13,8 +13,8 @@ class Client
13
13
  @conn = conn
14
14
  end
15
15
 
16
- def sushi(jname)
17
- res = @conn.get("/#{jname}")
16
+ def sushi(jname, params: {})
17
+ res = @conn.get("/#{jname}", params)
18
18
  data = JSON.parse(res.body)
19
19
  data['name']
20
20
  end
@@ -42,7 +42,7 @@ class ClientTest < Test::Unit::TestCase
42
42
  stubs.verify_stubbed_calls
43
43
  end
44
44
 
45
- def test_sushi_404
45
+ def test_sushi_not_found
46
46
  stubs = Faraday::Adapter::Test::Stubs.new
47
47
  stubs.get('/ebi') do
48
48
  [
@@ -60,7 +60,7 @@ class ClientTest < Test::Unit::TestCase
60
60
  def test_sushi_exception
61
61
  stubs = Faraday::Adapter::Test::Stubs.new
62
62
  stubs.get('/ebi') do
63
- raise Faraday::ConnectionFailed, nil
63
+ raise Faraday::ConnectionFailed
64
64
  end
65
65
 
66
66
  cli = client(stubs)
@@ -70,6 +70,45 @@ class ClientTest < Test::Unit::TestCase
70
70
  stubs.verify_stubbed_calls
71
71
  end
72
72
 
73
+ def test_strict_mode
74
+ stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
75
+ stubs.get('/ebi?abc=123') do
76
+ [
77
+ 200,
78
+ { 'Content-Type': 'application/javascript' },
79
+ '{"name": "shrimp"}'
80
+ ]
81
+ end
82
+
83
+ cli = client(stubs)
84
+ assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
85
+
86
+ # uncomment to raise Stubs::NotFound
87
+ # assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
88
+ stubs.verify_stubbed_calls
89
+ end
90
+
91
+ def test_non_default_params_encoder
92
+ stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
93
+ stubs.get('/ebi?a=x&a=y&a=z') do
94
+ [
95
+ 200,
96
+ { 'Content-Type': 'application/javascript' },
97
+ '{"name": "shrimp"}'
98
+ ]
99
+ end
100
+ conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
101
+ builder.adapter :test, stubs
102
+ end
103
+
104
+ cli = Client.new(conn)
105
+ assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
106
+
107
+ # uncomment to raise Stubs::NotFound
108
+ # assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
109
+ stubs.verify_stubbed_calls
110
+ end
111
+
73
112
  def client(stubs)
74
113
  conn = Faraday.new do |builder|
75
114
  builder.adapter :test, stubs
@@ -25,6 +25,9 @@ module Faraday
25
25
  # "showing item: #{meta[:match_data][1]}"
26
26
  # ]
27
27
  # end
28
+ #
29
+ # # You can set strict_mode to exactly match the stubbed requests.
30
+ # stub.strict_mode = true
28
31
  # end
29
32
  # end
30
33
  #
@@ -47,10 +50,11 @@ module Faraday
47
50
  class NotFound < StandardError
48
51
  end
49
52
 
50
- def initialize
53
+ def initialize(strict_mode: false)
51
54
  # { get: [Stub, Stub] }
52
55
  @stack = {}
53
56
  @consumed = {}
57
+ @strict_mode = strict_mode
54
58
  yield(self) if block_given?
55
59
  end
56
60
 
@@ -58,18 +62,20 @@ module Faraday
58
62
  @stack.empty?
59
63
  end
60
64
 
61
- def match(request_method, host, path, headers, body)
65
+ # @param env [Faraday::Env]
66
+ def match(env)
67
+ request_method = env[:method]
62
68
  return false unless @stack.key?(request_method)
63
69
 
64
70
  stack = @stack[request_method]
65
71
  consumed = (@consumed[request_method] ||= [])
66
72
 
67
- stub, meta = matches?(stack, host, path, headers, body)
73
+ stub, meta = matches?(stack, env)
68
74
  if stub
69
75
  consumed << stack.delete(stub)
70
76
  return stub, meta
71
77
  end
72
- matches?(consumed, host, path, headers, body)
78
+ matches?(consumed, env)
73
79
  end
74
80
 
75
81
  def get(path, headers = {}, &block)
@@ -115,6 +121,17 @@ module Faraday
115
121
  raise failed_stubs.join(' ') unless failed_stubs.empty?
116
122
  end
117
123
 
124
+ # Set strict_mode. If the value is true, this adapter tries to find matched requests strictly,
125
+ # which means that all of a path, parameters, and headers must be the same as an actual request.
126
+ def strict_mode=(value)
127
+ @strict_mode = value
128
+ @stack.each do |_method, stubs|
129
+ stubs.each do |stub|
130
+ stub.strict_mode = value
131
+ end
132
+ end
133
+ end
134
+
118
135
  protected
119
136
 
120
137
  def new_stub(request_method, path, headers = {}, body = nil, &block)
@@ -127,14 +144,18 @@ module Faraday
127
144
  Faraday::Utils.URI(path).host
128
145
  ]
129
146
  end
147
+ path, query = normalized_path.respond_to?(:split) ? normalized_path.split('?') : normalized_path
148
+ headers = Utils::Headers.new(headers)
130
149
 
131
- stub = Stub.new(host, normalized_path, headers, body, block)
150
+ stub = Stub.new(host, path, query, headers, body, @strict_mode, block)
132
151
  (@stack[request_method] ||= []) << stub
133
152
  end
134
153
 
135
- def matches?(stack, host, path, headers, body)
154
+ # @param stack [Hash]
155
+ # @param env [Faraday::Env]
156
+ def matches?(stack, env)
136
157
  stack.each do |stub|
137
- match_result, meta = stub.matches?(host, path, headers, body)
158
+ match_result, meta = stub.matches?(env)
138
159
  return stub, meta if match_result
139
160
  end
140
161
  nil
@@ -142,35 +163,20 @@ module Faraday
142
163
  end
143
164
 
144
165
  # Stub request
145
- # rubocop:disable Style/StructInheritance
146
- class Stub < Struct.new(:host, :path, :params, :headers, :body, :block)
147
- # rubocop:enable Style/StructInheritance
148
- def initialize(host, full, headers, body, block)
149
- path, query = full.respond_to?(:split) ? full.split('?') : full
150
- params =
151
- if query
152
- Faraday::Utils.parse_nested_query(query)
153
- else
154
- {}
155
- end
156
-
157
- super(host, path, params, headers, body, block)
158
- end
166
+ class Stub < Struct.new(:host, :path, :query, :headers, :body, :strict_mode, :block) # rubocop:disable Style/StructInheritance
167
+ # @param env [Faraday::Env]
168
+ def matches?(env)
169
+ request_host = env[:url].host
170
+ request_path = Faraday::Utils.normalize_path(env[:url].path)
171
+ request_headers = env.request_headers
172
+ request_body = env[:body]
159
173
 
160
- def matches?(request_host, request_uri, request_headers, request_body)
161
- request_path, request_query = request_uri.split('?')
162
- request_params =
163
- if request_query
164
- Faraday::Utils.parse_nested_query(request_query)
165
- else
166
- {}
167
- end
168
174
  # meta is a hash used as carrier
169
175
  # that will be yielded to consumer block
170
176
  meta = {}
171
177
  [(host.nil? || host == request_host) &&
172
178
  path_match?(request_path, meta) &&
173
- params_match?(request_params) &&
179
+ params_match?(env) &&
174
180
  (body.to_s.size.zero? || request_body == body) &&
175
181
  headers_match?(request_headers), meta]
176
182
  end
@@ -183,13 +189,30 @@ module Faraday
183
189
  end
184
190
  end
185
191
 
186
- def params_match?(request_params)
192
+ # @param env [Faraday::Env]
193
+ def params_match?(env)
194
+ request_params = env[:params]
195
+ params = env.params_encoder.decode(query) || {}
196
+
197
+ if strict_mode
198
+ return Set.new(params) == Set.new(request_params)
199
+ end
200
+
187
201
  params.keys.all? do |key|
188
202
  request_params[key] == params[key]
189
203
  end
190
204
  end
191
205
 
192
206
  def headers_match?(request_headers)
207
+ if strict_mode
208
+ headers_with_user_agent = headers.dup.tap do |hs|
209
+ # NOTE: Set User-Agent in case it's not set when creating Stubs.
210
+ # Users would not want to set Faraday's User-Agent explicitly.
211
+ hs[:user_agent] ||= Connection::USER_AGENT
212
+ end
213
+ return Set.new(headers_with_user_agent) == Set.new(request_headers)
214
+ end
215
+
193
216
  headers.keys.all? do |key|
194
217
  request_headers[key] == headers[key]
195
218
  end
@@ -210,26 +233,19 @@ module Faraday
210
233
  yield(stubs)
211
234
  end
212
235
 
236
+ # @param env [Faraday::Env]
213
237
  def call(env)
214
238
  super
215
- host = env[:url].host
216
- normalized_path = Faraday::Utils.normalize_path(env[:url])
217
- params_encoder = env.request.params_encoder ||
218
- Faraday::Utils.default_params_encoder
219
239
 
220
- stub, meta = stubs.match(env[:method], host, normalized_path,
221
- env.request_headers, env[:body])
240
+ env.request.params_encoder ||= Faraday::Utils.default_params_encoder
241
+ env[:params] = env.params_encoder.decode(env[:url].query) || {}
242
+ stub, meta = stubs.match(env)
222
243
 
223
244
  unless stub
224
245
  raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\
225
- "#{normalized_path} #{env[:body]}"
246
+ "#{env[:url]} #{env[:body]}"
226
247
  end
227
248
 
228
- env[:params] = if (query = env[:url].query)
229
- params_encoder.decode(query)
230
- else
231
- {}
232
- end
233
249
  block_arity = stub.block.arity
234
250
  status, headers, body =
235
251
  if block_arity >= 0
@@ -244,3 +260,5 @@ module Faraday
244
260
  end
245
261
  end
246
262
  end
263
+
264
+ Faraday::Adapter.register_middleware(test: Faraday::Adapter::Test)
@@ -5,28 +5,13 @@ 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
- net_http: [:NetHttp, 'net_http'],
15
- net_http_persistent: [
16
- :NetHttpPersistent,
17
- 'net_http_persistent'
18
- ],
19
- typhoeus: [:Typhoeus, 'typhoeus'],
20
- patron: [:Patron, 'patron'],
21
- em_synchrony: [:EMSynchrony, 'em_synchrony'],
22
- em_http: [:EMHttp, 'em_http'],
23
- excon: [:Excon, 'excon'],
24
- rack: [:Rack, 'rack'],
25
- httpclient: [:HTTPClient, 'httpclient']
26
-
27
11
  # This module marks an Adapter as supporting parallel requests.
28
12
  module Parallelism
29
13
  attr_writer :supports_parallel
14
+
30
15
  def supports_parallel?
31
16
  @supports_parallel
32
17
  end
@@ -113,3 +98,5 @@ module Faraday
113
98
  }.freeze
114
99
  end
115
100
  end
101
+
102
+ require 'faraday/adapter/test'