webmock 1.8.6 → 3.14.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 (138) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/CI.yml +37 -0
  3. data/.gitignore +6 -0
  4. data/CHANGELOG.md +1198 -0
  5. data/Gemfile +3 -15
  6. data/README.md +761 -305
  7. data/Rakefile +13 -40
  8. data/lib/webmock/api.rb +63 -17
  9. data/lib/webmock/callback_registry.rb +1 -1
  10. data/lib/webmock/config.rb +8 -0
  11. data/lib/webmock/cucumber.rb +2 -0
  12. data/lib/webmock/errors.rb +8 -24
  13. data/lib/webmock/http_lib_adapters/async_http_client_adapter.rb +216 -0
  14. data/lib/webmock/http_lib_adapters/curb_adapter.rb +148 -84
  15. data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +224 -4
  16. data/lib/webmock/http_lib_adapters/excon_adapter.rb +104 -34
  17. data/lib/webmock/http_lib_adapters/http_rb/client.rb +17 -0
  18. data/lib/webmock/http_lib_adapters/http_rb/request.rb +16 -0
  19. data/lib/webmock/http_lib_adapters/http_rb/response.rb +64 -0
  20. data/lib/webmock/http_lib_adapters/http_rb/streamer.rb +29 -0
  21. data/lib/webmock/http_lib_adapters/http_rb/webmock.rb +68 -0
  22. data/lib/webmock/http_lib_adapters/http_rb_adapter.rb +37 -0
  23. data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +152 -86
  24. data/lib/webmock/http_lib_adapters/manticore_adapter.rb +145 -0
  25. data/lib/webmock/http_lib_adapters/net_http.rb +155 -46
  26. data/lib/webmock/http_lib_adapters/net_http_response.rb +1 -1
  27. data/lib/webmock/http_lib_adapters/patron_adapter.rb +16 -15
  28. data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +76 -82
  29. data/lib/webmock/matchers/any_arg_matcher.rb +13 -0
  30. data/lib/webmock/matchers/hash_argument_matcher.rb +21 -0
  31. data/lib/webmock/matchers/hash_excluding_matcher.rb +15 -0
  32. data/lib/webmock/matchers/hash_including_matcher.rb +4 -12
  33. data/lib/webmock/minitest.rb +29 -3
  34. data/lib/webmock/rack_response.rb +14 -7
  35. data/lib/webmock/request_body_diff.rb +64 -0
  36. data/lib/webmock/request_execution_verifier.rb +38 -17
  37. data/lib/webmock/request_pattern.rb +158 -38
  38. data/lib/webmock/request_registry.rb +3 -3
  39. data/lib/webmock/request_signature.rb +7 -3
  40. data/lib/webmock/request_signature_snippet.rb +61 -0
  41. data/lib/webmock/request_stub.rb +9 -6
  42. data/lib/webmock/response.rb +30 -15
  43. data/lib/webmock/rspec/matchers/request_pattern_matcher.rb +38 -2
  44. data/lib/webmock/rspec/matchers/webmock_matcher.rb +23 -2
  45. data/lib/webmock/rspec/matchers.rb +0 -1
  46. data/lib/webmock/rspec.rb +11 -2
  47. data/lib/webmock/stub_registry.rb +31 -10
  48. data/lib/webmock/stub_request_snippet.rb +14 -6
  49. data/lib/webmock/test_unit.rb +4 -4
  50. data/lib/webmock/util/hash_counter.rb +20 -6
  51. data/lib/webmock/util/hash_keys_stringifier.rb +5 -3
  52. data/lib/webmock/util/hash_validator.rb +17 -0
  53. data/lib/webmock/util/headers.rb +23 -2
  54. data/lib/webmock/util/json.rb +20 -7
  55. data/lib/webmock/util/query_mapper.rb +281 -0
  56. data/lib/webmock/util/uri.rb +29 -19
  57. data/lib/webmock/util/values_stringifier.rb +20 -0
  58. data/lib/webmock/util/version_checker.rb +40 -2
  59. data/lib/webmock/version.rb +1 -1
  60. data/lib/webmock/webmock.rb +56 -17
  61. data/lib/webmock.rb +56 -46
  62. data/minitest/test_helper.rb +8 -3
  63. data/minitest/test_webmock.rb +4 -1
  64. data/minitest/webmock_spec.rb +16 -6
  65. data/spec/acceptance/async_http_client/async_http_client_spec.rb +375 -0
  66. data/spec/acceptance/async_http_client/async_http_client_spec_helper.rb +73 -0
  67. data/spec/acceptance/curb/curb_spec.rb +227 -68
  68. data/spec/acceptance/curb/curb_spec_helper.rb +11 -8
  69. data/spec/acceptance/em_http_request/em_http_request_spec.rb +322 -28
  70. data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +15 -10
  71. data/spec/acceptance/excon/excon_spec.rb +66 -4
  72. data/spec/acceptance/excon/excon_spec_helper.rb +21 -7
  73. data/spec/acceptance/http_rb/http_rb_spec.rb +93 -0
  74. data/spec/acceptance/http_rb/http_rb_spec_helper.rb +54 -0
  75. data/spec/acceptance/httpclient/httpclient_spec.rb +152 -11
  76. data/spec/acceptance/httpclient/httpclient_spec_helper.rb +25 -16
  77. data/spec/acceptance/manticore/manticore_spec.rb +107 -0
  78. data/spec/acceptance/manticore/manticore_spec_helper.rb +35 -0
  79. data/spec/acceptance/net_http/net_http_shared.rb +52 -24
  80. data/spec/acceptance/net_http/net_http_spec.rb +164 -50
  81. data/spec/acceptance/net_http/net_http_spec_helper.rb +19 -10
  82. data/spec/acceptance/net_http/real_net_http_spec.rb +1 -1
  83. data/spec/acceptance/patron/patron_spec.rb +29 -40
  84. data/spec/acceptance/patron/patron_spec_helper.rb +15 -11
  85. data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +229 -58
  86. data/spec/acceptance/shared/callbacks.rb +32 -30
  87. data/spec/acceptance/shared/complex_cross_concern_behaviors.rb +20 -5
  88. data/spec/acceptance/shared/enabling_and_disabling_webmock.rb +14 -14
  89. data/spec/acceptance/shared/precedence_of_stubs.rb +6 -6
  90. data/spec/acceptance/shared/request_expectations.rb +560 -296
  91. data/spec/acceptance/shared/returning_declared_responses.rb +180 -138
  92. data/spec/acceptance/shared/stubbing_requests.rb +385 -154
  93. data/spec/acceptance/typhoeus/typhoeus_hydra_spec.rb +78 -17
  94. data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +19 -15
  95. data/spec/acceptance/webmock_shared.rb +2 -2
  96. data/spec/fixtures/test.txt +1 -0
  97. data/spec/quality_spec.rb +27 -3
  98. data/spec/spec_helper.rb +11 -20
  99. data/spec/support/failures.rb +9 -0
  100. data/spec/support/my_rack_app.rb +8 -3
  101. data/spec/support/network_connection.rb +7 -13
  102. data/spec/support/webmock_server.rb +8 -3
  103. data/spec/unit/api_spec.rb +175 -0
  104. data/spec/unit/errors_spec.rb +116 -19
  105. data/spec/unit/http_lib_adapters/http_lib_adapter_registry_spec.rb +1 -1
  106. data/spec/unit/http_lib_adapters/http_lib_adapter_spec.rb +2 -2
  107. data/spec/unit/matchers/hash_excluding_matcher_spec.rb +61 -0
  108. data/spec/unit/matchers/hash_including_matcher_spec.rb +87 -0
  109. data/spec/unit/rack_response_spec.rb +54 -16
  110. data/spec/unit/request_body_diff_spec.rb +90 -0
  111. data/spec/unit/request_execution_verifier_spec.rb +147 -39
  112. data/spec/unit/request_pattern_spec.rb +462 -198
  113. data/spec/unit/request_registry_spec.rb +29 -9
  114. data/spec/unit/request_signature_snippet_spec.rb +89 -0
  115. data/spec/unit/request_signature_spec.rb +91 -49
  116. data/spec/unit/request_stub_spec.rb +71 -70
  117. data/spec/unit/response_spec.rb +100 -81
  118. data/spec/unit/stub_registry_spec.rb +37 -20
  119. data/spec/unit/stub_request_snippet_spec.rb +51 -31
  120. data/spec/unit/util/hash_counter_spec.rb +6 -6
  121. data/spec/unit/util/hash_keys_stringifier_spec.rb +4 -4
  122. data/spec/unit/util/headers_spec.rb +4 -4
  123. data/spec/unit/util/json_spec.rb +29 -3
  124. data/spec/unit/util/query_mapper_spec.rb +157 -0
  125. data/spec/unit/util/uri_spec.rb +150 -36
  126. data/spec/unit/util/version_checker_spec.rb +15 -9
  127. data/spec/unit/webmock_spec.rb +57 -4
  128. data/test/http_request.rb +3 -3
  129. data/test/shared_test.rb +45 -13
  130. data/test/test_helper.rb +1 -1
  131. data/test/test_webmock.rb +6 -0
  132. data/webmock.gemspec +30 -11
  133. metadata +308 -199
  134. data/.rvmrc +0 -1
  135. data/.travis.yml +0 -11
  136. data/Guardfile +0 -24
  137. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_0_x.rb +0 -151
  138. data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +0 -210
