webmock 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.gitignore +34 -0
  4. data/.rspec-tm +2 -0
  5. data/.travis.yml +19 -0
  6. data/CHANGELOG.md +1698 -0
  7. data/Gemfile +9 -0
  8. data/LICENSE +20 -0
  9. data/README.md +1125 -0
  10. data/Rakefile +28 -0
  11. data/lib/webmock.rb +59 -0
  12. data/lib/webmock/api.rb +109 -0
  13. data/lib/webmock/assertion_failure.rb +11 -0
  14. data/lib/webmock/callback_registry.rb +35 -0
  15. data/lib/webmock/config.rb +18 -0
  16. data/lib/webmock/cucumber.rb +10 -0
  17. data/lib/webmock/deprecation.rb +9 -0
  18. data/lib/webmock/errors.rb +17 -0
  19. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +214 -0
  20. data/lib/webmock/http_lib_adapters/curb_adapter.rb +347 -0
  21. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +228 -0
  22. data/lib/webmock/http_lib_adapters/excon_adapter.rb +162 -0
  23. data/lib/webmock/http_lib_adapters/http_lib_adapter.rb +7 -0
  24. data/lib/webmock/http_lib_adapters/http_lib_adapter_registry.rb +19 -0
  25. data/lib/webmock/http_lib_adapters/http_rb/client.rb +14 -0
  26. data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
  27. data/lib/webmock/http_lib_adapters/http_rb/response.rb +43 -0
  28. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
  29. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
  30. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
  31. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +242 -0
  32. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +130 -0
  33. data/lib/webmock/http_lib_adapters/net_http.rb +361 -0
  34. data/lib/webmock/http_lib_adapters/net_http_response.rb +34 -0
  35. data/lib/webmock/http_lib_adapters/patron_adapter.rb +130 -0
  36. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +174 -0
  37. data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
  38. data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
  39. data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
  40. data/lib/webmock/matchers/hash_including_matcher.rb +17 -0
  41. data/lib/webmock/minitest.rb +41 -0
  42. data/lib/webmock/rack_response.rb +69 -0
  43. data/lib/webmock/request_body_diff.rb +64 -0
  44. data/lib/webmock/request_execution_verifier.rb +77 -0
  45. data/lib/webmock/request_pattern.rb +370 -0
  46. data/lib/webmock/request_registry.rb +35 -0
  47. data/lib/webmock/request_signature.rb +54 -0
  48. data/lib/webmock/request_signature_snippet.rb +61 -0
  49. data/lib/webmock/request_stub.rb +100 -0
  50. data/lib/webmock/response.rb +153 -0
  51. data/lib/webmock/responses_sequence.rb +40 -0
  52. data/lib/webmock/rspec.rb +41 -0
  53. data/lib/webmock/rspec/matchers.rb +27 -0
  54. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +78 -0
  55. data/lib/webmock/rspec/matchers/webmock_matcher.rb +67 -0
  56. data/lib/webmock/stub_registry.rb +67 -0
  57. data/lib/webmock/stub_request_snippet.rb +38 -0
  58. data/lib/webmock/test_unit.rb +22 -0
  59. data/lib/webmock/util/hash_counter.rb +39 -0
  60. data/lib/webmock/util/hash_keys_stringifier.rb +25 -0
  61. data/lib/webmock/util/hash_validator.rb +17 -0
  62. data/lib/webmock/util/headers.rb +64 -0
  63. data/lib/webmock/util/json.rb +67 -0
  64. data/lib/webmock/util/query_mapper.rb +281 -0
  65. data/lib/webmock/util/uri.rb +110 -0
  66. data/lib/webmock/util/values_stringifier.rb +20 -0
  67. data/lib/webmock/util/version_checker.rb +111 -0
  68. data/lib/webmock/version.rb +3 -0
  69. data/lib/webmock/webmock.rb +161 -0
  70. data/minitest/test_helper.rb +34 -0
  71. data/minitest/test_webmock.rb +9 -0
  72. data/minitest/webmock_spec.rb +60 -0
  73. data/spec/acceptance/async_http_client/async_http_client_spec.rb +349 -0
  74. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
  75. data/spec/acceptance/curb/curb_spec.rb +492 -0
  76. data/spec/acceptance/curb/curb_spec_helper.rb +147 -0
  77. data/spec/acceptance/em_http_request/em_http_request_spec.rb +406 -0
  78. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +77 -0
  79. data/spec/acceptance/excon/excon_spec.rb +77 -0
  80. data/spec/acceptance/excon/excon_spec_helper.rb +50 -0
  81. data/spec/acceptance/http_rb/http_rb_spec.rb +82 -0
  82. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
  83. data/spec/acceptance/httpclient/httpclient_spec.rb +217 -0
  84. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +57 -0
  85. data/spec/acceptance/manticore/manticore_spec.rb +56 -0
  86. data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
  87. data/spec/acceptance/net_http/net_http_shared.rb +153 -0
  88. data/spec/acceptance/net_http/net_http_spec.rb +331 -0
  89. data/spec/acceptance/net_http/net_http_spec_helper.rb +64 -0
  90. data/spec/acceptance/net_http/real_net_http_spec.rb +20 -0
  91. data/spec/acceptance/patron/patron_spec.rb +125 -0
  92. data/spec/acceptance/patron/patron_spec_helper.rb +54 -0
  93. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +313 -0
  94. data/spec/acceptance/shared/callbacks.rb +148 -0
  95. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +36 -0
  96. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +95 -0
  97. data/spec/acceptance/shared/precedence_of_stubs.rb +15 -0
  98. data/spec/acceptance/shared/request_expectations.rb +930 -0
  99. data/spec/acceptance/shared/returning_declared_responses.rb +409 -0
  100. data/spec/acceptance/shared/stubbing_requests.rb +643 -0
  101. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +135 -0
  102. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +60 -0
  103. data/spec/acceptance/webmock_shared.rb +41 -0
  104. data/spec/fixtures/test.txt +1 -0
  105. data/spec/quality_spec.rb +84 -0
  106. data/spec/spec_helper.rb +48 -0
  107. data/spec/support/example_curl_output.txt +22 -0
  108. data/spec/support/failures.rb +9 -0
  109. data/spec/support/my_rack_app.rb +53 -0
  110. data/spec/support/network_connection.rb +19 -0
  111. data/spec/support/webmock_server.rb +70 -0
  112. data/spec/unit/api_spec.rb +175 -0
  113. data/spec/unit/errors_spec.rb +129 -0
  114. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +17 -0
  115. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +12 -0
  116. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
  117. data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
  118. data/spec/unit/rack_response_spec.rb +112 -0
  119. data/spec/unit/request_body_diff_spec.rb +90 -0
  120. data/spec/unit/request_execution_verifier_spec.rb +208 -0
  121. data/spec/unit/request_pattern_spec.rb +601 -0
  122. data/spec/unit/request_registry_spec.rb +95 -0
  123. data/spec/unit/request_signature_snippet_spec.rb +89 -0
  124. data/spec/unit/request_signature_spec.rb +155 -0
  125. data/spec/unit/request_stub_spec.rb +199 -0
  126. data/spec/unit/response_spec.rb +282 -0
  127. data/spec/unit/stub_registry_spec.rb +103 -0
  128. data/spec/unit/stub_request_snippet_spec.rb +115 -0
  129. data/spec/unit/util/hash_counter_spec.rb +39 -0
  130. data/spec/unit/util/hash_keys_stringifier_spec.rb +27 -0
  131. data/spec/unit/util/headers_spec.rb +28 -0
  132. data/spec/unit/util/json_spec.rb +33 -0
  133. data/spec/unit/util/query_mapper_spec.rb +157 -0
  134. data/spec/unit/util/uri_spec.rb +361 -0
  135. data/spec/unit/util/version_checker_spec.rb +65 -0
  136. data/spec/unit/webmock_spec.rb +19 -0
  137. data/test/http_request.rb +24 -0
  138. data/test/shared_test.rb +108 -0
  139. data/test/test_helper.rb +23 -0
  140. data/test/test_webmock.rb +6 -0
  141. data/webmock.gemspec +45 -0
  142. metadata +496 -0
