webmock 3.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/.gitignore +34 -0
- data/.rspec-tm +2 -0
- data/.travis.yml +19 -0
- data/CHANGELOG.md +1698 -0
- data/Gemfile +9 -0
- data/LICENSE +20 -0
- data/README.md +1125 -0
- data/Rakefile +28 -0
- data/lib/webmock.rb +59 -0
- data/lib/webmock/api.rb +109 -0
- data/lib/webmock/assertion_failure.rb +11 -0
- data/lib/webmock/callback_registry.rb +35 -0
- data/lib/webmock/config.rb +18 -0
- data/lib/webmock/cucumber.rb +10 -0
- data/lib/webmock/deprecation.rb +9 -0
- data/lib/webmock/errors.rb +17 -0
- data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +214 -0
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +347 -0
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +228 -0
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +162 -0
- data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +7 -0
- data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +19 -0
- data/lib/webmock/http_lib_adapters/http_rb/client.rb +14 -0
- data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +43 -0
- data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
- data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
- data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +242 -0
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +130 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +361 -0
- data/lib/webmock/http_lib_adapters/net_http_response.rb +34 -0
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +130 -0
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +174 -0
- data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
- data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
- data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
- data/lib/webmock/matchers/hash_including_matcher.rb +17 -0
- data/lib/webmock/minitest.rb +41 -0
- data/lib/webmock/rack_response.rb +69 -0
- data/lib/webmock/request_body_diff.rb +64 -0
- data/lib/webmock/request_execution_verifier.rb +77 -0
- data/lib/webmock/request_pattern.rb +370 -0
- data/lib/webmock/request_registry.rb +35 -0
- data/lib/webmock/request_signature.rb +54 -0
- data/lib/webmock/request_signature_snippet.rb +61 -0
- data/lib/webmock/request_stub.rb +100 -0
- data/lib/webmock/response.rb +153 -0
- data/lib/webmock/responses_sequence.rb +40 -0
- data/lib/webmock/rspec.rb +41 -0
- data/lib/webmock/rspec/matchers.rb +27 -0
- data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +78 -0
- data/lib/webmock/rspec/matchers/webmock_matcher.rb +67 -0
- data/lib/webmock/stub_registry.rb +67 -0
- data/lib/webmock/stub_request_snippet.rb +38 -0
- data/lib/webmock/test_unit.rb +22 -0
- data/lib/webmock/util/hash_counter.rb +39 -0
- data/lib/webmock/util/hash_keys_stringifier.rb +25 -0
- data/lib/webmock/util/hash_validator.rb +17 -0
- data/lib/webmock/util/headers.rb +64 -0
- data/lib/webmock/util/json.rb +67 -0
- data/lib/webmock/util/query_mapper.rb +281 -0
- data/lib/webmock/util/uri.rb +110 -0
- data/lib/webmock/util/values_stringifier.rb +20 -0
- data/lib/webmock/util/version_checker.rb +111 -0
- data/lib/webmock/version.rb +3 -0
- data/lib/webmock/webmock.rb +161 -0
- data/minitest/test_helper.rb +34 -0
- data/minitest/test_webmock.rb +9 -0
- data/minitest/webmock_spec.rb +60 -0
- data/spec/acceptance/async_http_client/async_http_client_spec.rb +349 -0
- data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
- data/spec/acceptance/curb/curb_spec.rb +492 -0
- data/spec/acceptance/curb/curb_spec_helper.rb +147 -0
- data/spec/acceptance/em_http_request/em_http_request_spec.rb +406 -0
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +77 -0
- data/spec/acceptance/excon/excon_spec.rb +77 -0
- data/spec/acceptance/excon/excon_spec_helper.rb +50 -0
- data/spec/acceptance/http_rb/http_rb_spec.rb +82 -0
- data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
- data/spec/acceptance/httpclient/httpclient_spec.rb +217 -0
- data/spec/acceptance/httpclient/httpclient_spec_helper.rb +57 -0
- data/spec/acceptance/manticore/manticore_spec.rb +56 -0
- data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
- data/spec/acceptance/net_http/net_http_shared.rb +153 -0
- data/spec/acceptance/net_http/net_http_spec.rb +331 -0
- data/spec/acceptance/net_http/net_http_spec_helper.rb +64 -0
- data/spec/acceptance/net_http/real_net_http_spec.rb +20 -0
- data/spec/acceptance/patron/patron_spec.rb +125 -0
- data/spec/acceptance/patron/patron_spec_helper.rb +54 -0
- data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +313 -0
- data/spec/acceptance/shared/callbacks.rb +148 -0
- data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +36 -0
- data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +95 -0
- data/spec/acceptance/shared/precedence_of_stubs.rb +15 -0
- data/spec/acceptance/shared/request_expectations.rb +930 -0
- data/spec/acceptance/shared/returning_declared_responses.rb +409 -0
- data/spec/acceptance/shared/stubbing_requests.rb +643 -0
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +135 -0
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +60 -0
- data/spec/acceptance/webmock_shared.rb +41 -0
- data/spec/fixtures/test.txt +1 -0
- data/spec/quality_spec.rb +84 -0
- data/spec/spec_helper.rb +48 -0
- data/spec/support/example_curl_output.txt +22 -0
- data/spec/support/failures.rb +9 -0
- data/spec/support/my_rack_app.rb +53 -0
- data/spec/support/network_connection.rb +19 -0
- data/spec/support/webmock_server.rb +70 -0
- data/spec/unit/api_spec.rb +175 -0
- data/spec/unit/errors_spec.rb +129 -0
- data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +17 -0
- data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +12 -0
- data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
- data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
- data/spec/unit/rack_response_spec.rb +112 -0
- data/spec/unit/request_body_diff_spec.rb +90 -0
- data/spec/unit/request_execution_verifier_spec.rb +208 -0
- data/spec/unit/request_pattern_spec.rb +601 -0
- data/spec/unit/request_registry_spec.rb +95 -0
- data/spec/unit/request_signature_snippet_spec.rb +89 -0
- data/spec/unit/request_signature_spec.rb +155 -0
- data/spec/unit/request_stub_spec.rb +199 -0
- data/spec/unit/response_spec.rb +282 -0
- data/spec/unit/stub_registry_spec.rb +103 -0
- data/spec/unit/stub_request_snippet_spec.rb +115 -0
- data/spec/unit/util/hash_counter_spec.rb +39 -0
- data/spec/unit/util/hash_keys_stringifier_spec.rb +27 -0
- data/spec/unit/util/headers_spec.rb +28 -0
- data/spec/unit/util/json_spec.rb +33 -0
- data/spec/unit/util/query_mapper_spec.rb +157 -0
- data/spec/unit/util/uri_spec.rb +361 -0
- data/spec/unit/util/version_checker_spec.rb +65 -0
- data/spec/unit/webmock_spec.rb +19 -0
- data/test/http_request.rb +24 -0
- data/test/shared_test.rb +108 -0
- data/test/test_helper.rb +23 -0
- data/test/test_webmock.rb +6 -0
- data/webmock.gemspec +45 -0
- metadata +496 -0
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
6
|
+
t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
|
7
|
+
t.pattern = 'spec/**/*_spec.rb'
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:spec_http_without_webmock) do |t|
|
11
|
+
t.rspec_opts = ["-c", "-f progress", "-r ./spec/acceptance/net_http/real_net_http_spec.rb"]
|
12
|
+
t.pattern = 'spec/acceptance/net_http/real_net_http_spec.rb'
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'rake/testtask'
|
16
|
+
Rake::TestTask.new(:test) do |test|
|
17
|
+
test.test_files = FileList["test/**/*.rb"].exclude("test/test_helper.rb")
|
18
|
+
test.verbose = false
|
19
|
+
test.warning = false
|
20
|
+
end
|
21
|
+
|
22
|
+
Rake::TestTask.new(:minitest) do |test|
|
23
|
+
test.test_files = FileList["minitest/**/*.rb"].exclude("test/test_helper.rb")
|
24
|
+
test.verbose = false
|
25
|
+
test.warning = false
|
26
|
+
end
|
27
|
+
|
28
|
+
task default: [:spec, :spec_http_without_webmock, :test, :minitest]
|
data/lib/webmock.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
require 'addressable/uri'
|
4
|
+
require 'addressable/template'
|
5
|
+
require 'crack/xml'
|
6
|
+
|
7
|
+
require_relative 'webmock/deprecation'
|
8
|
+
require_relative 'webmock/version'
|
9
|
+
|
10
|
+
require_relative 'webmock/errors'
|
11
|
+
|
12
|
+
require_relative 'webmock/util/query_mapper'
|
13
|
+
require_relative 'webmock/util/uri'
|
14
|
+
require_relative 'webmock/util/headers'
|
15
|
+
require_relative 'webmock/util/hash_counter'
|
16
|
+
require_relative 'webmock/util/hash_keys_stringifier'
|
17
|
+
require_relative 'webmock/util/values_stringifier'
|
18
|
+
require_relative 'webmock/util/json'
|
19
|
+
require_relative 'webmock/util/version_checker'
|
20
|
+
require_relative 'webmock/util/hash_validator'
|
21
|
+
|
22
|
+
require_relative 'webmock/matchers/hash_argument_matcher'
|
23
|
+
require_relative 'webmock/matchers/hash_excluding_matcher'
|
24
|
+
require_relative 'webmock/matchers/hash_including_matcher'
|
25
|
+
require_relative 'webmock/matchers/any_arg_matcher'
|
26
|
+
|
27
|
+
require_relative 'webmock/request_pattern'
|
28
|
+
require_relative 'webmock/request_signature'
|
29
|
+
require_relative 'webmock/responses_sequence'
|
30
|
+
require_relative 'webmock/request_stub'
|
31
|
+
require_relative 'webmock/response'
|
32
|
+
require_relative 'webmock/rack_response'
|
33
|
+
|
34
|
+
require_relative 'webmock/stub_request_snippet'
|
35
|
+
require_relative 'webmock/request_signature_snippet'
|
36
|
+
require_relative 'webmock/request_body_diff'
|
37
|
+
|
38
|
+
require_relative 'webmock/assertion_failure'
|
39
|
+
require_relative 'webmock/request_execution_verifier'
|
40
|
+
require_relative 'webmock/config'
|
41
|
+
require_relative 'webmock/callback_registry'
|
42
|
+
require_relative 'webmock/request_registry'
|
43
|
+
require_relative 'webmock/stub_registry'
|
44
|
+
require_relative 'webmock/api'
|
45
|
+
|
46
|
+
require_relative 'webmock/http_lib_adapters/http_lib_adapter_registry'
|
47
|
+
require_relative 'webmock/http_lib_adapters/http_lib_adapter'
|
48
|
+
require_relative 'webmock/http_lib_adapters/net_http'
|
49
|
+
require_relative 'webmock/http_lib_adapters/http_rb_adapter'
|
50
|
+
require_relative 'webmock/http_lib_adapters/httpclient_adapter'
|
51
|
+
require_relative 'webmock/http_lib_adapters/patron_adapter'
|
52
|
+
require_relative 'webmock/http_lib_adapters/curb_adapter'
|
53
|
+
require_relative 'webmock/http_lib_adapters/em_http_request_adapter'
|
54
|
+
require_relative 'webmock/http_lib_adapters/typhoeus_hydra_adapter'
|
55
|
+
require_relative 'webmock/http_lib_adapters/excon_adapter'
|
56
|
+
require_relative 'webmock/http_lib_adapters/manticore_adapter'
|
57
|
+
require_relative 'webmock/http_lib_adapters/async_http_client_adapter'
|
58
|
+
|
59
|
+
require_relative 'webmock/webmock'
|
data/lib/webmock/api.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
module WebMock
|
2
|
+
module API
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def stub_request(method, uri)
|
6
|
+
WebMock::StubRegistry.instance.
|
7
|
+
register_request_stub(WebMock::RequestStub.new(method, uri))
|
8
|
+
end
|
9
|
+
|
10
|
+
alias_method :stub_http_request, :stub_request
|
11
|
+
|
12
|
+
def a_request(method, uri)
|
13
|
+
WebMock::RequestPattern.new(method, uri)
|
14
|
+
end
|
15
|
+
|
16
|
+
class << self
|
17
|
+
alias :request :a_request
|
18
|
+
end
|
19
|
+
|
20
|
+
def assert_requested(*args, &block)
|
21
|
+
if not args[0].is_a?(WebMock::RequestStub)
|
22
|
+
args = convert_uri_method_and_options_to_request_and_options(args[0], args[1], args[2], &block)
|
23
|
+
elsif block
|
24
|
+
raise ArgumentError, "assert_requested with a stub object, doesn't accept blocks"
|
25
|
+
end
|
26
|
+
assert_request_requested(*args)
|
27
|
+
end
|
28
|
+
|
29
|
+
def assert_not_requested(*args, &block)
|
30
|
+
if not args[0].is_a?(WebMock::RequestStub)
|
31
|
+
args = convert_uri_method_and_options_to_request_and_options(args[0], args[1], args[2], &block)
|
32
|
+
elsif block
|
33
|
+
raise ArgumentError, "assert_not_requested with a stub object, doesn't accept blocks"
|
34
|
+
end
|
35
|
+
assert_request_not_requested(*args)
|
36
|
+
end
|
37
|
+
alias refute_requested assert_not_requested
|
38
|
+
|
39
|
+
# Similar to RSpec::Mocks::ArgumentMatchers#hash_including()
|
40
|
+
#
|
41
|
+
# Matches a hash that includes the specified key(s) or key/value pairs.
|
42
|
+
# Ignores any additional keys.
|
43
|
+
#
|
44
|
+
# @example
|
45
|
+
#
|
46
|
+
# object.should_receive(:message).with(hash_including(:key => val))
|
47
|
+
# object.should_receive(:message).with(hash_including(:key))
|
48
|
+
# object.should_receive(:message).with(hash_including(:key, :key2 => val2))
|
49
|
+
def hash_including(*args)
|
50
|
+
if defined?(super)
|
51
|
+
super
|
52
|
+
else
|
53
|
+
WebMock::Matchers::HashIncludingMatcher.new(anythingize_lonely_keys(*args))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def hash_excluding(*args)
|
58
|
+
if defined?(super)
|
59
|
+
super
|
60
|
+
else
|
61
|
+
WebMock::Matchers::HashExcludingMatcher.new(anythingize_lonely_keys(*args))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def remove_request_stub(stub)
|
66
|
+
WebMock::StubRegistry.instance.remove_request_stub(stub)
|
67
|
+
end
|
68
|
+
|
69
|
+
def reset_executed_requests!
|
70
|
+
WebMock::RequestRegistry.instance.reset!
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def convert_uri_method_and_options_to_request_and_options(method, uri, options, &block)
|
76
|
+
options ||= {}
|
77
|
+
options_for_pattern = options.dup
|
78
|
+
[:times, :at_least_times, :at_most_times].each { |key| options_for_pattern.delete(key) }
|
79
|
+
request = WebMock::RequestPattern.new(method, uri, options_for_pattern)
|
80
|
+
request = request.with(&block) if block
|
81
|
+
[request, options]
|
82
|
+
end
|
83
|
+
|
84
|
+
def assert_request_requested(request, options = {})
|
85
|
+
times = options.delete(:times)
|
86
|
+
at_least_times = options.delete(:at_least_times)
|
87
|
+
at_most_times = options.delete(:at_most_times)
|
88
|
+
times = 1 if times.nil? && at_least_times.nil? && at_most_times.nil?
|
89
|
+
verifier = WebMock::RequestExecutionVerifier.new(request, times, at_least_times, at_most_times)
|
90
|
+
WebMock::AssertionFailure.failure(verifier.failure_message) unless verifier.matches?
|
91
|
+
end
|
92
|
+
|
93
|
+
def assert_request_not_requested(request, options = {})
|
94
|
+
times = options.delete(:times)
|
95
|
+
at_least_times = options.delete(:at_least_times)
|
96
|
+
at_most_times = options.delete(:at_most_times)
|
97
|
+
verifier = WebMock::RequestExecutionVerifier.new(request, times, at_least_times, at_most_times)
|
98
|
+
WebMock::AssertionFailure.failure(verifier.failure_message_when_negated) unless verifier.does_not_match?
|
99
|
+
end
|
100
|
+
|
101
|
+
#this is a based on RSpec::Mocks::ArgumentMatchers#anythingize_lonely_keys
|
102
|
+
def anythingize_lonely_keys(*args)
|
103
|
+
hash = args.last.class == Hash ? args.delete_at(-1) : {}
|
104
|
+
args.each { | arg | hash[arg] = WebMock::Matchers::AnyArgMatcher.new(nil) }
|
105
|
+
hash
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module WebMock
|
2
|
+
class CallbackRegistry
|
3
|
+
@@callbacks = []
|
4
|
+
|
5
|
+
def self.add_callback(options, block)
|
6
|
+
@@callbacks << {options: options, block: block}
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.callbacks
|
10
|
+
@@callbacks
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.invoke_callbacks(options, request_signature, response)
|
14
|
+
return if @@callbacks.empty?
|
15
|
+
CallbackRegistry.callbacks.each do |callback|
|
16
|
+
except = callback[:options][:except]
|
17
|
+
real_only = callback[:options][:real_requests_only]
|
18
|
+
unless except && except.include?(options[:lib])
|
19
|
+
if !real_only || options[:real_request]
|
20
|
+
callback[:block].call(request_signature, response)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.reset
|
27
|
+
@@callbacks = []
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.any_callbacks?
|
31
|
+
!@@callbacks.empty?
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module WebMock
|
2
|
+
class Config
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@show_stubbing_instructions = true
|
7
|
+
@show_body_diff = true
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_accessor :allow_net_connect
|
11
|
+
attr_accessor :allow_localhost
|
12
|
+
attr_accessor :allow
|
13
|
+
attr_accessor :net_http_connect_on_start
|
14
|
+
attr_accessor :show_stubbing_instructions
|
15
|
+
attr_accessor :query_values_notation
|
16
|
+
attr_accessor :show_body_diff
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module WebMock
|
2
|
+
|
3
|
+
class NetConnectNotAllowedError < Exception
|
4
|
+
def initialize(request_signature)
|
5
|
+
request_signature_snippet = RequestSignatureSnippet.new(request_signature)
|
6
|
+
text = [
|
7
|
+
"Real HTTP connections are disabled. Unregistered request: #{request_signature}",
|
8
|
+
request_signature_snippet.stubbing_instructions,
|
9
|
+
request_signature_snippet.request_stubs,
|
10
|
+
"="*60
|
11
|
+
].compact.join("\n\n")
|
12
|
+
super(text)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
begin
|
2
|
+
require 'async'
|
3
|
+
require 'async/http'
|
4
|
+
rescue LoadError
|
5
|
+
# async-http not found
|
6
|
+
end
|
7
|
+
|
8
|
+
if defined?(Async::HTTP)
|
9
|
+
module WebMock
|
10
|
+
module HttpLibAdapters
|
11
|
+
class AsyncHttpClientAdapter < HttpLibAdapter
|
12
|
+
adapter_for :async_http_client
|
13
|
+
|
14
|
+
OriginalAsyncHttpClient = Async::HTTP::Client unless const_defined?(:OriginalAsyncHttpClient)
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def enable!
|
18
|
+
Async::HTTP.send(:remove_const, :Client)
|
19
|
+
Async::HTTP.send(:const_set, :Client, Async::HTTP::WebMockClientWrapper)
|
20
|
+
end
|
21
|
+
|
22
|
+
def disable!
|
23
|
+
Async::HTTP.send(:remove_const, :Client)
|
24
|
+
Async::HTTP.send(:const_set, :Client, OriginalAsyncHttpClient)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module Async
|
32
|
+
module HTTP
|
33
|
+
class WebMockClientWrapper < Client
|
34
|
+
def initialize(
|
35
|
+
endpoint,
|
36
|
+
protocol = endpoint.protocol,
|
37
|
+
scheme = endpoint.scheme,
|
38
|
+
authority = endpoint.authority,
|
39
|
+
options = {}
|
40
|
+
)
|
41
|
+
webmock_endpoint = WebMockEndpoint.new(scheme, authority, protocol)
|
42
|
+
|
43
|
+
@network_client = WebMockClient.new(endpoint, protocol, scheme, authority, options)
|
44
|
+
@webmock_client = WebMockClient.new(webmock_endpoint, protocol, scheme, authority, options)
|
45
|
+
|
46
|
+
@scheme = scheme
|
47
|
+
@authority = authority
|
48
|
+
end
|
49
|
+
|
50
|
+
def call(request)
|
51
|
+
request.scheme ||= self.scheme
|
52
|
+
request.authority ||= self.authority
|
53
|
+
|
54
|
+
request_signature = build_request_signature(request)
|
55
|
+
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
56
|
+
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
57
|
+
net_connect_allowed = WebMock.net_connect_allowed?(request_signature.uri)
|
58
|
+
|
59
|
+
if webmock_response
|
60
|
+
webmock_response.raise_error_if_any
|
61
|
+
raise Async::TimeoutError, 'WebMock timeout error' if webmock_response.should_timeout
|
62
|
+
WebMockApplication.add_webmock_response(request, webmock_response)
|
63
|
+
response = @webmock_client.call(request)
|
64
|
+
elsif net_connect_allowed
|
65
|
+
response = @network_client.call(request)
|
66
|
+
else
|
67
|
+
raise WebMock::NetConnectNotAllowedError.new(request_signature) unless webmock_response
|
68
|
+
end
|
69
|
+
|
70
|
+
if WebMock::CallbackRegistry.any_callbacks?
|
71
|
+
webmock_response ||= build_webmock_response(response)
|
72
|
+
WebMock::CallbackRegistry.invoke_callbacks(
|
73
|
+
{
|
74
|
+
lib: :async_http_client,
|
75
|
+
real_request: net_connect_allowed
|
76
|
+
},
|
77
|
+
request_signature,
|
78
|
+
webmock_response
|
79
|
+
)
|
80
|
+
end
|
81
|
+
|
82
|
+
response
|
83
|
+
end
|
84
|
+
|
85
|
+
def close
|
86
|
+
@network_client.close
|
87
|
+
@webmock_client.close
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def build_request_signature(request)
|
93
|
+
body = request.read
|
94
|
+
request.body = ::Protocol::HTTP::Body::Buffered.wrap(body)
|
95
|
+
WebMock::RequestSignature.new(
|
96
|
+
request.method.downcase.to_sym,
|
97
|
+
"#{request.scheme}://#{request.authority}#{request.path}",
|
98
|
+
headers: request.headers.to_h,
|
99
|
+
body: body
|
100
|
+
)
|
101
|
+
end
|
102
|
+
|
103
|
+
def build_webmock_response(response)
|
104
|
+
body = response.read
|
105
|
+
response.body = ::Protocol::HTTP::Body::Buffered.wrap(body)
|
106
|
+
|
107
|
+
webmock_response = WebMock::Response.new
|
108
|
+
webmock_response.status = [
|
109
|
+
response.status,
|
110
|
+
::Protocol::HTTP1::Reason::DESCRIPTIONS[response.status]
|
111
|
+
]
|
112
|
+
webmock_response.headers = build_webmock_response_headers(response)
|
113
|
+
webmock_response.body = body
|
114
|
+
webmock_response
|
115
|
+
end
|
116
|
+
|
117
|
+
def build_webmock_response_headers(response)
|
118
|
+
response.headers.each.each_with_object({}) do |(k, v), o|
|
119
|
+
o[k] ||= []
|
120
|
+
o[k] << v
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class WebMockClient < Client
|
126
|
+
end
|
127
|
+
|
128
|
+
class WebMockEndpoint
|
129
|
+
def initialize(scheme, authority, protocol)
|
130
|
+
@scheme = scheme
|
131
|
+
@authority = authority
|
132
|
+
@protocol = protocol
|
133
|
+
end
|
134
|
+
|
135
|
+
attr :scheme, :authority, :protocol
|
136
|
+
|
137
|
+
def connect
|
138
|
+
server_socket, client_socket = create_connected_sockets
|
139
|
+
Async do
|
140
|
+
accept_socket(server_socket)
|
141
|
+
end
|
142
|
+
client_socket
|
143
|
+
end
|
144
|
+
|
145
|
+
def inspect
|
146
|
+
"\#<#{self.class}> #{scheme}://#{authority} protocol=#{protocol}"
|
147
|
+
end
|
148
|
+
|
149
|
+
private
|
150
|
+
|
151
|
+
def create_connected_sockets
|
152
|
+
Async::IO::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM).tap do |sockets|
|
153
|
+
sockets.each do |socket|
|
154
|
+
socket.instance_variable_set :@alpn_protocol, @alpn_protocol
|
155
|
+
socket.instance_eval do
|
156
|
+
def alpn_protocol
|
157
|
+
nil # means HTTP11 will be used for HTTPS
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def accept_socket(socket)
|
165
|
+
server = Async::HTTP::Server.new(WebMockApplication, self)
|
166
|
+
server.accept(socket, socket.remote_address)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
module WebMockApplication
|
171
|
+
WEBMOCK_REQUEST_ID_HEADER = 'x-webmock-request-id'.freeze
|
172
|
+
|
173
|
+
class << self
|
174
|
+
def call(request)
|
175
|
+
request.read
|
176
|
+
webmock_response = get_webmock_response(request)
|
177
|
+
build_response(webmock_response)
|
178
|
+
end
|
179
|
+
|
180
|
+
def add_webmock_response(request, webmock_response)
|
181
|
+
webmock_request_id = request.object_id.to_s
|
182
|
+
request.headers.add(WEBMOCK_REQUEST_ID_HEADER, webmock_request_id)
|
183
|
+
webmock_responses[webmock_request_id] = webmock_response
|
184
|
+
end
|
185
|
+
|
186
|
+
def get_webmock_response(request)
|
187
|
+
webmock_request_id = request.headers[WEBMOCK_REQUEST_ID_HEADER][0]
|
188
|
+
webmock_responses.fetch(webmock_request_id)
|
189
|
+
end
|
190
|
+
|
191
|
+
private
|
192
|
+
|
193
|
+
def webmock_responses
|
194
|
+
@webmock_responses ||= {}
|
195
|
+
end
|
196
|
+
|
197
|
+
def build_response(webmock_response)
|
198
|
+
headers = (webmock_response.headers || {}).each_with_object([]) do |(k, value), o|
|
199
|
+
Array(value).each do |v|
|
200
|
+
o.push [k, v]
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
::Protocol::HTTP::Response[
|
205
|
+
webmock_response.status[0],
|
206
|
+
headers,
|
207
|
+
webmock_response.body
|
208
|
+
]
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|