@@ -19,23 +19,29 @@ unless RUBY_PLATFORM =~ /java/
19
19
  @sess.base_url = "http://www.example.com"
20
20
  end
21
21
 
22
+ it "should allow stubbing PATCH request with body" do
23
+ stub_request(:patch, "http://www.example.com/")
24
+ .with(body: "abc")
25
+
26
+ @sess.patch('/', "abc")
27
+ end
28
+
22
29
  describe "file requests" do
23
30
 
24
31
  before(:each) do
25
32
  @dir_path = Dir.mktmpdir('webmock-')
26
33
  @file_path = File.join(@dir_path, "webmock_temp_test_file")
27
- FileUtils.rm_rf(@file_path) if File.exists?(@file_path)
34
+ FileUtils.rm_rf(@file_path) if File.exist?(@file_path)
28
35
  end
29
36
 
30
37
  after(:each) do
31
38
  FileUtils.rm_rf(@dir_path) if File.exist?(@dir_path)
32
39
  end
33
40
 
34
-
35
41
  it "should work with get_file" do
36
- stub_request(:get, "www.example.com").to_return(:body => "abc")
42
+ stub_request(:get, "www.example.com").to_return(body: "abc")
37
43
  @sess.get_file("/", @file_path)
38
- File.read(@file_path).should == "abc"
44
+ expect(File.read(@file_path)).to eq("abc")
39
45
  end