@@ -0,0 +1,57 @@
1
+ module HTTPClientSpecHelper
2
+ class << self
3
+ attr_accessor :async_mode
4
+ end
5
+
6
+ def http_request(method, uri, options = {}, &block)
7
+ uri = Addressable::URI.heuristic_parse(uri)
8
+ c = options.fetch(:client) { HTTPClient.new }
9
+ c.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
10
+ c.reset_all
11
+ if options[:basic_auth]
12
+ c.force_basic_auth = true
13
+ c.set_basic_auth(nil, options[:basic_auth][0], options[:basic_auth][1])
14
+ end
15
+ params = [method, uri.normalize.to_s,
16
+ WebMock::Util::QueryMapper.query_to_values(uri.query, notation: WebMock::Config.instance.query_values_notation), options[:body], options[:headers] || {}]
17
+ if HTTPClientSpecHelper.async_mode
18
+ connection = c.request_async(*params)
19
+ connection.join
20
+ response = connection.pop
21
+ else
22
+ response = c.request(*params, &block)
23
+ end
24
+ headers = merge_headers(response)
25
+ OpenStruct.new({
26
+ body: HTTPClientSpecHelper.async_mode ? response.content.read : response.content,
27
+ headers: headers,
28
+ status: response.code.to_s,
29
+ message: response.reason
30
+ })
31
+ end
32
+
33
+ def client_timeout_exception_class
34
+ HTTPClient::TimeoutError
35
+ end
36
+
37
+ def connection_refused_exception_class
38
+ Errno::ECONNREFUSED
39
+ end
40
+
41
+ def http_library
42
+ :httpclient
43
+ end
44
+
45
+ private
46
+
47
+ def merge_headers(response)
48
+ response.header.all.inject({}) do |headers, header|
49
+ if !headers.has_key?(header[0])
50
+ headers[header[0]] = header[1]
51
+ else
52
+ headers[header[0]] = [headers[header[0]], header[1]].join(', ')
53
+ end
54
+ headers
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'acceptance/webmock_shared'
3
+
4
+ if RUBY_PLATFORM =~ /java/
5
+ require 'acceptance/manticore/manticore_spec_helper'
6
+
7
+ describe "Manticore" do
8
+ include ManticoreSpecHelper
9
+
10
+ include_context "with WebMock", :no_status_message
11
+
12
+ context "calling http methods on Manticore directly using Manticore's facade" do
13
+ it "handles GET" do
14
+ stub_request(:get, "http://example-foo.com").to_return(status: 301)
15
+ response = Manticore.get("http://example-foo.com")
16
+ expect(response.code).to eq(301)
17
+ end
18
+
19
+ it "handles POST" do
20
+ stub_request(:post, "http://example-foo.com").to_return(status: 201)
21
+ response = Manticore.post("http://example-foo.com", {hello: "world"})
22
+ expect(response.code).to eq(201)
23
+ end
24
+
25
+ it "handles PUT" do
26
+ stub_request(:put, "http://example-foo.com").to_return(status: 409)
27
+ response = Manticore.put("http://example-foo.com", {hello: "world"})
28
+ expect(response.code).to eq(409)
29
+ end
30
+
31
+ it "handles PATCH" do
32
+ stub_request(:patch, "http://example-foo.com").to_return(status: 409)
33
+ response = Manticore.patch("http://example-foo.com", {hello: "world"})
34
+ expect(response.code).to eq(409)
35
+ end
36
+
37
+ it "handles DELETE" do
38
+ stub_request(:delete, "http://example-foo.com").to_return(status: 204)
39
+ response = Manticore.delete("http://example-foo.com", {id: 1})
40
+ expect(response.code).to eq(204)
41
+ end
42
+
43
+ it "handles OPTIONS" do
44
+ stub_request(:options, "http://example-foo.com").to_return(status: 200)
45
+ response = Manticore.options("http://example-foo.com")
46
+ expect(response.code).to eq(200)
47
+ end
48
+
49
+ it "handles HEAD" do
50
+ stub_request(:head, "http://example-foo.com").to_return(status: 204)
51
+ response = Manticore.head("http://example-foo.com")
52
+ expect(response.code).to eq(204)
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,35 @@
1
+ module ManticoreSpecHelper
2
+ def http_request(method, uri, options = {})
3
+ client = Manticore::Client.new
4
+
5
+ if basic_auth = options[:basic_auth]
6
+ options = options.merge(auth: {user: basic_auth[0], pass: basic_auth[1]})
7
+ end
8
+
9
+ response = client.http(method, uri, options)
10
+ OpenStruct.new({
11
+ body: response.body || '',
12
+ headers: WebMock::Util::Headers.normalize_headers(join_array_values(response.headers)),
13
+ status: response.code.to_s
14
+ })
15
+ end
16
+
17
+ def join_array_values(hash)
18
+ hash.reduce({}) do |h, (k,v)|
19
+ v = v.join(', ') if v.is_a?(Array)
20
+ h.merge(k => v)
21
+ end
22
+ end
23
+
24
+ def client_timeout_exception_class
25
+ Manticore::ConnectTimeout
26
+ end
27
+
28
+ def connection_refused_exception_class
29
+ Manticore::SocketException
30
+ end
31
+
32
+ def http_library
33
+ :manticore
34
+ end
35
+ end
@@ -0,0 +1,153 @@
1
+ shared_examples_for "Net::HTTP" do
2
+ describe "when making real requests", net_connect: true do
3
+ let(:port){ WebMockServer.instance.port }
4
+
5
+ before(:each) do
6
+ @http = Net::HTTP.new("localhost", port)
7
+ end
8
+
9
+ it "should return a Net::ReadAdapter from response.body when a real request is made with a block and #read_body", net_connect: true do
10
+ response = Net::HTTP.new("localhost", port).request_get('/') { |r| r.read_body { } }
11
+ expect(response.body).to be_a(Net::ReadAdapter)
12
+ end
13
+
14
+ it "should handle requests with block passed to read_body", net_connect: true do
15
+ body = "".dup
16
+ req = Net::HTTP::Get.new("/")
17
+ Net::HTTP.start("localhost", port) do |http|
18
+ http.request(req) do |res|
19
+ res.read_body do |str|
20
+ body << str
21
+ end
22
+ end
23
+ end
24
+ expect(body).to match(/hello world/)
25
+ end
26
+
27
+ it "should connect only once when connected on start", net_connect: true do
28
+ @http = Net::HTTP.new('localhost', port)
29
+ socket_id_before_request = socket_id_after_request = nil
30
+ @http.start {|conn|
31
+ socket_id_before_request = conn.instance_variable_get(:@socket).object_id
32
+ conn.request(Net::HTTP::Get.new("/"))
33
+ socket_id_after_request = conn.instance_variable_get(:@socket).object_id
34
+ }
35
+
36
+ if !defined?(WebMock::Config) || WebMock::Config.instance.net_http_connect_on_start
37
+ expect(socket_id_before_request).not_to eq(nil.object_id)
38
+ expect(socket_id_after_request).not_to eq(nil.object_id)
39
+ expect(socket_id_after_request).to eq(socket_id_before_request)
40
+ else
41
+ expect(socket_id_before_request).to eq(nil.object_id)
42
+ expect(socket_id_after_request).not_to eq(nil.object_id)
43
+ end
44
+ end
45
+
46
+ it "should pass the read_timeout value on", net_connect: true do
47
+ @http = Net::HTTP.new('localhost', port)
48
+ read_timeout = @http.read_timeout + 1
49
+ @http.read_timeout = read_timeout
50
+ @http.start {|conn|
51
+ conn.request(Net::HTTP::Get.new("/"))
52
+ socket = conn.instance_variable_get(:@socket)
53
+ expect(socket.read_timeout).to eq(read_timeout)
54
+ }
55
+ end
56
+
57
+ describe "without start" do
58
+ it "should close connection after a real request" do
59
+ @http.get('/') { }
60
+ expect(@http).not_to be_started
61
+ end
62
+
63
+ it "should execute block exactly once" do
64
+ times = 0
65
+ @http.get('/') { times += 1 }
66
+ expect(times).to eq(1)
67
+ end
68
+
69
+ it "should have socket open during a real request" do
70
+ socket_id = nil
71
+ @http.get('/') {
72
+ socket_id = @http.instance_variable_get(:@socket).object_id
73
+ }
74
+ expect(socket_id).not_to be_nil
75
+ end
76
+
77
+ it "should be started during a real request" do
78
+ started = nil
79
+ @http.get('/') {
80
+ started = @http.started?
81
+ }
82
+ expect(started).to eq(true)
83
+ expect(@http.started?).to eq(false)
84
+ end
85
+ end
86
+
87
+ describe "with start" do
88
+ it "should close connection after a real request" do
89
+ @http.start {|conn| conn.get('/') { } }
90
+ expect(@http).not_to be_started
91
+ end
92
+
93
+ it "should execute block exactly once" do
94
+ times = 0
95
+ @http.start {|conn| conn.get('/') { times += 1 }}
96
+ expect(times).to eq(1)
97
+ end
98
+
99
+ it "should have socket open during a real request" do
100
+ socket_id = nil
101
+ @http.start {|conn| conn.get('/') {
102
+ socket_id = conn.instance_variable_get(:@socket).object_id
103
+ }
104
+ }
105
+ expect(socket_id).not_to be_nil
106
+ end
107
+
108
+ it "should be started during a real request" do
109
+ started = nil
110
+ @http.start {|conn| conn.get('/') {
111
+ started = conn.started?
112
+ }
113
+ }
114
+ expect(started).to eq(true)
115
+ expect(@http.started?).to eq(false)
116
+ end
117
+ end
118
+
119
+ describe "with start without request block" do
120
+ it "should close connection after a real request" do
121
+ @http.start {|conn| conn.get('/') }
122
+ expect(@http).not_to be_started
123
+ end
124
+
125
+ it "should have socket open during a real request" do
126
+ socket_id = nil
127
+ @http.start {|conn|
128
+ socket_id = conn.instance_variable_get(:@socket).object_id
129
+ }
130
+ expect(socket_id).not_to be_nil
131
+ end
132
+
133
+ it "should be started during a real request" do
134
+ started = nil
135
+ @http.start {|conn|
136
+ started = conn.started?
137
+ }
138
+ expect(started).to eq(true)
139
+ expect(@http.started?).to eq(false)
140
+ end
141
+ end
142
+
143
+ describe "with start without a block and finish" do
144
+ it "should gracefully start and close connection" do
145
+ @http.start
146
+ @http.get("/")
147
+ expect(@http).to be_started
148
+ @http.finish
149
+ expect(@http).not_to be_started
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,331 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+ require 'acceptance/webmock_shared'
4
+ require 'acceptance/net_http/net_http_spec_helper'
5
+ require 'acceptance/net_http/net_http_shared'
6
+
7
+ include NetHTTPSpecHelper
8
+
9
+ describe "Net:HTTP" do
10
+ include_examples "with WebMock", :no_url_auth
11
+
12
+ let(:port) { WebMockServer.instance.port }
13
+
14
+ describe "marshalling" do
15
+ class TestMarshalingInWebMockNetHTTP
16
+ attr_accessor :r
17
+ end
18
+ before(:each) do
19
+ @b = TestMarshalingInWebMockNetHTTP.new
20
+ end
21
+ after(:each) do
22
+ WebMock.enable!
23
+ end
24
+ it "should be possible to load object marshalled when webmock was disabled" do
25
+ WebMock.disable!
26
+ original_constants = [
27
+ Net::HTTP::Get,
28
+ Net::HTTP::Post,
29
+ Net::HTTP::Put,
30
+ Net::HTTP::Delete,
31
+ Net::HTTP::Head,
32
+ Net::HTTP::Options
33
+ ]
34
+ @b.r = original_constants
35
+ original_serialized = Marshal.dump(@b)
36
+ Marshal.load(original_serialized)
37
+ WebMock.enable!
38
+ Marshal.load(original_serialized)
39
+ end
40
+
41
+ it "should be possible to load object marshalled when webmock was enabled" do
42
+ WebMock.enable!
43
+ new_constants = [
44
+ Net::HTTP::Get,
45
+ Net::HTTP::Post,
46
+ Net::HTTP::Put,
47
+ Net::HTTP::Delete,
48
+ Net::HTTP::Head,
49
+ Net::HTTP::Options
50
+ ]
51
+ @b.r = new_constants
52
+ new_serialized = Marshal.dump(@b)
53
+ Marshal.load(new_serialized)
54
+ WebMock.disable!
55
+ Marshal.load(new_serialized)
56
+ end
57
+ end
58
+
59
+ describe "constants" do
60
+ it "should still have const Get defined on replaced Net::HTTP" do
61
+ expect(Object.const_get("Net").const_get("HTTP").const_defined?("Get")).to be_truthy
62
+ end
63
+
64
+ it "should still have const Get within constants on replaced Net::HTTP" do
65
+ expect(Object.const_get("Net").const_get("HTTP").constants.map(&:to_s)).to include("Get")
66
+ end
67
+
68
+ it "should still have const Get within constants on replaced Net::HTTP" do
69
+ expect(Object.const_get("Net").const_get("HTTP").const_get("Get")).not_to be_nil
70
+ end
71
+
72
+ if Module.method(:const_defined?).arity != 1
73
+ it "should still have const Get defined (and not inherited) on replaced Net::HTTP" do
74
+ expect(Object.const_get("Net").const_get("HTTP").const_defined?("Get", false)).to be_truthy
75
+ end
76
+ end
77
+
78
+ if Module.method(:const_get).arity != 1
79
+ it "should still be able to get non inherited constant Get on replaced Net::HTTP" do
80
+ expect(Object.const_get("Net").const_get("HTTP").const_get("Get", false)).not_to be_nil
81
+ end
82
+ end
83
+
84
+ if Module.method(:constants).arity != 0
85
+ it "should still Get within non inherited constants on replaced Net::HTTP" do
86
+ expect(Object.const_get("Net").const_get("HTTP").constants(false).map(&:to_s)).to include("Get")
87
+ end
88
+ end
89
+
90
+ describe "after WebMock is disabled" do
91
+ after(:each) do
92
+ WebMock.enable!
93
+ end
94
+ it "Net::HTTP should have the same constants" do
95
+ orig_consts_number = WebMock::HttpLibAdapters::NetHttpAdapter::OriginalNetHTTP.constants.size
96
+ Net::HTTP.send(:const_set, "TEST_CONST", 10)
97
+ expect(Net::HTTP.constants.size).to eq(orig_consts_number + 1)
98
+ WebMock.disable!
99
+ expect(Net::HTTP.constants.size).to eq(orig_consts_number + 1)
100
+ end
101
+ end
102
+ end
103
+
104
+ it "should work with block provided" do
105
+ stub_http_request(:get, "www.example.com").to_return(body: "abc"*100000)
106
+ expect(Net::HTTP.start("www.example.com") { |query| query.get("/") }.body).to eq("abc"*100000)
107
+ end
108
+
109
+ it "should handle requests with raw binary data" do
110
+ body = "\x14\x00\x00\x00\x70\x69\x6e\x67\x00\x00"
111
+ stub_http_request(:post, "www.example.com").with(body: body).to_return(body: "abc")
112
+ req = Net::HTTP::Post.new("/")
113
+ req.body = body
114
+ req.content_type = "application/octet-stream"
115
+ expect(Net::HTTP.start("www.example.com") { |http| http.request(req)}.body).to eq("abc")
116
+ end
117
+
118
+ it "raises an ArgumentError if passed headers as symbols if RUBY_VERSION < 2.3.0" do
119
+ uri = URI.parse("http://google.com/")
120
+ http = Net::HTTP.new(uri.host, uri.port)
121
+ request = Net::HTTP::Get.new(uri.request_uri)
122
+
123
+ # Net::HTTP calls downcase on header keys assigned with []=
124
+ # In Ruby 1.8.7 symbols do not respond to downcase
125
+ #
126
+ # Meaning you can not assign header keys as symbols in ruby 1.8.7 using []=
127
+ if :symbol.respond_to?(:downcase)
128
+ request[:InvalidHeaderSinceItsASymbol] = "this will not be valid"
129
+ else
130
+ request.instance_eval do
131
+ @header = request.to_hash.merge({InvalidHeaderSinceItsASymbol: "this will not be valid"})
132
+ end
133
+ end
134
+
135
+ if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.3.0')
136
+ expect do
137
+ http.request(request)
138
+ end.to raise_error ArgumentError, "Net:HTTP does not accept headers as symbols"
139
+ else
140
+ stub_http_request(:get, "google.com").with(headers: { InvalidHeaderSinceItsASymbol: "this will not be valid" })
141
+ expect do
142
+ http.request(request)
143
+ end.not_to raise_error
144
+ end
145
+ end
146
+
147
+ it "should handle multiple values for the same response header" do
148
+ stub_http_request(:get, "www.example.com").to_return(headers: { 'Set-Cookie' => ['foo=bar', 'bar=bazz'] })
149
+ response = Net::HTTP.get_response(URI.parse("http://www.example.com/"))
150
+ expect(response.get_fields('Set-Cookie')).to eq(['bar=bazz', 'foo=bar'])
151
+ end
152
+
153
+ it "should yield block on response" do
154
+ stub_http_request(:get, "www.example.com").to_return(body: "abc")
155
+ response_body = ""
156
+ http_request(:get, "http://www.example.com/") do |response|
157
+ response_body = response.body
158
+ end
159
+ expect(response_body).to eq("abc")
160
+ end
161
+
162
+ it "should handle Net::HTTP::Post#body" do
163
+ stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
164
+ req = Net::HTTP::Post.new("/")
165
+ req.body = "my_params"
166
+ expect(Net::HTTP.start("www.example.com") { |http| http.request(req)}.body).to eq("abc")
167
+ end
168
+
169
+ it "should handle Net::HTTP::Post#body_stream" do
170
+ stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
171
+ req = Net::HTTP::Post.new("/")
172
+ req.body_stream = StringIO.new("my_params")
173
+ expect(Net::HTTP.start("www.example.com") { |http| http.request(req)}.body).to eq("abc")
174
+ end
175
+
176
+ it "should behave like Net::HTTP and raise error if both request body and body argument are set" do
177
+ stub_http_request(:post, "www.example.com").with(body: "my_params").to_return(body: "abc")
178
+ req = Net::HTTP::Post.new("/")
179
+ req.body = "my_params"
180
+ expect {
181
+ Net::HTTP.start("www.example.com") { |http| http.request(req, "my_params")}
182
+ }.to raise_error("both of body argument and HTTPRequest#body set")
183
+ end
184
+
185
+ it "should return a Net::ReadAdapter from response.body when a stubbed request is made with a block and #read_body" do
186
+ WebMock.stub_request(:get, 'http://example.com/').to_return(body: "the body")
187
+ response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
188
+ expect(response.body).to be_a(Net::ReadAdapter)
189
+ end
190
+
191
+ it "should have request 1 time executed in registry after 1 real request", net_connect: true do
192
+ WebMock.allow_net_connect!
193
+ http = Net::HTTP.new('localhost', port)
194
+ http.get('/') {}
195
+ expect(WebMock::RequestRegistry.instance.requested_signatures.hash.size).to eq(1)
196
+ expect(WebMock::RequestRegistry.instance.requested_signatures.hash.values.first).to eq(1)
197
+ end
198
+
199
+ it "should work with Addressable::URI passed to Net::HTTP.get_response" do
200
+ stub_request(:get, 'http://www.example.com/hello?a=1').to_return(body: "abc")
201
+ expect(Net::HTTP.get_response(Addressable::URI.parse('http://www.example.com/hello?a=1')).body).to eq("abc")
202
+ end
203
+
204
+ describe "connecting on Net::HTTP.start" do
205
+ before(:each) do
206
+ @http = Net::HTTP.new('www.google.com', 443)
207
+ @http.use_ssl = true
208
+ @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
209
+ end
210
+
211
+ describe "when net http is allowed" do
212
+ it "should not connect to the server until the request", net_connect: true do
213
+ WebMock.allow_net_connect!
214
+ @http.start {|conn|
215
+ expect(conn.peer_cert).to be_nil
216
+ }
217
+ end
218
+
219
+ it "should connect to the server on start", net_connect: true do
220
+ WebMock.allow_net_connect!(net_http_connect_on_start: true)
221
+ @http.start {|conn|
222
+ cert = OpenSSL::X509::Certificate.new conn.peer_cert
223
+ expect(cert).to be_a(OpenSSL::X509::Certificate)
224
+ }
225
+ end
226
+
227
+ end
228
+
229
+ describe "when net http is disabled and allowed only for some hosts" do
230
+ it "should not connect to the server until the request", net_connect: true do
231
+ WebMock.disable_net_connect!(allow: "www.google.com")
232
+ @http.start {|conn|
233
+ expect(conn.peer_cert).to be_nil
234
+ }
235
+ end
236
+
237
+ it "should connect to the server on start", net_connect: true do
238
+ WebMock.disable_net_connect!(allow: "www.google.com", net_http_connect_on_start: true)
239
+ @http.start {|conn|
240
+ cert = OpenSSL::X509::Certificate.new conn.peer_cert
241
+ expect(cert).to be_a(OpenSSL::X509::Certificate)
242
+ }
243
+ end
244
+
245
+ it "should connect to the server if the URI matches an regex", net_connect: true do
246
+ WebMock.disable_net_connect!(allow: /google.com/)
247
+ Net::HTTP.get('www.google.com','/')
248
+ end
249
+
250
+ it "should connect to the server if the URI matches any regex the array", net_connect: true do
251
+ WebMock.disable_net_connect!(allow: [/google.com/, /yahoo.com/])
252
+ Net::HTTP.get('www.google.com','/')
253
+ end
254
+
255
+ end
256
+
257
+ end
258
+
259
+ describe "when net_http_connect_on_start is true" do
260
+ before(:each) do
261
+ WebMock.allow_net_connect!(net_http_connect_on_start: true)
262
+ end
263
+ it_should_behave_like "Net::HTTP"
264
+ end
265
+
266
+ describe "when net_http_connect_on_start is false" do
267
+ before(:each) do
268
+ WebMock.allow_net_connect!(net_http_connect_on_start: false)
269
+ end
270
+ it_should_behave_like "Net::HTTP"
271
+ end
272
+
273
+ describe 'after_request callback support', net_connect: true do
274
+ let(:expected_body_regex) { /hello world/ }
275
+
276
+ before(:each) do
277
+ WebMock.allow_net_connect!
278
+ @callback_invocation_count = 0
279
+ WebMock.after_request do |_, response|
280
+ @callback_invocation_count += 1
281
+ @callback_response = response
282
+ end
283
+ end
284
+
285
+ after(:each) do
286
+ WebMock.reset_callbacks
287
+ end
288
+
289
+ def perform_get_with_returning_block
290
+ http_request(:get, "http://localhost:#{port}/") do |response|
291
+ return response.body
292
+ end
293
+ end
294
+
295
+ it "should support the after_request callback on an request with block and read_body" do
296
+ response_body = ''.dup
297
+ http_request(:get, "http://localhost:#{port}/") do |response|
298
+ response.read_body { |fragment| response_body << fragment }
299
+ end
300
+ expect(response_body).to match(expected_body_regex)
301
+
302
+ expect(@callback_response.body).to eq(response_body)
303
+ end
304
+
305
+ it "should support the after_request callback on a request with a returning block" do
306
+ response_body = perform_get_with_returning_block
307
+ expect(response_body).to match(expected_body_regex)
308
+ expect(@callback_response).to be_instance_of(WebMock::Response)
309
+ expect(@callback_response.body).to eq(response_body)
310
+ end
311
+
312
+ it "should only invoke the after_request callback once, even for a recursive post request" do
313
+ Net::HTTP.new('localhost', port).post('/', nil)
314
+ expect(@callback_invocation_count).to eq(1)
315
+ end
316
+ end
317
+
318
+ it "should match http headers, even if their values have been set in a request as numbers" do
319
+ WebMock.disable_net_connect!
320
+
321
+ stub_request(:post, "www.example.com").with(headers: {"My-Header" => 99})
322
+
323
+ uri = URI.parse('http://www.example.com/')
324
+ req = Net::HTTP::Post.new(uri.path)
325
+ req['My-Header'] = 99
326
+
327
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
328
+ http.request(req, '')
329
+ end
330
+ end
331
+ end