webmock 3.16.2 → 3.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +144 -6
  3. data/README.md +54 -18
  4. data/lib/webmock/api.rb +2 -0
  5. data/lib/webmock/assertion_failure.rb +2 -0
  6. data/lib/webmock/callback_registry.rb +2 -0
  7. data/lib/webmock/config.rb +2 -0
  8. data/lib/webmock/cucumber.rb +2 -0
  9. data/lib/webmock/deprecation.rb +2 -0
  10. data/lib/webmock/errors.rb +2 -0
  11. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +2 -0
  12. data/lib/webmock/http_lib_adapters/curb_adapter.rb +2 -0
  13. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +15 -7
  14. data/lib/webmock/http_lib_adapters/excon_adapter.rb +2 -0
  15. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +2 -0
  16. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +2 -0
  17. data/lib/webmock/http_lib_adapters/http_rb/client.rb +2 -0
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +17 -5
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +32 -12
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +2 -0
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +2 -0
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +7 -5
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +26 -25
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +2 -0
  25. data/lib/webmock/http_lib_adapters/net_http.rb +19 -101
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +2 -0
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +2 -0
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +18 -2
  29. data/lib/webmock/matchers/any_arg_matcher.rb +2 -0
  30. data/lib/webmock/matchers/hash_argument_matcher.rb +2 -0
  31. data/lib/webmock/matchers/hash_excluding_matcher.rb +2 -0
  32. data/lib/webmock/matchers/hash_including_matcher.rb +2 -0
  33. data/lib/webmock/minitest.rb +2 -0
  34. data/lib/webmock/rack_response.rb +5 -1
  35. data/lib/webmock/request_body_diff.rb +2 -0
  36. data/lib/webmock/request_execution_verifier.rb +2 -0
  37. data/lib/webmock/request_pattern.rb +26 -8
  38. data/lib/webmock/request_registry.rb +2 -0
  39. data/lib/webmock/request_signature.rb +4 -2
  40. data/lib/webmock/request_signature_snippet.rb +2 -0
  41. data/lib/webmock/request_stub.rb +20 -1
  42. data/lib/webmock/response.rb +15 -13
  43. data/lib/webmock/responses_sequence.rb +2 -0
  44. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +2 -0
  45. data/lib/webmock/rspec/matchers/webmock_matcher.rb +2 -0
  46. data/lib/webmock/rspec/matchers.rb +2 -0
  47. data/lib/webmock/rspec.rb +2 -0
  48. data/lib/webmock/stub_registry.rb +2 -0
  49. data/lib/webmock/stub_request_snippet.rb +2 -0
  50. data/lib/webmock/test_unit.rb +2 -0
  51. data/lib/webmock/util/hash_counter.rb +12 -6
  52. data/lib/webmock/util/hash_keys_stringifier.rb +2 -0
  53. data/lib/webmock/util/hash_validator.rb +2 -0
  54. data/lib/webmock/util/headers.rb +23 -10
  55. data/lib/webmock/util/json.rb +2 -0
  56. data/lib/webmock/util/query_mapper.rb +2 -0
  57. data/lib/webmock/util/uri.rb +3 -1
  58. data/lib/webmock/util/values_stringifier.rb +2 -0
  59. data/lib/webmock/util/version_checker.rb +7 -5
  60. data/lib/webmock/version.rb +3 -1
  61. data/lib/webmock/webmock.rb +2 -0
  62. data/lib/webmock.rb +2 -0
  63. metadata +43 -161
  64. data/.gemtest +0 -0
  65. data/.github/workflows/CI.yml +0 -38
  66. data/.gitignore +0 -34
  67. data/.rspec-tm +0 -2
  68. data/Gemfile +0 -9
  69. data/Rakefile +0 -38
  70. data/minitest/test_helper.rb +0 -34
  71. data/minitest/test_webmock.rb +0 -9
  72. data/minitest/webmock_spec.rb +0 -60
  73. data/spec/acceptance/async_http_client/async_http_client_spec.rb +0 -375
  74. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +0 -73
  75. data/spec/acceptance/curb/curb_spec.rb +0 -510
  76. data/spec/acceptance/curb/curb_spec_helper.rb +0 -147
  77. data/spec/acceptance/em_http_request/em_http_request_spec.rb +0 -462
  78. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +0 -77
  79. data/spec/acceptance/excon/excon_spec.rb +0 -77
  80. data/spec/acceptance/excon/excon_spec_helper.rb +0 -52
  81. data/spec/acceptance/http_rb/http_rb_spec.rb +0 -93
  82. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +0 -54
  83. data/spec/acceptance/httpclient/httpclient_spec.rb +0 -217
  84. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +0 -57
  85. data/spec/acceptance/manticore/manticore_spec.rb +0 -107
  86. data/spec/acceptance/manticore/manticore_spec_helper.rb +0 -35
  87. data/spec/acceptance/net_http/net_http_shared.rb +0 -190
  88. data/spec/acceptance/net_http/net_http_spec.rb +0 -396
  89. data/spec/acceptance/net_http/net_http_spec_helper.rb +0 -64
  90. data/spec/acceptance/net_http/real_net_http_spec.rb +0 -20
  91. data/spec/acceptance/patron/patron_spec.rb +0 -125
  92. data/spec/acceptance/patron/patron_spec_helper.rb +0 -54
  93. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +0 -313
  94. data/spec/acceptance/shared/callbacks.rb +0 -148
  95. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +0 -36
  96. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +0 -95
  97. data/spec/acceptance/shared/precedence_of_stubs.rb +0 -15
  98. data/spec/acceptance/shared/request_expectations.rb +0 -930
  99. data/spec/acceptance/shared/returning_declared_responses.rb +0 -409
  100. data/spec/acceptance/shared/stubbing_requests.rb +0 -678
  101. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +0 -135
  102. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +0 -60
  103. data/spec/acceptance/webmock_shared.rb +0 -41
  104. data/spec/fixtures/test.txt +0 -1
  105. data/spec/quality_spec.rb +0 -84
  106. data/spec/spec_helper.rb +0 -48
  107. data/spec/support/example_curl_output.txt +0 -22
  108. data/spec/support/failures.rb +0 -9
  109. data/spec/support/my_rack_app.rb +0 -53
  110. data/spec/support/network_connection.rb +0 -19
  111. data/spec/support/webmock_server.rb +0 -70
  112. data/spec/unit/api_spec.rb +0 -175
  113. data/spec/unit/errors_spec.rb +0 -129
  114. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +0 -17
  115. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +0 -12
  116. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +0 -61
  117. data/spec/unit/matchers/hash_including_matcher_spec.rb +0 -87
  118. data/spec/unit/rack_response_spec.rb +0 -112
  119. data/spec/unit/request_body_diff_spec.rb +0 -90
  120. data/spec/unit/request_execution_verifier_spec.rb +0 -208
  121. data/spec/unit/request_pattern_spec.rb +0 -736
  122. data/spec/unit/request_registry_spec.rb +0 -95
  123. data/spec/unit/request_signature_snippet_spec.rb +0 -89
  124. data/spec/unit/request_signature_spec.rb +0 -175
  125. data/spec/unit/request_stub_spec.rb +0 -234
  126. data/spec/unit/response_spec.rb +0 -286
  127. data/spec/unit/stub_registry_spec.rb +0 -103
  128. data/spec/unit/stub_request_snippet_spec.rb +0 -115
  129. data/spec/unit/util/hash_counter_spec.rb +0 -39
  130. data/spec/unit/util/hash_keys_stringifier_spec.rb +0 -27
  131. data/spec/unit/util/headers_spec.rb +0 -28
  132. data/spec/unit/util/json_spec.rb +0 -33
  133. data/spec/unit/util/query_mapper_spec.rb +0 -157
  134. data/spec/unit/util/uri_spec.rb +0 -371
  135. data/spec/unit/util/version_checker_spec.rb +0 -65
  136. data/spec/unit/webmock_spec.rb +0 -114
  137. data/test/http_request.rb +0 -24
  138. data/test/shared_test.rb +0 -108
  139. data/test/test_helper.rb +0 -23
  140. data/test/test_webmock.rb +0 -12
  141. data/webmock.gemspec +0 -54
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 438dab0f90920381d01009b8c75892ac83e46043e170c17e1dba5e46ca755c61
4
- data.tar.gz: a760f4b2399f465c62cf03268c13a7ae973900945ed0c4631e146c44f688a97d
3
+ metadata.gz: 8a2d1add2b84d303329a722f0c7e9643f237949d0dc471ea1678021324cd41b4
4
+ data.tar.gz: e00640f1b7664959922011ae2f7b922811d94be4fb1bea90a988f3cca83486bc
5
5
  SHA512:
6
- metadata.gz: c8033597431b3e8ce43e60837d31f18d1ffa52c35079827e122a4616587f29b480b2340786c841bbe9f6b9390c5df08e9b84e933160412c8dc1d5c8a9b214f8b
7
- data.tar.gz: e0580a7b6b712073edf7d6c38fbf7b6df00a294aa9910f1de0e2de61535de67d07315b5d10f141d7e3387bf91aaee7ce6bb204d40190b5357b2120dc0d7d4427
6
+ metadata.gz: 87f797196dedde96873a3740234d33f799745df144953ccfc36403ca6fbe4bd239f5f05818eb7cec25730823c2f99f83a1f42b79cf977b5672d720ed02bb3495
7
+ data.tar.gz: af708a4202382fb1a55c90a7f2c14b1a9d9b277bc8c4ee898dd31b712162bee4e9fb1fb6dd2931c1e39123eee8f15545db9fcd198ea5751f746be7eabfad33eb
data/CHANGELOG.md CHANGED
@@ -1,14 +1,148 @@
1
1
  # Changelog
2
2
 
3
- # 3.16.2
3
+ # 3.23.0
4
+
5
+ * Fixed HTTP.rb adapter to support streaming real responses when WebMock is enabled.
6
+
7
+ Thanks to [Viacheslav Nepomniashchikh](https://github.com/aka-nez) for reporting and investigating this [issue](https://github.com/bblimke/webmock/issues/1017).
8
+
9
+ # 3.22.0
10
+
11
+ * Addressed an issue in the HTTPClient adapter where memoized stubbed responses and memoized request_signatures were incorrectly persisted between subsequent requests ([#1019](https://github.com/bblimke/webmock/issues/1019)). The implementation of a more robust thread-safety solution by [Tom Beauvais](https://github.com/tbeauvais) in [PR #300](https://github.com/bblimke/webmock/pull/300) not only resolved the memoization problem but also enhanced the overall thread safety of the adapter. This update ensures that stubbed responses and request signatures are correctly isolated to individual requests, improving both consistency and thread safety.
12
+
13
+ # 3.21.2
14
+
15
+ * Corrected type checking in `WebMock::Response#assert_valid_body!` to accurately recognize `Hash` objects. Additionally, improved the clarity of the error message for unsupported body types, guiding users towards proper usage.
16
+
17
+ Thanks to [Jake Robb](https://github.com/jakerobb) for reporting.
18
+
19
+ # 3.21.1
20
+
21
+ * The stubbed Net::HTTPResponse#uri now returns request.uri, aligning it with the behavior of an actual Net::HTTPResponse.
22
+
23
+ Thanks to [Abe Voelker](https://github.com/abevoelker) for reporting and to [Victor Maslov](https://github.com/Nakilon) and [Gio Lodi](https://github.com/mokagio) for the suggested solution.
24
+
25
+ # 3.21.0
26
+
27
+ * Don't use deprecated Rack::VERSION for Rack >= 3
28
+
29
+ Thanks to [Étienne Barrié](https://github.com/etiennebarrie)
30
+
31
+ * Updated HTTPClient adapter, to build request signature using the URI after filters have been applied.
32
+
33
+ Thanks to [Matt Brown](https://github.com/mattbnz)
34
+
35
+ # 3.20.0
36
+
37
+ * Optimised EmHttpRequestAdapter performance.
38
+
39
+ Thanks to [Ricardo Trindade](https://github.com/RicardoTrindade)
40
+
41
+ * Removed runtime dependency on base64.
42
+
43
+ Thanks to [Earlopain](https://github.com/Earlopain)
44
+
45
+ * Typhoeus::Response objects constructed from stubbed responses now have all timing attributes set to 0.0.
46
+
47
+ Thanks to [James Brown](https://github.com/Roguelazer)
48
+
49
+ * Optimised WebMock::Util::Headers by removing redundant freeze invocations.
50
+
51
+ Thanks to [Kazuhiro NISHIYAMA](https://github.com/znz)
52
+
53
+ * The default stubbed response body, which is an empty String, is unfrozen.
54
+
55
+ * When building signatures of requests made by http.rb, the request body encoding is now preserved.
56
+
57
+ # 3.19.1
58
+
59
+ * When passing a Proc or lambda as response body to `to_return_json`, the body is evaluated at the time of request and not at the time of `to_return_json` method invocation.
60
+
61
+ Thanks to [Jason Karns](https://github.com/jasonkarns) for reporting.
62
+
63
+ # 3.19.0
64
+
65
+ * Do not alter real (non-stubbed) request headers when handling em-http-request requests.
66
+
67
+ Thanks to [Yoann Lecuyer](https://github.com/ylecuyer)
68
+
69
+ * Fix matching stubs with HashExcludingMatcher
70
+
71
+ Thanks to [Lucas Arnaud](https://github.com/lucasarnaud)
72
+
73
+ * Remove development and test files from the gem package
74
+
75
+ Thanks to [Orien Madgwick](https://github.com/orien)
4
76
 
5
- * Minimum required Ruby version is 2.0 again.
77
+ * Fix supported http client gem version checks.
6
78
 
7
- # 3.16.1
79
+ Thanks to [Marc Rohloff](https://github.com/marcrohloff) and [Roman Stražanec](https://github.com/romanstrazanec)
80
+
81
+ * Non-string body passed to #to_return_json is now converted to JSON string.
82
+
83
+ Thanks to [inkstak](https://github.com/inkstak)
84
+
85
+ * `[::1]` is recognised as localhost e.g when passing `allow_localhost` option to `disable_net_connect!`
86
+
87
+ Thanks to [Yuki Inoue](https://github.com/Yuki-Inoue)
88
+
89
+ * Optimized `normalize_headers` for performance
90
+
91
+ Thanks to [Brandon Weaver](https://github.com/baweaver)
92
+
93
+ * Added Frozen string literal to files to optimise memory usage
94
+
95
+ Thanks to [Josh Nichols](https://github.com/technicalpickles)
96
+
97
+ # 3.18.1
98
+
99
+ * Reverted simplified connection handing in Net::HTTP adapter due to https://github.com/bblimke/webmock/issues/999
100
+
101
+ # 3.18.0
102
+
103
+ * Net::BufferedIO is not replaced anymore.
104
+
105
+ Thanks to [Ray Zane](https://github.com/rzane)
106
+
107
+ * Simplified connection handing in Net::HTTP adapter.
108
+
109
+ Thanks to [Ray Zane](https://github.com/rzane)
110
+
111
+ # 3.17.1
112
+
113
+ * Fixed Syntax Error
114
+
115
+ Thanks to [Mark Spangler](https://github.com/mspangler)
116
+
117
+ # 3.17.0
8
118
 
9
119
  * Minimum required Ruby version is 2.3
10
120
 
11
- # 3.16.0
121
+ Thanks to [Go Sueyoshi](https://github.com/sue445)
122
+
123
+ * When using Net::HTTP, stubbed socket StubSocket#close and StubSocket#closed? behave more like the real sockets.
124
+
125
+ Thanks to [Ray Zane](https://github.com/rzane)
126
+
127
+ * Added `peeraddr`, `ssl_version` and `cipher` methods to stubbed sockets used by Net::HTTP.
128
+
129
+ Thanks to [Ray Zane](https://github.com/rzane)
130
+
131
+ * Added support for matching top-level array in JSON request body.
132
+
133
+ E.g.
134
+
135
+ ````
136
+ stub_request(:post, 'www.example.com').with(body: [{a: 1}])
137
+ ````
138
+
139
+ Thanks to [Cedric Sohrauer](https://github.com/cedrics)
140
+
141
+ # 3.16.2
142
+
143
+ * Minimum required Ruby version is 2.0.
144
+
145
+ # 3.16.0 (yanked)
12
146
 
13
147
  * Fix leaky file descriptors and reuse socket for persistent connections.
14
148
 
@@ -18,7 +152,11 @@
18
152
 
19
153
  Thanks to [Ray Zane](https://github.com/rzane)
20
154
 
21
- # 3.15.0
155
+ # 3.15.2
156
+
157
+ * Minimum required Ruby version is 2.0.
158
+
159
+ # 3.15.0 (yanked)
22
160
 
23
161
  * fixed async-http adapter on Windows
24
162
 
@@ -72,7 +210,7 @@
72
210
 
73
211
  * Fixed async-http adapter to only considered requests as real if they are real.
74
212
 
75
- Thanks to Thanks to [Tony Schneider](https://github.com/tonywok) and [Samuel Williams](https://github.com/ioquatix)
213
+ Thanks to [Tony Schneider](https://github.com/tonywok) and [Samuel Williams](https://github.com/ioquatix)
76
214
 
77
215
  # 3.11.2
78
216
 
data/README.md CHANGED
@@ -1,11 +1,9 @@
1
1
  WebMock
2
2
  =======
3
3
  [![Gem Version](https://badge.fury.io/rb/webmock.svg)](http://badge.fury.io/rb/webmock)
4
- [![Build Status](https://github.com/bblimke/webmock/workflows/CI/badge.svg?branch=master)](https://github.com/bblimke/webmock/actions)
4
+ [![Build Status](https://github.com/bblimke/webmock/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/bblimke/webmock/actions)
5
5
  [![Code Climate](https://codeclimate.com/github/bblimke/webmock/badges/gpa.svg)](https://codeclimate.com/github/bblimke/webmock)
6
6
  [![Mentioned in Awesome Ruby](https://awesome.re/mentioned-badge.svg)](https://github.com/markets/awesome-ruby)
7
- [![Inline docs](http://inch-ci.org/github/bblimke/webmock.svg?branch=master)](http://inch-ci.org/github/bblimke/webmock)
8
- [![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=webmock&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=webmock&package-manager=bundler&version-scheme=semver)
9
7
 
10
8
  Library for stubbing and setting expectations on HTTP requests in Ruby.
11
9
 
@@ -40,32 +38,34 @@ Supported HTTP libraries
40
38
 
41
39
  Supported Ruby Interpreters
42
40
  ---------------------------
43
-
44
- * MRI 2.5
45
41
  * MRI 2.6
46
42
  * MRI 2.7
43
+ * MRI 3.0
44
+ * MRI 3.1
45
+ * MRI 3.2
47
46
  * JRuby
48
- * Rubinius
49
47
 
50
48
  ## Installation
51
49
 
52
50
  ```bash
53
- gem install webmock
51
+ gem install webmock
54
52
  ```
55
53
  or alternatively:
56
54
 
57
55
  ```ruby
58
- # add to your Gemfile
59
- group :test do
60
- gem "webmock"
61
- end
56
+ # add to your Gemfile
57
+ group :test do
58
+ gem "webmock"
59
+ end
62
60
  ```
63
61
 
64
62
  ### or to install the latest development version from github master
65
63
 
66
- git clone http://github.com/bblimke/webmock.git
67
- cd webmock
68
- rake install
64
+ ```bash
65
+ git clone http://github.com/bblimke/webmock.git
66
+ cd webmock
67
+ rake install
68
+ ```
69
69
 
70
70
  ## Upgrading from v1.x to v2.x
71
71
 
@@ -330,6 +330,16 @@ stub_request(:any, "www.example.com").
330
330
  Net::HTTP.get('www.example.com', '/') # ===> "abc\n"
331
331
  ```
332
332
 
333
+ ### Response with JSON body
334
+
335
+ ```ruby
336
+
337
+ stub_request(:any, "www.example.com").
338
+ to_return_json(body: {foo: "bar"})
339
+
340
+ Net::HTTP.get('www.example.com', '/') # ===> "{\"foo\": \"bar\"}"
341
+ ```
342
+
333
343
  ### Response with custom status message
334
344
 
335
345
  ```ruby
@@ -383,7 +393,8 @@ RestClient.post('www.example.net', 'abc') # ===> "abc\n"
383
393
 
384
394
  ### Dynamically evaluated raw responses recorded with `curl -is`
385
395
 
386
- `curl -is www.example.com > /tmp/www.example.com.txt`
396
+ `curl -is www.example.com > /tmp/www.example.com.txt`
397
+
387
398
  ```ruby
388
399
  stub_request(:get, "www.example.com").
389
400
  to_return(lambda { |request| File.new("/tmp/#{request.uri.host.to_s}.txt") })
@@ -540,7 +551,7 @@ RestClient.get('www.example.org:8080', '/') # ===> Allowed
540
551
  With a `Regexp` matching the URI:
541
552
 
542
553
  ```ruby
543
- WebMock.disable_net_connect!(allow: %r{ample.org/foo})
554
+ WebMock.disable_net_connect!(allow: %r{ample\.org/foo})
544
555
 
545
556
  RestClient.get('www.example.org', '/foo/bar') # ===> Allowed
546
557
  RestClient.get('sample.org', '/foo') # ===> Allowed
@@ -595,7 +606,11 @@ which can be passed to `WebMock.allow_net_connect!` and `WebMock.disable_net_con
595
606
  WebMock.allow_net_connect!(net_http_connect_on_start: true)
596
607
  ```
597
608
 
598
- This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
609
+ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`. At the time of connection being made there is no information about the request or URL yet, therefore WebMock is not able to decide whether to stub a request or not and all connections are allowed. To enable connections only to a specific domain (e.g. your test server) use:
610
+
611
+ ```ruby
612
+ WebMock.allow_net_connect!(net_http_connect_on_start: "www.example.com")
613
+ ```
599
614
 
600
615
  ## Setting Expectations
601
616
 
@@ -1166,13 +1181,34 @@ People who submitted patches and new features or suggested improvements. Many th
1166
1181
  * Timmitry
1167
1182
  * Michael Fairley
1168
1183
  * Ray Zane
1184
+ * Go Sueyoshi
1185
+ * Cedric Sohrauer
1186
+ * Akira Matsuda
1187
+ * Mark Spangler
1188
+ * Henrik Nyh
1189
+ * Yoann Lecuyer
1190
+ * Lucas Arnaud
1191
+ * Marc Rohloff
1192
+ * inkstak
1193
+ * Yuki Inoue
1194
+ * Brandon Weaver
1195
+ * Josh Nichols
1196
+ * Ricardo Trindade
1197
+ * Earlopain
1198
+ * James Brown
1199
+ * Kazuhiro NISHIYAMA
1200
+ * Étienne Barrié
1201
+ * Matt Brown
1202
+ * Victor Maslov
1203
+ * Gio Lodi
1204
+ * Ryan Brooks
1169
1205
 
1170
1206
  For a full list of contributors you can visit the
1171
1207
  [contributors](https://github.com/bblimke/webmock/contributors) page.
1172
1208
 
1173
1209
  ## Background
1174
1210
 
1175
- Thank you Fakeweb! This library was inspired by [FakeWeb](http://fakeweb.rubyforge.org).
1211
+ Thank you Fakeweb! This library was inspired by [FakeWeb](https://github.com/chrisk/fakeweb).
1176
1212
  I imported some solutions from that project to WebMock. I also copied some code i.e Net:HTTP adapter.
1177
1213
  Fakeweb architecture unfortunately didn't allow me to extend it easily with the features I needed.
1178
1214
  I also preferred some things to work differently i.e request stub precedence.
data/lib/webmock/api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
  module API
3
5
  extend self
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
  class AssertionFailure
3
5
  @error_class = RuntimeError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
  class CallbackRegistry
3
5
  @@callbacks = []
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
  class Config
3
5
  include Singleton
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'webmock'
2
4
  require 'webmock/rspec/matchers'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
  class Deprecation
3
5
  class << self
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
 
3
5
  class NetConnectNotAllowedError < Exception
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'async'
3
5
  require 'async/http'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'curb'
3
5
  rescue LoadError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'em-http-request'
3
5
  rescue LoadError
@@ -101,7 +103,7 @@ if defined?(EventMachine::HttpClient)
101
103
 
102
104
  def connection_completed
103
105
  @state = :response_header
104
- send_request(request_signature.headers, request_signature.body)
106
+ send_request(*headers_and_body_processed_by_middleware)
105
107
  end
106
108
 
107
109
  def send_request(head, body)
@@ -154,7 +156,7 @@ if defined?(EventMachine::HttpClient)
154
156
  raw_cookie = response_header.cookie
155
157
  raw_cookie = [raw_cookie] if raw_cookie.is_a? String
156
158
 
157
- cookie = raw_cookie.select { |c| c.start_with? name }.first
159
+ cookie = raw_cookie.detect { |c| c.start_with? name }
158
160
  cookie and cookie.split('=', 2)[1]
159
161
  end
160
162
 
@@ -168,12 +170,18 @@ if defined?(EventMachine::HttpClient)
168
170
  webmock_response
169
171
  end
170
172
 
171
- def build_request_signature
172
- headers, body = build_request, @req.body
173
-
174
- @conn.middleware.select {|m| m.respond_to?(:request) }.each do |m|
175
- headers, body = m.request(self, headers, body)
173
+ def headers_and_body_processed_by_middleware
174
+ @headers_and_body_processed_by_middleware ||= begin
175
+ head, body = build_request, @req.body
176
+ @conn.middleware.each do |m|
177
+ head, body = m.request(self, head, body) if m.respond_to?(:request)
178
+ end
179
+ [head, body]
176
180
  end
181
+ end
182
+
183
+ def build_request_signature
184
+ headers, body = headers_and_body_processed_by_middleware
177
185
 
178
186
  method = @req.method
179
187
  uri = @req.uri.clone
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require 'excon'
3
5
  rescue LoadError
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
  class HttpLibAdapter
3
5
  def self.adapter_for(lib)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module WebMock
2
4
  class HttpLibAdapterRegistry
3
5
  include Singleton
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HTTP
2
4
  class Client
3
5
  alias_method :__perform__, :perform
@@ -1,11 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HTTP
2
4
  class Request
3
5
  def webmock_signature
4
- request_body = if defined?(HTTP::Request::Body)
5
- ''.tap { |string| body.each { |part| string << part } }
6
- else
7
- body
8
- end
6
+ request_body = nil
7
+
8
+ if defined?(HTTP::Request::Body)
9
+ request_body = String.new
10
+ first_chunk_encoding = nil
11
+ body.each do |part|
12
+ request_body << part
13
+ first_chunk_encoding ||= part.encoding
14
+ end
15
+
16
+ request_body.force_encoding(first_chunk_encoding) if first_chunk_encoding
17
+ request_body
18
+ else
19
+ request_body = body
20
+ end
9
21
 
10
22
  ::WebMock::RequestSignature.new(verb, uri.to_s, {
11
23
  headers: headers.to_h,
@@ -1,12 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HTTP
2
4
  class Response
3
5
  def to_webmock
4
6
  webmock_response = ::WebMock::Response.new
5
7
 
6
8
  webmock_response.status = [status.to_i, reason]
9
+
7
10
  webmock_response.body = body.to_s
8
- webmock_response.headers = headers.to_h
11
+ # This call is used to reset the body of the response to enable it to be streamed if necessary.
12
+ # The `body.to_s` call above reads the body, which allows WebMock to trigger any registered callbacks.
13
+ # However, once the body is read to_s, it cannot be streamed again and attempting to do so
14
+ # will raise a "HTTP::StateError: body has already been consumed" error.
15
+ # To avoid this error, we replace the original body with a new one.
16
+ # The new body has its @stream attribute set to new Streamer, instead of the original Connection.
17
+ # Unfortunately, it's not possible to reset the original body to its initial streaming state.
18
+ # Therefore, this replacement is the best workaround currently available.
19
+ reset_body_to_allow_it_to_be_streamed!(webmock_response)
9
20
 
21
+ webmock_response.headers = headers.to_h
10
22
  webmock_response
11
23
  end
12
24
 
@@ -19,16 +31,7 @@ module HTTP
19
31
  # HTTP.rb 3.0+ uses a keyword argument to pass the encoding, but 1.x
20
32
  # and 2.x use a positional argument, and 0.x don't support supplying
21
33
  # the encoding.
22
- body = if HTTP::VERSION < "1.0.0"
23
- Body.new(Streamer.new(webmock_response.body))
24
- elsif HTTP::VERSION < "3.0.0"
25
- Body.new(Streamer.new(webmock_response.body), webmock_response.body.encoding)
26
- else
27
- Body.new(
28
- Streamer.new(webmock_response.body, encoding: webmock_response.body.encoding),
29
- encoding: webmock_response.body.encoding
30
- )
31
- end
34
+ body = build_http_rb_response_body_from_webmock_response(webmock_response)
32
35
 
33
36
  return new(status, "1.1", headers, body, uri) if HTTP::VERSION < "1.0.0"
34
37
 
@@ -52,7 +55,18 @@ module HTTP
52
55
  })
53
56
  end
54
57
 
55
- private
58
+ def build_http_rb_response_body_from_webmock_response(webmock_response)
59
+ if HTTP::VERSION < "1.0.0"
60
+ Body.new(Streamer.new(webmock_response.body))
61
+ elsif HTTP::VERSION < "3.0.0"
62
+ Body.new(Streamer.new(webmock_response.body), webmock_response.body.encoding)
63
+ else
64
+ Body.new(
65
+ Streamer.new(webmock_response.body, encoding: webmock_response.body.encoding),
66
+ encoding: webmock_response.body.encoding
67
+ )
68
+ end
69
+ end
56
70
 
57
71
  def normalize_uri(uri)
58
72
  return unless uri
@@ -63,5 +77,11 @@ module HTTP
63
77
  uri
64
78
  end
65
79
  end
80
+
81
+ private
82
+
83
+ def reset_body_to_allow_it_to_be_streamed!(webmock_response)
84
+ @body = self.class.build_http_rb_response_body_from_webmock_response(webmock_response)
85
+ end
66
86
  end
67
87
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HTTP
2
4
  class Response
3
5
  class Streamer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HTTP
2
4
  class WebMockPerform
3
5
  def initialize(request, options, &perform)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  begin
2
4
  require "http"
3
5
  rescue LoadError
@@ -29,9 +31,9 @@ if defined?(HTTP) && defined?(HTTP::VERSION)
29
31
  end
30
32
  end
31
33
 
32
- require "webmock/http_lib_adapters/http_rb/client"
33
- require "webmock/http_lib_adapters/http_rb/request"
34
- require "webmock/http_lib_adapters/http_rb/response"
35
- require "webmock/http_lib_adapters/http_rb/streamer"
36
- require "webmock/http_lib_adapters/http_rb/webmock"
34
+ require_relative "http_rb/client"
35
+ require_relative "http_rb/request"
36
+ require_relative "http_rb/response"
37
+ require_relative "http_rb/streamer"
38
+ require_relative "http_rb/webmock"
37
39
  end