40
46
 
41
47
  it "should raise same error as Patron if file is not readable for get request" do
@@ -44,9 +50,9 @@ unless RUBY_PLATFORM =~ /java/
44
50
  tmpfile.chmod(0400)
45
51
  end
46
52
  begin
47
- lambda {
53
+ expect {
48
54
  @sess.get_file("/", "/tmp/read_only_file")
49
- }.should raise_error(ArgumentError, "Unable to open specified file.")
55
+ }.to raise_error(ArgumentError, "Unable to open specified file.")
50
56
  ensure
51
57
  File.unlink("/tmp/read_only_file")
52
58
  end
@@ -54,21 +60,21 @@ unless RUBY_PLATFORM =~ /java/
54
60
 
55
61
  it "should work with put_file" do
56
62
  File.open(@file_path, "w") {|f| f.write "abc"}
57
- stub_request(:put, "www.example.com").with(:body => "abc")
63
+ stub_request(:put, "www.example.com").with(body: "abc")
58
64
  @sess.put_file("/", @file_path)
59
65
  end
60
66
 
61
67
  it "should work with post_file" do
62
68
  File.open(@file_path, "w") {|f| f.write "abc"}
63
- stub_request(:post, "www.example.com").with(:body => "abc")
69
+ stub_request(:post, "www.example.com").with(body: "abc")
64
70
  @sess.post_file("/", @file_path)
65
71
  end
66
72
 
67
73
  it "should raise same error as Patron if file is not readable for post request" do
