webmock 3.36.0 → 4.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 (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +335 -0
  3. data/README.md +227 -6
  4. data/lib/webmock/api.rb +4 -0
  5. data/lib/webmock/callback_registry.rb +14 -7
  6. data/lib/webmock/errors.rb +1 -1
  7. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +18 -1
  8. data/lib/webmock/http_lib_adapters/curb_adapter.rb +292 -46
  9. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +11 -3
  10. data/lib/webmock/http_lib_adapters/excon_adapter.rb +23 -2
  11. data/lib/webmock/http_lib_adapters/http_rb/response.rb +3 -0
  12. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +92 -5
  13. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +127 -109
  14. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +1 -1
  15. data/lib/webmock/http_lib_adapters/net_http.rb +78 -10
  16. data/lib/webmock/http_lib_adapters/patron_adapter.rb +32 -13
  17. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +65 -7
  18. data/lib/webmock/json_body_hint.rb +119 -12
  19. data/lib/webmock/request_body_diff.rb +16 -6
  20. data/lib/webmock/request_execution_verifier.rb +19 -6
  21. data/lib/webmock/request_pattern.rb +178 -33
  22. data/lib/webmock/request_registry.rb +75 -5
  23. data/lib/webmock/request_signature.rb +44 -5
  24. data/lib/webmock/request_signature_snippet.rb +33 -4
  25. data/lib/webmock/request_stub.rb +32 -7
  26. data/lib/webmock/response.rb +42 -3
  27. data/lib/webmock/stub_registry.rb +7 -6
  28. data/lib/webmock/stub_request_snippet.rb +9 -3
  29. data/lib/webmock/trailing_slash_hint.rb +55 -0
  30. data/lib/webmock/util/hash_counter.rb +27 -8
  31. data/lib/webmock/util/hash_keys_stringifier.rb +23 -1
  32. data/lib/webmock/util/headers.rb +17 -0
  33. data/lib/webmock/util/message_string.rb +28 -0
  34. data/lib/webmock/util/parsers/json.rb +23 -54
  35. data/lib/webmock/util/upload_body.rb +63 -0
  36. data/lib/webmock/util/uri.rb +21 -12
  37. data/lib/webmock/util/values_stringifier.rb +54 -0
  38. data/lib/webmock/version.rb +1 -1
  39. data/lib/webmock/webmock.rb +35 -4
  40. data/lib/webmock.rb +3 -0
  41. metadata +10 -7
data/README.md CHANGED
@@ -23,7 +23,7 @@ Supported HTTP libraries
23
23
  ------------------------
24
24
 
25
25
  * [Async::HTTP::Client](https://github.com/socketry/async-http)
26
- * [Curb](https://github.com/taf2/curb) (currently only Curb::Easy)
26
+ * [Curb](https://github.com/taf2/curb) (`Curl::Easy` and `Curl::Multi`)
27
27
  * [EM-HTTP-Request](https://github.com/igrigorik/em-http-request)
28
28
  * [Excon](https://github.com/excon/excon)
29
29
  * [HTTPClient](https://github.com/nahi/httpclient)
@@ -38,15 +38,13 @@ Supported HTTP libraries
38
38
 
39
39
  Supported Ruby Interpreters
40
40
  ---------------------------
41
- * MRI 2.6
42
- * MRI 2.7
43
41
  * MRI 3.0
44
42
  * MRI 3.1
45
43
  * MRI 3.2
46
44
  * MRI 3.3
47
45
  * MRI 3.4
48
46
  * MRI 4.0
49
- * JRuby
47
+ * JRuby 9.4 or later
50
48
 
51
49
  ## Installation
52
50
 
@@ -74,6 +72,8 @@ rake install
74
72
 
75
73
  WebMock 2.x has changed somewhat since version 1.x. Changes are listed in [CHANGELOG.md](CHANGELOG.md)
76
74
 
75
+ ## Setup
76
+
77
77
  ### Cucumber
78
78
 
79
79
  Create a file `features/support/webmock.rb` with the following contents:
@@ -106,6 +106,48 @@ Add the following code to `test/test_helper.rb`
106
106
  require 'webmock/test_unit'
107
107
  ```
108
108
 
109
+ ### What the test framework integrations do
110
+
111
+ `webmock/rspec`, `webmock/minitest`, `webmock/test_unit` and `webmock/cucumber` do three things:
112
+
113
+ * They enable WebMock. `webmock/rspec` does it in a `before(:suite)` hook and disables WebMock again in `after(:suite)`. The others enable it as soon as they are required.
114
+ * They call `WebMock.reset!` after every example, test or scenario. This removes the stubs declared with `stub_request` and the history of requests, so a stub declared in RSpec's `before(:all)` or `before(:context)`, or in a minitest-hooks `before_all`, is gone after the first example. Declare such stubs in `before(:each)` or `setup` instead, or use [global stubs](#global-stubs), which `WebMock.reset!` keeps. If you override `teardown` in Minitest, call `super`, or WebMock's reset doesn't run.
115
+ * They leave the net connect settings alone. By default WebMock blocks real requests. `WebMock.allow_net_connect!` allows them all, and `WebMock.disable_net_connect!` blocks them again, except for what its options allow, such as `allow_localhost: true`. Each setting stays in effect for every later example until you change it, and each call to `disable_net_connect!` replaces the `allow`, `allow_localhost` and `net_http_connect_on_start` options given before.
116
+
117
+ Requiring one of these files in a spec helper or in any spec file enables WebMock for the whole run, not only for that file. To use WebMock only in some examples, require `webmock` and set it up yourself, for example for RSpec examples tagged with `:webmock`:
118
+
119
+ ```ruby
120
+ require 'webmock'
121
+ require 'webmock/rspec/matchers'
122
+
123
+ WebMock::AssertionFailure.error_class = RSpec::Expectations::ExpectationNotMetError
124
+
125
+ RSpec.configure do |config|
126
+ config.include WebMock::API, :webmock
127
+ config.include WebMock::Matchers, :webmock
128
+
129
+ config.around(:each, :webmock) do |example|
130
+ WebMock.enable!
131
+ WebMock.disable_net_connect!
132
+ example.run
133
+ ensure
134
+ WebMock.reset!
135
+ WebMock.disable!
136
+ end
137
+ end
138
+ ```
139
+
140
+ Examples without the tag then make real requests. Create HTTP clients inside each example: a client created while WebMock was disabled isn't intercepted, and a Curb or HTTPClient object created in a tagged example is still intercepted after it.
141
+
142
+ Enabling WebMock and allowing real requests are separate settings. `WebMock.enable!` and `WebMock.disable!` decide whether WebMock intercepts requests at all. `WebMock.disable_net_connect!` and `WebMock.allow_net_connect!` decide what happens to an intercepted request that no stub matches. While WebMock is disabled, it doesn't intercept requests, so these settings only take effect once it is enabled again.
143
+
144
+ For HTTP clients such as Net::HTTP, HTTPClient and Curb, WebMock intercepts requests by replacing their classes while it is enabled. `webmock/rspec` enables WebMock just before the first example, after your app and spec files have loaded. A class that inherits from `Net::HTTP`, or an HTTP client object such as a `Net::HTTP` or `HTTPClient` instance, created while they load uses the original class, and its requests reach the network. A client that creates its HTTP object for each request is not affected. If your app defines such a class or creates such an object while it loads, enable WebMock right after requiring it:
145
+
146
+ ```ruby
147
+ require 'webmock/rspec'
148
+ WebMock.enable!
149
+ ```
150
+
109
151
  ### Outside a test framework
110
152
 
111
153
  You can also use WebMock outside a test framework:
@@ -335,6 +377,23 @@ RestClient.get("http://www.example.com/?a=b") # ===> Failure
335
377
  RestClient.get("http://www.example.com/?a=c") # ===> Success
336
378
  ```
337
379
 
380
+ ### Matching repeated query params
381
+
382
+ By default WebMock reads a query the way Rack and Rails do, so a key repeated without brackets keeps only its last value: `?a=1&a=2` is read as `?a=2`. To match repeated keys, switch to the `:flat_array` notation and give the query as a string:
383
+
384
+ ```ruby
385
+ WebMock::Config.instance.query_values_notation = :flat_array
386
+
387
+ stub_request(:get, "www.example.com/?a=1&a=2")
388
+ # or
389
+ stub_request(:get, "www.example.com").with(query: "a=1&a=2")
390
+
391
+ RestClient.get("http://www.example.com/?a=1&a=2") # ===> Success
392
+ RestClient.get("http://www.example.com/?a=2") # ===> Failure
393
+ ```
394
+
395
+ The notation applies to every stub and request. Hashes can't describe repeated keys, so with `:flat_array`, `hash_including`, `hash_excluding`, query hashes given with a regexp or template URI, and form bodies given as a hash don't match. Set the notation only in the tests that need it, and set it back to `:subscript` afterwards.
396
+
338
397
  ### Stubbing with custom response
339
398
 
340
399
  ```ruby
@@ -351,6 +410,12 @@ Set appropriate Content-Type for HTTParty's `parsed_response`.
351
410
  stub_request(:any, "www.example.com").to_return body: '{}', headers: {content_type: 'application/json'}
352
411
  ```
353
412
 
413
+ For JSON, `to_return_json` sets it for you:
414
+
415
+ ```ruby
416
+ stub_request(:any, "www.example.com").to_return_json(body: {})
417
+ ```
418
+
354
419
  ### Response with body specified as IO object
355
420
 
356
421
  ```ruby
@@ -372,6 +437,8 @@ stub_request(:any, "www.example.com").
372
437
  Net::HTTP.get('www.example.com', '/') # ===> "{\"foo\": \"bar\"}"
373
438
  ```
374
439
 
440
+ `to_return_json` encodes the body and sets the `Content-Type` header to `application/json`, so there is no need to call `to_json` or set the header yourself. A `Content-Type` given in `headers` takes precedence.
441
+
375
442
  A `File` or `Pathname` body is returned as it is, so it should hold JSON that is already encoded:
376
443
 
377
444
  ```ruby
@@ -501,6 +568,20 @@ stub_request(:any, 'www.example.net').to_timeout
501
568
  RestClient.post('www.example.net', 'abc') # ===> RestClient::RequestTimeout
502
569
  ```
503
570
 
571
+ ### Curb multi requests
572
+
573
+ `Curl::Multi` matches each handle against stubs when it is added. An unstubbed
574
+ handle raises `WebMock::NetConnectNotAllowedError` from `multi.add` unless its
575
+ URL is allowed by WebMock's network settings. Stubbed handles complete when
576
+ `multi.perform` runs; allowed handles still run together on curb's native multi
577
+ loop. Both kinds of requests are recorded and invoke callbacks, including
578
+ `WebMock.after_request`. Callbacks registered with `real_requests_only: true`
579
+ receive only real transfers.
580
+
581
+ This also applies to `Curl::Multi.get`, `post`, `put` and `http`, and the `Curl.get`
582
+ and `Curl.post` convenience methods. Create the multi and easy handles after
583
+ WebMock is enabled, as with `Curl::Easy` requests.
584
+
504
585
  ### Multiple responses for repeated requests
505
586
 
506
587
  ```ruby
@@ -650,6 +731,26 @@ RestClient.get('bbc.com', '/') # ===> Allowed
650
731
  RestClient.get('www.bbc.com', '/') # ===> Failure
651
732
  ```
652
733
 
734
+ ### Real requests can be allowed or disabled only within a block
735
+
736
+ Given a block, `WebMock.allow_net_connect!` and `WebMock.disable_net_connect!` apply their settings only while the block runs and return the block's value. When the block ends, even with an exception, WebMock restores the settings from before the call.
737
+
738
+ ```ruby
739
+ WebMock.disable_net_connect!
740
+
741
+ WebMock.allow_net_connect! do
742
+ Net::HTTP.get('www.something.com', '/') # ===> /.+Something.+/
743
+ end
744
+
745
+ WebMock.disable_net_connect!(allow: 'www.example.org') do
746
+ RestClient.get('www.example.org', '/') # ===> Allowed
747
+ end
748
+
749
+ Net::HTTP.get('www.something.com', '/') # ===> Failure
750
+ ```
751
+
752
+ Settings changed inside the block, for example by another call to `WebMock.disable_net_connect!`, are undone as well. The settings are global, so other threads see them while the block runs, and blocks that overlap in different threads can restore each other's settings in the wrong order. Use the block form only where tests run one at a time in each process.
753
+
653
754
  ## Connecting on Net::HTTP.start
654
755
 
655
756
  HTTP protocol has 3 steps: connect, request and response (or 4 with close). Most Ruby HTTP client libraries
@@ -726,6 +827,8 @@ assert_requested(stub_get)
726
827
  assert_not_requested(stub_post)
727
828
  ```
728
829
 
830
+ See [What an expectation on a stub counts](#what-an-expectation-on-a-stub-counts) for which requests these assertions count.
831
+
729
832
 
730
833
  ### Setting expectations in RSpec on `WebMock` module
731
834
  This style is borrowed from [fakeweb-matcher](http://github.com/pat/fakeweb-matcher)
@@ -802,9 +905,59 @@ expect(stub).to have_been_requested.once.with(body: {"a" => [4, 5, 6]})
802
905
 
803
906
  Options and a block given to `with` take the same form as in `stub_request(...).with`. The expectation then counts only the requests that match both the stub and those conditions. The stub itself does not change.
804
907
 
908
+ ### What an expectation on a stub counts
909
+
910
+ An expectation on a stub, such as `assert_requested(stub)` or `expect(stub).to have_been_requested`, counts the requests since the last `WebMock.reset!` or `WebMock.reset_executed_requests!` that match the stub's pattern. A matching request counts even if another stub answered it, if it was made before the stub was declared, or if it was made after the stub was removed with `remove_request_stub`.
911
+
912
+ ```ruby
913
+ stub_any = stub_request(:post, "www.example.com")
914
+ stub_body = stub_request(:post, "www.example.com").with(body: "abc")
915
+
916
+ Net::HTTP.post(URI("http://www.example.com"), "abc") # answered by stub_body
917
+
918
+ expect(stub_body).to have_been_requested.once # ===> Success
919
+ expect(stub_any).to have_been_requested.once # ===> Success, as its pattern matches too
920
+ ```
921
+
922
+ To make an expectation stricter, give the stub its own conditions, such as `with(body: "")` for requests without a body. In RSpec you can also add them to the expectation with `with`, as shown above. With `assert_requested`, pass the method and URL instead of the stub, for example `assert_requested(:post, "www.example.com", body: "")`. To count from zero, call `WebMock.reset_executed_requests!`.
923
+
924
+ WebMock may run a `with` block once for several identical requests and multiply the count, so keep such blocks free of side effects. To look at every request, use [`executed_requests`](#inspecting-the-requests-made).
925
+
926
+ ### Inspecting the requests made
927
+
928
+ `WebMock.executed_requests` lists every request made since the last `WebMock.reset!` or `WebMock.reset_executed_requests!`, in the order they were made, including repeats. It lets you check what your code sent with plain Ruby.
929
+
930
+ ```ruby
931
+ stub_request(:post, "www.example.com/orders")
932
+
933
+ Net::HTTP.post(URI("http://www.example.com/orders"), '{"name":"John"}', "Content-Type" => "application/json")
934
+
935
+ request = WebMock.executed_requests.last
936
+ request.method # ===> :post
937
+ request.uri.path # ===> "/orders"
938
+ request.headers["Content-Type"] # ===> "application/json"
939
+ JSON.parse(request.body) # ===> {"name" => "John"}
940
+ ```
941
+
942
+ In RSpec, Minitest and Test::Unit you can call it as `executed_requests`, and it starts empty in every test. For example, to check which requests were made and in what order:
943
+
944
+ ```ruby
945
+ expect(executed_requests.map { |request| request.uri.path }).to eq(["/orders", "/orders/1/confirm"])
946
+ ```
947
+
948
+ A few things to know:
949
+
950
+ * The URI is normalized, with the port added and the query sorted, so compare its parts, such as `uri.host` or `uri.query_values`, rather than `uri.to_s`.
951
+ * The requests are copies, so changing them doesn't change what WebMock recorded.
952
+ * When a Curb, Patron or em-http-request request uploads a file or stream, its body is the uploaded bytes. WebMock reads them without moving the stream, from regular files, tempfiles, `StringIO` and gzip readers over those. Other streams, such as pipes, are listed with the stream's `to_s`.
953
+ * Multipart fields of a Curb PUT are listed with their `name`, `content`, `content_type`, `local_file` and `remote_file`. Files in a Typhoeus body given as an array are listed as `Pathname` objects.
954
+ * With these three clients, blocks given to `stub_request(...).with` and `after_request` callbacks still receive the original file or stream, not the bytes. With other clients, such as Net::HTTP and Excon, the stream is read when the request is made, and blocks and callbacks receive the bytes.
955
+ * An expectation on a stub, such as `assert_requested(stub)`, reuses the decision the stub made when the upload happened, so a stub block that checks the stream gives the same result in the expectation. If the stub never saw the request, because another stub answered first or the stub was declared later, the expectation checks the uploaded bytes, as `a_request(...)` and conditions given to the expectation's `with` always do.
956
+ * Depending on the HTTP client, requests that WebMock blocked because real connections are disabled may be listed too.
957
+
805
958
  ## Clearing stubs and request history
806
959
 
807
- If you want to reset all current stubs and history of requests use `WebMock.reset!`
960
+ If you want to reset all current stubs and history of requests use `WebMock.reset!`. [Global stubs](#global-stubs) stay.
808
961
 
809
962
  ```ruby
810
963
  stub_request(:any, "www.example.com")
@@ -820,7 +973,7 @@ assert_not_requested :get, "www.example.com" # ===> Success
820
973
 
821
974
  ## Clearing request counters
822
975
 
823
- If you want to reset **only** the counters of the executed requests use `WebMock.reset_executed_requests!`
976
+ If you want to reset **only** the counters and the list of executed requests, and keep the stubs, use `WebMock.reset_executed_requests!`
824
977
 
825
978
  ```ruby
826
979
  stub = stub_request(:get, "www.example.com")
@@ -840,6 +993,28 @@ expect(stub).not_to have_been_requested
840
993
  expect(stub2).not_to have_been_requested
841
994
  ```
842
995
 
996
+ ## Global stubs
997
+
998
+ `WebMock.reset!` removes the stubs declared with `stub_request`, and `webmock/rspec`, `webmock/minitest`, `webmock/test_unit` and `webmock/cucumber` call it after every test or scenario. To answer some requests in every test, for example calls to a metrics service, declare a global stub once when the test suite starts:
999
+
1000
+ ```ruby
1001
+ WebMock.globally_stub_request do |request|
1002
+ { status: 200, body: "{}" } if request.uri.host == "metrics.example.com"
1003
+ end
1004
+ ```
1005
+
1006
+ WebMock calls the block with the request when it reaches the global stub. The block returns a response hash with the same keys as `to_return`, or `nil` to let other stubs handle the request. `WebMock.reset!` doesn't remove global stubs, and there is no way to remove only one, so declare each of them once, for example in RSpec's `before(:suite)`.
1007
+
1008
+ Global stubs are checked before the stubs declared with `stub_request`. To check a global stub after them instead, so a test can override it with its own stubs, declare it with `:after_local_stubs`:
1009
+
1010
+ ```ruby
1011
+ WebMock.globally_stub_request(:after_local_stubs) do |request|
1012
+ { status: 200, body: "{}" } if request.uri.host == "metrics.example.com"
1013
+ end
1014
+ ```
1015
+
1016
+ Requests answered by a global stub are recorded like any other, so `a_request`, `assert_requested` and `executed_requests` include them.
1017
+
843
1018
  ## Disabling and enabling WebMock or only some http client adapters
844
1019
 
845
1020
  ```ruby
@@ -877,6 +1052,8 @@ stub_request(:get, "www.example.com").to_return(body: "def")
877
1052
  Net::HTTP.get('www.example.com', '/') # ====> "def"
878
1053
  ```
879
1054
 
1055
+ [Global stubs](#global-stubs) are checked before these stubs, or after them when declared with `:after_local_stubs`.
1056
+
880
1057
  ## Matching URIs
881
1058
 
882
1059
  WebMock will match all different representations of the same URI.
@@ -981,6 +1158,20 @@ WebMock.after_request(except: [:patron],
981
1158
  end
982
1159
  ```
983
1160
 
1161
+ #### Callbacks and Net::HTTP streaming
1162
+
1163
+ A callback that runs for real Net::HTTP requests makes WebMock read the whole response body before it calls the block given to `request`, so the callback gets all of it. Without such a callback, the block gets the response while Net::HTTP reads it, as without WebMock, so it can read the body in chunks. WebMock decides this when the request is sent, so a callback registered while a request with a block is in flight doesn't get that response. Callbacks stay registered across tests, so one added in an earlier test turns streaming off in later tests too. To keep a callback away from Net::HTTP, register it with `except: [:net_http]`.
1164
+
1165
+ #### Removing callbacks
1166
+
1167
+ Callbacks stay registered after `WebMock.reset!`, so a callback added in one test keeps running in later ones. To remove all callbacks, call:
1168
+
1169
+ ```ruby
1170
+ WebMock.reset_callbacks
1171
+ ```
1172
+
1173
+ This also removes callbacks registered by other libraries, such as VCR, so prefer registering your callbacks once, for example in your test helper.
1174
+
984
1175
  ## Bugs and Issues
985
1176
 
986
1177
  Please submit them here [http://github.com/bblimke/webmock/issues](http://github.com/bblimke/webmock/issues)
@@ -1283,6 +1474,36 @@ People who submitted patches and new features or suggested improvements. Many th
1283
1474
  * Erik Berlin
1284
1475
  * Josch Bockler
1285
1476
  * Leslie Hoare
1477
+ * Chris Gunther
1478
+ * hikmetba-bit
1479
+ * Daniel Pepper
1480
+ * Christian Bruckmayer
1481
+ * Chris
1482
+ * Jan Graichen
1483
+ * Olivier Brisse
1484
+ * Henry Blyth
1485
+ * Sébastien S.
1486
+ * Euisan Kim
1487
+ * Alex
1488
+ * Andy Williams
1489
+ * Tim Mertens
1490
+ * Mike Taylor
1491
+ * James Rodovich
1492
+ * Michael Spiz
1493
+ * Tom Brammar
1494
+ * Benoit Tigeot
1495
+ * nightpool
1496
+ * Andrii Furmanets
1497
+ * thoran
1498
+ * Julik Tarkhanov
1499
+ * Bob Aman
1500
+ * AmeenKhan12345
1501
+ * Andrew K. Fader
1502
+ * Yang Bai
1503
+ * Hamza Tarar
1504
+ * John Gallagher
1505
+ * Yegor Bugayenko
1506
+ * Todd Fisher
1286
1507
 
1287
1508
  For a full list of contributors you can visit the
1288
1509
  [contributors](https://github.com/bblimke/webmock/contributors) page.
data/lib/webmock/api.rb CHANGED
@@ -72,6 +72,10 @@ module WebMock
72
72
  WebMock::RequestRegistry.instance.reset!
73
73
  end
74
74
 
75
+ def executed_requests
76
+ WebMock::RequestRegistry.instance.executed_requests
77
+ end
78
+
75
79
  private
76
80
 
77
81
  def convert_uri_method_and_options_to_request_and_options(method, uri, options, &block)
@@ -15,16 +15,23 @@ module WebMock
15
15
  def self.invoke_callbacks(options, request_signature, response)
16
16
  return if @@callbacks.empty?
17
17
  CallbackRegistry.callbacks.each do |callback|
18
- except = callback[:options][:except]
19
- real_only = callback[:options][:real_requests_only]
20
- unless except && except.include?(options[:lib])
21
- if !real_only || options[:real_request]
22
- callback[:block].call(request_signature, response)
23
- end
24
- end
18
+ callback[:block].call(request_signature, response) if runs_for?(callback, options)
25
19
  end
26
20
  end
27
21
 
22
+ # Whether invoke_callbacks would run any callback for a request with
23
+ # these options, given each callback's except and real_requests_only.
24
+ def self.any_callbacks_for?(options)
25
+ @@callbacks.any? { |callback| runs_for?(callback, options) }
26
+ end
27
+
28
+ def self.runs_for?(callback, options)
29
+ except = callback[:options][:except]
30
+ real_only = callback[:options][:real_requests_only]
31
+ !(except && except.include?(options[:lib])) && (!real_only || options[:real_request])
32
+ end
33
+ private_class_method :runs_for?
34
+
28
35
  def self.reset
29
36
  @@callbacks = []
30
37
  end
@@ -10,7 +10,7 @@ module WebMock
10
10
  request_signature_snippet.stubbing_instructions,
11
11
  request_signature_snippet.request_stubs,
12
12
  "="*60
13
- ].compact.join("\n\n")
13
+ ].compact.map { |part| Util::MessageString.utf8(part) }.join("\n\n")
14
14
  super(text)
15
15
  end
16
16
 
@@ -66,7 +66,7 @@ if defined?(Async::HTTP)
66
66
  request.authority ||= self.authority
67
67
 
68
68
  request_signature = build_request_signature(request)
69
- WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
69
+ WebMock::RequestRegistry.instance.put(request_signature)
70
70
  webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
71
71
  net_connect_allowed = WebMock.net_connect_allowed?(request_signature.uri)
72
72
  real_request = false
@@ -103,6 +103,23 @@ if defined?(Async::HTTP)
103
103
  @webmock_client.close
104
104
  end
105
105
 
106
+ # Some libraries, such as async-websocket, take a connection from the
107
+ # pool instead of calling #call. WebMock can't see or stub what they
108
+ # send, so hand out the real pool only when WebMock's own settings
109
+ # allow real connections to the host the pool connects to: the
110
+ # endpoint's, whatever authority the requests name, with a ws or wss
111
+ # endpoint checked as http or https. VCR can't record or replay such
112
+ # connections, so it can't allow them.
113
+ def pool
114
+ host = @endpoint.hostname
115
+ host = "[#{host}]" if host.include?(":") && !host.start_with?("[")
116
+ uri = Addressable::URI.new(scheme: @endpoint.secure? ? "https" : "http", host: host, port: @endpoint.port, path: "/")
117
+ unless WebMock.net_connect_allowed_by_settings?(uri)
118
+ raise WebMock::NetConnectNotAllowedError.new(WebMock::RequestSignature.new(:get, uri))
119
+ end
120
+ @network_client.pool
121
+ end
122
+
106
123
  private
107
124
 
108
125
  def build_request_signature(request)