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
@@ -1,6 +1,7 @@
1
1
  shared_context "callbacks" do |*adapter_info|
2
2
  describe "when after_request callback is declared" do
3
3
  before(:each) do
4
+ @called = nil
4
5
  WebMock.reset_callbacks
5
6
  stub_request(:get, "http://www.example.com")
6
7
  end
@@ -9,7 +10,7 @@ shared_context "callbacks" do |*adapter_info|
9
10
  WebMock.after_request {
10
11
  @called = true
11
12
  }
12
- @called.should == nil
13
+ expect(@called).to eq(nil)
13
14
  end
14
15
 
15
16
  it "should invoke a callback after request is made" do
@@ -17,31 +18,31 @@ shared_context "callbacks" do |*adapter_info|
17
18
  @called = true
18
19
  }
19
20
  http_request(:get, "http://www.example.com/")
20
- @called.should == true
21
+ expect(@called).to eq(true)
21
22
  end
22
23
 
23
24
  it "should not invoke a callback if this http library should be ignored" do
24
- WebMock.after_request(:except => [http_library()]) {
25
+ WebMock.after_request(except: [http_library()]) {
25
26
  @called = true
26
27
  }
27
28
  http_request(:get, "http://www.example.com/")
28
- @called.should == nil
29
+ expect(@called).to eq(nil)
29
30
  end
30
31
 
31
32
  it "should invoke a callback even if other http libraries should be ignored" do
32
- WebMock.after_request(:except => [:other_lib]) {
33
+ WebMock.after_request(except: [:other_lib]) {
33
34
  @called = true
34
35
  }
35
36
  http_request(:get, "http://www.example.com/")
36
- @called.should == true
37
+ expect(@called).to eq(true)
37
38
  end
38
39
 
39
40
  it "should pass request signature to the callback" do
40
- WebMock.after_request(:except => [:other_lib]) do |request_signature, _|
41
+ WebMock.after_request(except: [:other_lib]) do |request_signature, _|
41
42
  @request_signature = request_signature
42
43
  end
43
44
  http_request(:get, "http://www.example.com/")
44
- @request_signature.uri.to_s.should == "http://www.example.com:80/"
45
+ expect(@request_signature.uri.to_s).to eq("http://www.example.com:80/")
45
46
  end
46
47
 
47
48
  after(:each) do
@@ -60,7 +61,7 @@ shared_context "callbacks" do |*adapter_info|
60
61
  end
61
62
 
62
63
  http_request(:get, "http://www.example.com/")
63
- global_stub_request_sig.should be(after_request_request_sig)
64
+ expect(global_stub_request_sig).to be(after_request_request_sig)
64
65
  end
65
66
 
66
67
  context "passing response to callback" do
@@ -68,53 +69,54 @@ shared_context "callbacks" do |*adapter_info|
68
69
  before(:each) do
69
70
  stub_request(:get, "http://www.example.com").
70
71
  to_return(
71
- :status => ["200", "hello"],
72
- :headers => {'Content-Length' => '666', 'Hello' => 'World'},
73
- :body => "foo bar"
72
+ status: [200, "hello"],
73
+ headers: {'Content-Length' => '666', 'Hello' => 'World'},
74
+ body: "foo bar"
74
75
  )
75
- WebMock.after_request(:except => [:other_lib]) do |_, response|
76
+ WebMock.after_request(except: [:other_lib]) do |_, response|
76
77
  @response = response
77
78
  end
78
79
  http_request(:get, "http://www.example.com/")
79
80
  end
80
81
 
81
82
  it "should pass response to callback with the status and message" do
82
- @response.status.should == ["200", "hello"]
83
+ expect(@response.status).to eq([200, "hello"])
83
84
  end
84
85
 
85
86
  it "should pass response to callback with headers" do
86
- @response.headers.should == {
87
+ expect(@response.headers).to eq({
87
88
  'Content-Length' => '666',
88
89
  'Hello' => 'World'
89
- }
90
+ })
90
91
  end
91
92
 
92
93
  it "should pass response to callback with body" do
93
- @response.body.should == "foo bar"
94
+ expect(@response.body).to eq("foo bar")
94
95
  end
95
96
  end
96
97
 
97
- describe "when request is not stubbed", :net_connect => true do
98
+ describe "when request is not stubbed", net_connect: true do
98
99
  before(:each) do
99
100
  WebMock.reset!
100
101
  WebMock.allow_net_connect!
101
- WebMock.after_request(:except => [:other_lib]) do |_, response|
102
+ WebMock.after_request(except: [:other_lib]) do |_, response|
102
103
  @response = response
103
104
  end
104
- http_request(:get, "http://www.example.com/")
105
+ http_request(:get, "http://httpstat.us/201", headers: { "Accept" => "*" })
105
106
  end
106
107
 
107
108
  it "should pass real response to callback with status and message" do
108
- @response.status[0].should == 302
109
- @response.status[1].should == "Found" unless adapter_info.include?(:no_status_message)
109
+ expect(@response.status[0]).to eq(201)
110
+ expect(@response.status[1]).to eq("Created") unless adapter_info.include?(:no_status_message)
110
111
  end
111
112
 
112
113
  it "should pass real response to callback with headers" do
113
- @response.headers["Content-Length"].should == "0"
114
+ expect(@response.headers["X-Powered-By"]).to eq( "ASP.NET")
115
+ expect(@response.headers["Content-Length"]).to eq("11") unless adapter_info.include?(:no_content_length_header)
114
116
  end
115
117
 
116
118
  it "should pass response to callback with body" do
117
- @response.body.size.should == 0
119
+ expect(@response.body.size).to eq(11)
118
120
  end
119
121
  end
120
122
  end
@@ -123,16 +125,16 @@ shared_context "callbacks" do |*adapter_info|
123
125
  WebMock.after_request { @called = 1 }
124
126
  WebMock.after_request { @called += 1 }
125
127
  http_request(:get, "http://www.example.com/")
126
- @called.should == 2
128
+ expect(@called).to eq(2)
127
129
  end
128
130
 
129
- it "should invoke callbacks only for real requests if requested", :net_connect => true do
130
- WebMock.after_request(:real_requests_only => true) { @called = true }
131
+ it "should invoke callbacks only for real requests if requested", net_connect: true do
132
+ WebMock.after_request(real_requests_only: true) { @called = true }
131
133
  http_request(:get, "http://www.example.com/")
132
- @called.should == nil
134
+ expect(@called).to eq(nil)
133
135
  WebMock.allow_net_connect!
134
136
  http_request(:get, "http://www.example.net/")
135
- @called.should == true
137
+ expect(@called).to eq(true)
136
138
  end
137
139
 
138
140
  it "should not invoke any callbacks after callbacks were reset" do
@@ -140,7 +142,7 @@ shared_context "callbacks" do |*adapter_info|
140
142
  WebMock.reset_callbacks
141
143
  stub_request(:get, "http://www.example.com/")
142
144
  http_request(:get, "http://www.example.com/")
143
- @called.should == nil
145
+ expect(@called).to eq(nil)
144
146
  end
145
147
  end
146
148
  end
@@ -7,15 +7,30 @@ shared_context "complex cross-concern behaviors" do |*adapter_info|
7
7
  real_response = http_request(:get, webmock_server_url)
8
8
 
9
9
  stub_request(:get, webmock_server_url).to_return(
10
- :status => recorded_response.status,
11
- :body => recorded_response.body,
12
- :headers => recorded_response.headers
10
+ status: recorded_response.status,
11
+ body: recorded_response.body,
12
+ headers: recorded_response.headers
13
13
  )
14
14
 
15
15
  played_back_response = http_request(:get, webmock_server_url)
16
16
 
17
- played_back_response.headers.keys.should include('Set-Cookie')
18
- played_back_response.should == real_response
17
+ expect(played_back_response.headers.keys).to include('Set-Cookie')
18
+ expect(played_back_response).to eq(real_response)
19
+ end
20
+
21
+ let(:no_content_url) { 'http://httpstat.us/204' }
22
+ [nil, ''].each do |stub_val|
23
+ it "returns the same value (nil or "") for a request stubbed as #{stub_val.inspect} that a real empty response has", net_connect: true do
24
+ unless http_library == :curb
25
+ WebMock.allow_net_connect!
26
+
27
+ real_response = http_request(:get, no_content_url)
28
+ stub_request(:get, no_content_url).to_return(status: 204, body: stub_val)
29
+ stubbed_response = http_request(:get, no_content_url)
30
+
31
+ expect(stubbed_response.body).to eq(real_response.body)
32
+ end
33
+ end
19
34
  end
20
35
  end
21
36
 
@@ -19,7 +19,7 @@ shared_context "enabled and disabled webmock" do |*adapter_info|
19
19
 
20
20
  describe "when webmock is disabled except this lib" do
21
21
  before(:each) do
22
- WebMock.disable!(:except => [http_library])
22
+ WebMock.disable!(except: [http_library])
23
23
  end
24
24
  after(:each) do
25
25
  WebMock.enable!
@@ -30,7 +30,7 @@ shared_context "enabled and disabled webmock" do |*adapter_info|
30
30
  describe "when webmock is enabled except this lib" do
31
31
  before(:each) do
32
32
  WebMock.disable!
33
- WebMock.enable!(:except => [http_library])
33
+ WebMock.enable!(except: [http_library])
34
34
  end
35
35
  after(:each) do
36
36
  WebMock.enable!
@@ -42,18 +42,18 @@ end
42
42
  shared_context "disabled WebMock" do
43
43
  it "should not register executed requests" do
44
44
  http_request(:get, webmock_server_url)
45
- a_request(:get, webmock_server_url).should_not have_been_made
45
+ expect(a_request(:get, webmock_server_url)).not_to have_been_made
46
46
  end
47
47
 
48
48
  it "should not block unstubbed requests" do
49
- lambda {
49
+ expect {
50
50
  http_request(:get, webmock_server_url)
51
- }.should_not raise_error
51
+ }.not_to raise_error
52
52
  end
53
53
 
54
54
  it "should return real response even if there are stubs" do
55
- stub_request(:get, /.*/).to_return(:body => "x")
56
- http_request(:get, webmock_server_url).body.should == "hello world"
55
+ stub_request(:get, /.*/).to_return(body: "x")
56
+ expect(http_request(:get, webmock_server_url).body).to eq("hello world")
57
57
  end
58
58
 
59
59
  it "should not invoke any callbacks" do
@@ -62,7 +62,7 @@ shared_context "disabled WebMock" do
62
62
  @called = nil
63
63
  WebMock.after_request { @called = 1 }
64
64
  http_request(:get, webmock_server_url)
65
- @called.should == nil
65
+ expect(@called).to eq(nil)
66
66
  end
67
67
  end
68
68
 
@@ -70,18 +70,18 @@ shared_context "enabled WebMock" do
70
70
  it "should register executed requests" do
71
71
  WebMock.allow_net_connect!
72
72
  http_request(:get, webmock_server_url)
73
- a_request(:get, webmock_server_url).should have_been_made
73
+ expect(a_request(:get, webmock_server_url)).to have_been_made
74
74
  end
75
75
 
76
76
  it "should block unstubbed requests" do
77
- lambda {
77
+ expect {
78
78
  http_request(:get, "http://www.example.com/")
79
- }.should raise_error(WebMock::NetConnectNotAllowedError)
79
+ }.to raise_error(WebMock::NetConnectNotAllowedError)
80
80
  end
81
81
 
82
82
  it "should return stubbed response" do
83
- stub_request(:get, /.*/).to_return(:body => "x")
84
- http_request(:get, "http://www.example.com/").body.should == "x"
83
+ stub_request(:get, /.*/).to_return(body: "x")
84
+ expect(http_request(:get, "http://www.example.com/").body).to eq("x")
85
85
  end
86
86
 
87
87
  it "should invoke callbacks" do
@@ -90,6 +90,6 @@ shared_context "enabled WebMock" do
90
90
  @called = nil
91
91
  WebMock.after_request { @called = 1 }
92
92
  http_request(:get, webmock_server_url)
93
- @called.should == 1
93
+ expect(@called).to eq(1)
94
94
  end
95
95
  end
@@ -1,15 +1,15 @@
1
1
  shared_context "precedence of stubs" do |*adapter_info|
2
2
  describe "when choosing a matching request stub" do
3
3
  it "should use the last declared matching request stub" do
4
- stub_request(:get, "www.example.com").to_return(:body => "abc")
5
- stub_request(:get, "www.example.com").to_return(:body => "def")
6
- http_request(:get, "http://www.example.com/").body.should == "def"
4
+ stub_request(:get, "www.example.com").to_return(body: "abc")
5
+ stub_request(:get, "www.example.com").to_return(body: "def")
6
+ expect(http_request(:get, "http://www.example.com/").body).to eq("def")
7
7
  end
8
8
 
9
9
  it "should not be affected by the type of uri or request method" do
10
- stub_request(:get, "www.example.com").to_return(:body => "abc")
11
- stub_request(:any, /.*example.*/).to_return(:body => "def")
12
- http_request(:get, "http://www.example.com/").body.should == "def"
10
+ stub_request(:get, "www.example.com").to_return(body: "abc")
11
+ stub_request(:any, /.*example.*/).to_return(body: "def")
12
+ expect(http_request(:get, "http://www.example.com/").body).to eq("def")
13
13
  end
14
14
  end
15
15
  end