68
- stub_request(:post, "www.example.com").with(:body => "abc")
69
- lambda {
74
+ stub_request(:post, "www.example.com").with(body: "abc")
75
+ expect {
70
76
  @sess.post_file("/", "/path/to/non/existing/file")
71
- }.should raise_error(ArgumentError, "Unable to open specified file.")
77
+ }.to raise_error(ArgumentError, "Unable to open specified file.")
72
78
  end
73
79
 
74
80
  end
@@ -76,14 +82,14 @@ unless RUBY_PLATFORM =~ /java/
76
82
  describe "handling errors same way as patron" do
77
83
  it "should raise error if put request has neither upload_data nor file_name" do
78
84
  stub_request(:post, "www.example.com")
79
- lambda {
85
+ expect {
80
86
  @sess.post("/", nil)
81
- }.should raise_error(ArgumentError, "Must provide either data or a filename when doing a PUT or POST")
87
+ }.to raise_error(ArgumentError, "Must provide either data or a filename when doing a PUT or POST")
82
88
  end
83
89
  end
84
90
 
85
91
  it "should work with WebDAV copy request" do
86
- stub_request(:copy, "www.example.com/abc").with(:headers => {'Destination' => "/def"})
92
+ stub_request(:copy, "www.example.com/abc").with(headers: {'Destination' => "/def"})
87
93
  @sess.copy("/abc", "/def")
88
94
  end
89
95
 
@@ -96,39 +102,22 @@ unless RUBY_PLATFORM =~ /java/
96
102
  Encoding.default_internal = @encoding
97
103
  end
98
104
 
99
- it "should encode body with default encoding" do
100
- stub_request(:get, "www.example.com").
101
- to_return(:body => "Øl")
102
-
103
- @sess.get("").body.encoding.should == Encoding::UTF_8
104
- end
105
-
106
- it "should encode body to default internal" do
105
+ it "should not encode body with default encoding" do
107
106
  stub_request(:get, "www.example.com").
108
- to_return(:headers => {'Content-Type' => 'text/html; charset=iso-8859-1'},
109
- :body => "Øl".encode("iso-8859-1"))
107
+ to_return(body: "Øl")
110
108
 
111
- @sess.get("").body.encoding.should == Encoding.default_internal
109
+ expect(@sess.get("").body.encoding).to eq(Encoding::ASCII_8BIT)
110
+ expect(@sess.get("").inspectable_body.encoding).to eq(Encoding::UTF_8)
112
111
  end
113
112
 
114
- it "should encode body based on encoding-attribute in body" do
113
+ it "should not encode body to default internal" do
115
114
  stub_request(:get, "www.example.com").
116
- to_return(:body => "<?xml encoding=\"iso-8859-1\">Øl</xml>".encode("iso-8859-1"))
115
+ to_return(headers: {'Content-Type' => 'text/html; charset=iso-8859-1'},
116
+ body: "Øl".encode("iso-8859-1"))
117
117
 
118
-
119
- @sess.get("").body.encoding.should == Encoding::ISO_8859_1
120
- end
121
-
122
-
123
- it "should encode body based on Session#default_response_charset" do
124
- stub_request(:get, "www.example.com").
125
- to_return(:body => "Øl".encode("iso-8859-1"))
126
-
127
- @sess.default_response_charset = "iso-8859-1"
128
-
129
- @sess.get("").body.encoding.should == Encoding::ISO_8859_1
118
+ expect(@sess.get("").body.encoding).to eq(Encoding::ASCII_8BIT)
119
+ expect(@sess.get("").decoded_body.encoding).to eq(Encoding.default_internal)
130
120
  end
131
-
132
121
  end
133
122
  end
134
123
  end
@@ -2,19 +2,23 @@ require 'ostruct'
2
2
 
3
3
  module PatronSpecHelper
4
4
  def http_request(method, uri, options = {}, &block)
5
+ method = method.to_sym
5
6
  uri = Addressable::URI.heuristic_parse(uri)
6
7
  sess = Patron::Session.new
7
- sess.base_url = "#{uri.omit(:userinfo, :path, :query).normalize.to_s}".gsub(/\/$/,"")
8
- sess.username = uri.user
9
- sess.password = uri.password
8
+ sess.base_url = "#{uri.omit(:path, :query).normalize.to_s}".gsub(/\/$/,"")
9
+
10
+ if options[:basic_auth]
11
+ sess.username = options[:basic_auth][0]
12
+ sess.password = options[:basic_auth][1]
13
+ end
10
14
 
11
15
  sess.connect_timeout = 30
12
16
  sess.timeout = 30
13
17
  sess.max_redirects = 0
14
18
  uri = "#{uri.path}#{uri.query ? '?' : ''}#{uri.query}"
15
- uri.gsub!(' ','+')
19
+ uri = uri.gsub(' ','%20')
16
20
  response = sess.request(method, uri, options[:headers] || {}, {
17
- :data => options[:body]
21
+ data: options[:body]
18
22
  })
19
23
  headers = {}
20
24
  if response.headers
@@ -24,14 +28,14 @@ module PatronSpecHelper
24
28
  end
25
29
  end
26
30
 
27
- status_line_pattern = %r(\AHTTP/(\d+\.\d+)\s+(\d\d\d)\s*([^\r\n]+)?)
28
- message = response.status_line.match(status_line_pattern)[3] || ""
31
+ status_line_pattern = %r(\AHTTP/(\d+(\.\d+)?)\s+(\d\d\d)\s*([^\r\n]+)?)
32
+ message = response.status_line.match(status_line_pattern)[4] || ""
29
33
 
30
34
  OpenStruct.new({
31
- :body => response.body,
32
- :headers => WebMock::Util::Headers.normalize_headers(headers),
33
- :status => response.status.to_s,
34
- :message => message
35
+ body: response.body,
36
+ headers: WebMock::Util::Headers.normalize_headers(headers),
37
+ status: response.status.to_s,
38
+ message: message
35
39
  })
36
40
  end
37
41
 
@@ -1,24 +1,33 @@
1
1
  shared_context "allowing and disabling net connect" do |*adapter_info|
2
2
  describe "when net connect" do
3
- describe "is allowed", :net_connect => true do
3
+ describe "is allowed", net_connect: true do
4
4
  before(:each) do
5
5
  WebMock.allow_net_connect!
6
6
  end
7
7
 
8
8
  it "should make a real web request if request is not stubbed" do
9
- http_request(:get, webmock_server_url).status.should == "200"
9
+ expect(http_request(:get, webmock_server_url).status).to eq("200")
10
10
  end
11
11
 
12
12
  it "should make a real https request if request is not stubbed" do
13
13
  unless http_library == :httpclient
14
- http_request(:get, "https://www.google.com/").
15
- body.should =~ /.*google.*/
14
+ result = http_request(:get, "https://www.google.com/").body
15
+ if result.respond_to? :encode
16
+ result = result.encode(
17
+ 'UTF-8',
18
+ 'binary',
19
+ invalid: :replace,
20
+ undef: :replace,
21
+ replace: ''
22
+ )
23
+ end
24
+ expect(result).to match(/.*google.*/)
16
25
  end
17
26
  end
18
27
 
19
28
  it "should return stubbed response if request was stubbed" do
20
- stub_request(:get, "www.example.com").to_return(:body => "abc")
21
- http_request(:get, "http://www.example.com/").body.should == "abc"
29
+ stub_request(:get, "www.example.com").to_return(body: "abc")
30
+ expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
22
31
  end
23
32
  end
24
33
 
@@ -28,115 +37,277 @@ shared_context "allowing and disabling net connect" do |*adapter_info|
28
37
  end
29
38
 
30
39
  it "should return stubbed response if request was stubbed" do
31
- stub_request(:get, "www.example.com").to_return(:body => "abc")
32
- http_request(:get, "http://www.example.com/").body.should == "abc"
40
+ stub_request(:get, "www.example.com").to_return(body: "abc")
41
+ expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
33
42
  end
34
43
 
35
44
  it "should return stubbed response if request with path was stubbed" do
36
- stub_request(:get, "www.example.com/hello_world").to_return(:body => "abc")
37
- http_request(:get, "http://www.example.com/hello_world").body.should == "abc"
45
+ stub_request(:get, "www.example.com/hello_world").to_return(body: "abc")
46
+ expect(http_request(:get, "http://www.example.com/hello_world").body).to eq("abc")
38
47
  end
39
48
 
40
49
  it "should raise exception if request was not stubbed" do
41
- lambda {
50
+ expect {
42
51
  http_request(:get, "http://www.example.com/")
43
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
52
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
44
53
  end
45
54
  end
46
55
 
47
56
  describe "is not allowed with exception for localhost" do
48
57
  before(:each) do
49
- WebMock.disable_net_connect!(:allow_localhost => true)
58
+ WebMock.disable_net_connect!(allow_localhost: true)
50
59
  end
51
60
 
52
61
  it "should return stubbed response if request was stubbed" do
53
- stub_request(:get, "www.example.com").to_return(:body => "abc")
54
- http_request(:get, "http://www.example.com/").body.should == "abc"
62
+ stub_request(:get, "www.example.com").to_return(body: "abc")
63
+ expect(http_request(:get, "http://www.example.com/").body).to eq("abc")
55
64
  end
56
65
 
57
66
  it "should raise exception if request was not stubbed" do
58
- lambda {
67
+ expect {
59
68
  http_request(:get, "http://www.example.com/")
60
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
69
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
61
70
  end
62
71
 
63
72
  it "should make a real request to localhost" do
64
- lambda {
73
+ expect {
65
74
  http_request(:get, "http://localhost:12345/")
66
- }.should raise_error(connection_refused_exception_class)
75
+ }.to raise_error(connection_refused_exception_class)
67
76
  end
68
77
 
69
78
  it "should make a real request to 127.0.0.1" do
70
- lambda {
79
+ expect {
71
80
  http_request(:get, "http://127.0.0.1:12345/")
72
- }.should raise_error(connection_refused_exception_class)
81
+ }.to raise_error(connection_refused_exception_class)
73
82
  end
74
83
 
75
84
  it "should make a real request to 0.0.0.0" do
76
- lambda {
85
+ expect {
77
86
  http_request(:get, "http://0.0.0.0:12345/")
78
- }.should raise_error(connection_refused_exception_class)
87
+ }.to raise_error(connection_refused_exception_class)
79
88
  end
80
89
  end
81
90
 
82
- describe "is not allowed with exception for allowed domains" do
83
- let(:host_with_port){ WebMockServer.instance.host_with_port }
91
+ describe "is not allowed, with exceptions" do
92
+ describe "allowing by host string" do
93
+ before :each do
94
+ WebMock.disable_net_connect!(allow: 'httpstat.us')
95
+ end
84
96
 
85
- before(:each) do
86
- WebMock.disable_net_connect!(:allow => ["www.example.org", host_with_port])
97
+ context "when the host is not allowed" do
98
+ it "should return stubbed response if request was stubbed" do
99
+ stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
100
+ expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
101
+ end
102
+
103
+ it "should raise exception if request was not stubbed" do
104
+ expect {
105
+ http_request(:get, 'http://disallowed.example.com/')
106
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
107
+ end
108
+ end
109
+
110
+ context "when the host is allowed" do
111
+ it "should return stubbed response if request was stubbed" do
112
+ stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
113
+ expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
114
+ end
115
+
116
+ # WARNING: this makes a real HTTP request!
117
+ it "should make a real request to allowed host", net_connect: true do
118
+ expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
119
+ end
120
+ end
87
121
  end
88
122
 
89
- context "when the host is not allowed" do
90
- it "should return stubbed response if request was stubbed" do
91
- stub_request(:get, "www.example.com").to_return(:body => "abc")
92
- http_request(:get, "http://www.example.com/").body.should == "abc"
123
+ describe "allowing by host:port string" do
124
+ def replace_with_different_port(uri)
125
+ uri.sub(%r{:(\d+)}){|m0, m1| ':' + ($~[1].to_i + 1).to_s }
126
+ end
127
+
128
+ let(:allowed_host_with_port) { WebMockServer.instance.host_with_port }
129
+ let(:disallowed_host_with_port) { replace_with_different_port(allowed_host_with_port) }
130
+
131
+ before :each do
132
+ WebMock.disable_net_connect!(allow: allowed_host_with_port)
93
133
  end
94
134
 
95
- it "should raise exception if request was not stubbed" do
96
- lambda {
97
- http_request(:get, "http://www.example.com/")
98
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
135
+ context "when the host is not allowed" do
136
+ it "should return stubbed response if request was stubbed" do
137
+ request_url = "http://#{disallowed_host_with_port}/foo"
138
+ stub_request(:get, request_url).to_return(body: "abc")
139
+ expect(http_request(:get, request_url).body).to eq("abc")
140
+ end
141
+
142
+ it "should raise exception if request was not stubbed" do
143
+ request_url = "http://#{disallowed_host_with_port}/foo"
144
+ expect {
145
+ http_request(:get, request_url)
146
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET #{request_url}))
147
+ end
148
+ end
149
+
150
+ context "when the host is allowed" do
151
+ it "should return stubbed response if request was stubbed" do
152
+ request_url = "http://#{allowed_host_with_port}/foo"
153
+ stub_request(:get, request_url).to_return(body: "abc")
154
+ expect(http_request(:get, request_url).body).to eq('abc')
155
+ end
156
+
157
+ it "should make a real request to allowed host", net_connect: true do
158
+ request_url = "http://#{allowed_host_with_port}/foo"
159
+ expect(http_request(:get, request_url).status).to eq('200')
160
+ end
99
161
  end
100
162
  end
101
163
 
102
- context "when the host with port is not allowed" do
103
- it "should return stubbed response if request was stubbed" do
104
- stub_request(:get, "http://localhost:2345").to_return(:body => "abc")
105
- http_request(:get, "http://localhost:2345/").body.should == "abc"
164
+ describe "allowing by scheme:host string" do
165
+ before :each do
166
+ WebMock.disable_net_connect!(allow: 'https://www.google.pl')
106
167
  end
107
168
 
108
- it "should raise exception if request was not stubbed" do
109
- lambda {
110
- http_request(:get, "http://localhost:2345/")
111
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://localhost:2345/))
169
+ context "when the host and scheme is not allowed" do
170
+ it "should return stubbed response if request was stubbed" do
171
+ stub_request(:get, 'https://disallowed.example.com/foo').to_return(body: "abc")
172
+ expect(http_request(:get, 'https://disallowed.example.com/foo').body).to eq("abc")
173
+ end
174
+
175
+ it "should raise exception if request was not stubbed" do
176
+ expect {
177
+ http_request(:get, 'https://disallowed.example.com/')
178
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET https://disallowed.example.com))
179
+ end
180
+
181
+ it "should raise exception if request was made to different port" do
182
+ expect {
183
+ http_request(:get, 'https://www.google.pl:80/')
184
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET https://www.google.pl:80))
185
+ end
186
+
187
+ it "should raise exception if request was made to different scheme" do
188
+ expect {
189
+ http_request(:get, 'http://www.google.pl/')
190
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.google.pl))
191
+ end
192
+ end
193
+
194
+ context "when the host is allowed" do
195
+ it "should return stubbed response if request was stubbed" do
196
+ stub_request(:get, 'https://www.google.pl').to_return(body: "abc")
197
+ expect(http_request(:get, "https://www.google.pl/").body).to eq("abc")
198
+ end
199
+
200
+ it "should make a real request to allowed host with scheme", net_connect: true do
201
+ method = http_library == :httpclient ? :head : :get #https://github.com/nahi/httpclient/issues/299
202
+ expect(http_request(method, "https://www.google.pl/").status).to eq('200')
203
+ end
204
+
205
+ it "should make a real request to allowed host with scheme and port", net_connect: true do
206
+ method = http_library == :httpclient ? :head : :get
207
+ expect(http_request(method, "https://www.google.pl:443/").status).to eq('200')
208
+ end
112
209
  end
113
210
  end
114
211
 
115
- context "when the host is allowed" do
116
- it "should raise exception if request was not stubbed" do
117
- lambda {
118
- http_request(:get, "http://www.example.com/")
119
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
212
+ describe "allowing by regular expression" do
213
+ before :each do
214
+ WebMock.disable_net_connect!(allow: %r{httpstat})
120
215
  end
121
216
 
122
- it "should make a real request to allowed host", :net_connect => true do
123
- http_request(:get, "http://www.example.org/").status.should == "302"
217
+ context "when the host is not allowed" do
218
+ it "should return stubbed response if request was stubbed" do
219
+ stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
220
+ expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
221
+ end
222
+
223
+ it "should raise exception if request was not stubbed" do
224
+ expect {
225
+ http_request(:get, 'http://disallowed.example.com/')
226
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
227
+ end
228
+ end
229
+
230
+ context "when the host is allowed" do
231
+ it "should return stubbed response if request was stubbed" do
232
+ stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
233
+ expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
234
+ end
235
+
236
+ # WARNING: this makes a real HTTP request!
237
+ it "should make a real request to allowed host", net_connect: true do
238
+ expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
239
+ end
240
+
241
+ it "should make a real request if request is allowed by path regexp and url contains default port", net_connect: true do
242
+ WebMock.disable_net_connect!(allow: %r{www.google.pl/webhp})
243
+ method = http_library == :httpclient ? :head : :get
244
+ expect(http_request(method, 'https://www.google.pl:443/webhp').status).to eq('200')
245
+ end
124
246
  end
125
247
  end
126
248
 
127
- context "when the host with port is allowed" do
128
- it "should make a real request to allowed host", :net_connect => true do
129
- http_request(:get, "http://#{host_with_port}/").status.should == "200"
249
+ describe "allowing by a callable" do
250
+ before :each do
251
+ WebMock.disable_net_connect!(allow: lambda{|url| url.to_str.include?('httpstat') })
252
+ end
253
+
254
+ context "when the host is not allowed" do
255
+ it "should return stubbed response if request was stubbed" do
256
+ stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
257
+ expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
258
+ end
259
+
260
+ it "should raise exception if request was not stubbed" do
261
+ expect {
262
+ http_request(:get, 'http://disallowed.example.com/')
263
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
264
+ end
265
+ end
266
+
267
+ context "when the host is allowed" do
268
+ it "should return stubbed response if request was stubbed" do
269
+ stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
270
+ expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
271
+ end
272
+
273
+ # WARNING: this makes a real HTTP request!
274
+ it "should make a real request to allowed host", net_connect: true do
275
+ expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
276
+ end
130
277
  end
131
278
  end
132
279
 
133
- context "when the host is allowed but not port" do
134
- it "should make a real request to allowed host", :net_connect => true do
135
- lambda {
136
- http_request(:get, "http://localhost:123/")
137
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://localhost:123/))
280
+ describe "allowing by a list of the above" do
281
+ before :each do
282
+ WebMock.disable_net_connect!(allow: [lambda{|_| false }, %r{foobar}, 'httpstat.us'])
283
+ end
284
+
285
+ context "when the host is not allowed" do
286
+ it "should return stubbed response if request was stubbed" do
287
+ stub_request(:get, 'disallowed.example.com/foo').to_return(body: "abc")
288
+ expect(http_request(:get, 'http://disallowed.example.com/foo').body).to eq("abc")
289
+ end
290
+
291
+ it "should raise exception if request was not stubbed" do
292
+ expect {
293
+ http_request(:get, 'http://disallowed.example.com/')
294
+ }.to raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://disallowed.example.com))
295
+ end
296
+ end
297
+
298
+ context "when the host is allowed" do
299
+ it "should return stubbed response if request was stubbed" do
300
+ stub_request(:get, 'httpstat.us/200').to_return(body: "abc")
301
+ expect(http_request(:get, "http://httpstat.us/200").body).to eq("abc")
302
+ end
303
+
304
+ # WARNING: this makes a real HTTP request!
305
+ it "should make a real request to allowed host", net_connect: true do
306
+ expect(http_request(:get, "http://httpstat.us/200").status).to eq('200')
307
+ end
138
308
  end
139
309
  end
310
+
140
311
  end
141
312
  end
142
313
  end