webmock 1.7.2 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.7.3
4
+
5
+ * Added `Get`, `Post`, `Delete`, `Put`, `Head`, `Option` constants to replaced `Net::HTTP` to make it possible to marshal objects with these constants assigned to properties. This fixed problem with `tvdb_party` gem which serializes HTTParty responses.
6
+
7
+ Thanks to [Klaus Hartl](https://github.com/carhartl) for reporting this issue.
8
+
3
9
  ## 1.7.2
4
10
 
5
11
  * Redefined `const_get` and `constants` methods on the replaced `Net::HTTP` to return same values as original `Net::HTTP`
@@ -16,8 +16,8 @@ module WebMock
16
16
  Net.send(:remove_const, :BufferedIO)
17
17
  Net.send(:remove_const, :HTTP)
18
18
  Net.send(:remove_const, :HTTPSession)
19
- Net.send(:const_set, :HTTP, Net::WebMockNetHTTP)
20
- Net.send(:const_set, :HTTPSession, Net::WebMockNetHTTP)
19
+ Net.send(:const_set, :HTTP, @webMockNetHTTP)
20
+ Net.send(:const_set, :HTTPSession, @webMockNetHTTP)
21
21
  Net.send(:const_set, :BufferedIO, Net::WebMockNetBufferedIO)
22
22
  end
23
23
 
@@ -29,6 +29,156 @@ module WebMock
29
29
  Net.send(:const_set, :HTTPSession, OriginalNetHTTP)
30
30
  Net.send(:const_set, :BufferedIO, OriginalNetBufferedIO)
31
31
  end
32
+
33
+ @webMockNetHTTP = Class.new(Net::HTTP) do
34
+ class << self
35
+ def socket_type_with_webmock
36
+ StubSocket
37
+ end
38
+ alias_method :socket_type_without_webmock, :socket_type
39
+ alias_method :socket_type, :socket_type_with_webmock
40
+
41
+ if Module.method(:const_defined?).arity == 1
42
+ def const_defined?(name)
43
+ super || self.superclass.const_defined?(name)
44
+ end
45
+ else
46
+ def const_defined?(name, inherit=true)
47
+ super || self.superclass.const_defined?(name, inherit)
48
+ end
49
+ end
50
+
51
+ if Module.method(:const_get).arity != 1
52
+ def const_get(name, inherit=true)
53
+ super
54
+ rescue NameError
55
+ self.superclass.const_get(name, inherit)
56
+ end
57
+ end
58
+
59
+ if Module.method(:constants).arity != 0
60
+ def constants(inherit=true)
61
+ super + self.superclass.constants(inherit)
62
+ end
63
+ end
64
+ end
65
+
66
+ def request_with_webmock(request, body = nil, &block)
67
+ request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)
68
+
69
+ WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
70
+
71
+ if WebMock::StubRegistry.instance.registered_request?(request_signature)
72
+ @socket = Net::HTTP.socket_type.new
73
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
74
+ WebMock::CallbackRegistry.invoke_callbacks(
75
+ {:lib => :net_http}, request_signature, webmock_response)
76
+ build_net_http_response(webmock_response, &block)
77
+ elsif WebMock.net_connect_allowed?(request_signature.uri)
78
+ check_right_http_connection
79
+ after_request = lambda do |response|
80
+ if WebMock::CallbackRegistry.any_callbacks?
81
+ webmock_response = build_webmock_response(response)
82
+ WebMock::CallbackRegistry.invoke_callbacks(
83
+ {:lib => :net_http, :real_request => true}, request_signature, webmock_response)
84
+ end
85
+ response.extend Net::WebMockHTTPResponse
86
+ block.call response if block
87
+ response
88
+ end
89
+ response = if (started? && !WebMock::Config.instance.net_http_connect_on_start) || !started?
90
+ @started = false #otherwise start_with_connect wouldn't execute and connect
91
+ start_with_connect {
92
+ response = request_without_webmock(request, nil)
93
+ after_request.call(response)
94
+ }
95
+ else
96
+ response = request_without_webmock(request, nil)
97
+ after_request.call(response)
98
+ end
99
+ else
100
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
101
+ end
102
+ end
103
+ alias_method :request_without_webmock, :request
104
+ alias_method :request, :request_with_webmock
105
+
106
+ def start_without_connect
107
+ raise IOError, 'HTTP session already opened' if @started
108
+ if block_given?
109
+ begin
110
+ @started = true
111
+ return yield(self)
112
+ ensure
113
+ do_finish
114
+ end
115
+ end
116
+ @started = true
117
+ self
118
+ end
119
+
120
+ def start_with_conditional_connect(&block)
121
+ if WebMock::Config.instance.net_http_connect_on_start
122
+ start_with_connect(&block)
123
+ else
124
+ start_without_connect(&block)
125
+ end
126
+ end
127
+ alias_method :start_with_connect, :start
128
+ alias_method :start, :start_with_conditional_connect
129
+
130
+ def build_net_http_response(webmock_response, &block)
131
+ response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
132
+ response.instance_variable_set(:@body, webmock_response.body)
133
+ webmock_response.headers.to_a.each do |name, values|
134
+ values = [values] unless values.is_a?(Array)
135
+ values.each do |value|
136
+ response.add_field(name, value)
137
+ end
138
+ end
139
+
140
+ response.instance_variable_set(:@read, true)
141
+
142
+ response.extend Net::WebMockHTTPResponse
143
+
144
+ raise Timeout::Error, "execution expired" if webmock_response.should_timeout
145
+
146
+ webmock_response.raise_error_if_any
147
+
148
+ yield response if block_given?
149
+
150
+ response
151
+ end
152
+
153
+ def build_webmock_response(net_http_response)
154
+ webmock_response = WebMock::Response.new
155
+ webmock_response.status = [
156
+ net_http_response.code.to_i,
157
+ net_http_response.message]
158
+ webmock_response.headers = net_http_response.to_hash
159
+ webmock_response.body = net_http_response.body
160
+ webmock_response
161
+ end
162
+
163
+
164
+ def check_right_http_connection
165
+ unless @@alredy_checked_for_right_http_connection ||= false
166
+ WebMock::NetHTTPUtility.puts_warning_for_right_http_if_needed
167
+ @@alredy_checked_for_right_http_connection = true
168
+ end
169
+ end
170
+ end
171
+ @webMockNetHTTP.version_1_2
172
+ [
173
+ [:Get, Net::HTTP::Get],
174
+ [:Post, Net::HTTP::Post],
175
+ [:Put, Net::HTTP::Put],
176
+ [:Delete, Net::HTTP::Delete],
177
+ [:Head, Net::HTTP::Head],
178
+ [:Options, Net::HTTP::Options]
179
+ ].each do |c|
180
+ @webMockNetHTTP.const_set(c[0], c[1])
181
+ end
32
182
  end
33
183
  end
34
184
  end
@@ -67,146 +217,6 @@ module Net #:nodoc: all
67
217
  alias_method :initialize, :initialize_with_webmock
68
218
  end
69
219
 
70
- class WebMockNetHTTP < HTTP
71
- class << self
72
- def socket_type_with_webmock
73
- StubSocket
74
- end
75
- alias_method :socket_type_without_webmock, :socket_type
76
- alias_method :socket_type, :socket_type_with_webmock
77
-
78
- if Module.method(:const_defined?).arity == 1
79
- def const_defined?(name)
80
- super || self.superclass.const_defined?(name)
81
- end
82
- else
83
- def const_defined?(name, inherit=true)
84
- super || self.superclass.const_defined?(name, inherit)
85
- end
86
- end
87
-
88
- if Module.method(:const_get).arity != 1
89
- def const_get(name, inherit=true)
90
- super
91
- rescue NameError
92
- self.superclass.const_get(name, inherit)
93
- end
94
- end
95
-
96
- if Module.method(:constants).arity != 0
97
- def constants(inherit=true)
98
- super + self.superclass.constants(inherit)
99
- end
100
- end
101
- end
102
-
103
- def request_with_webmock(request, body = nil, &block)
104
- request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)
105
-
106
- WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
107
-
108
- if WebMock::StubRegistry.instance.registered_request?(request_signature)
109
- @socket = Net::HTTP.socket_type.new
110
- webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
111
- WebMock::CallbackRegistry.invoke_callbacks(
112
- {:lib => :net_http}, request_signature, webmock_response)
113
- build_net_http_response(webmock_response, &block)
114
- elsif WebMock.net_connect_allowed?(request_signature.uri)
115
- check_right_http_connection
116
- after_request = lambda do |response|
117
- if WebMock::CallbackRegistry.any_callbacks?
118
- webmock_response = build_webmock_response(response)
119
- WebMock::CallbackRegistry.invoke_callbacks(
120
- {:lib => :net_http, :real_request => true}, request_signature, webmock_response)
121
- end
122
- response.extend Net::WebMockHTTPResponse
123
- block.call response if block
124
- response
125
- end
126
- response = if (started? && !WebMock::Config.instance.net_http_connect_on_start) || !started?
127
- @started = false #otherwise start_with_connect wouldn't execute and connect
128
- start_with_connect {
129
- response = request_without_webmock(request, nil)
130
- after_request.call(response)
131
- }
132
- else
133
- response = request_without_webmock(request, nil)
134
- after_request.call(response)
135
- end
136
- else
137
- raise WebMock::NetConnectNotAllowedError.new(request_signature)
138
- end
139
- end
140
- alias_method :request_without_webmock, :request
141
- alias_method :request, :request_with_webmock
142
-
143
- def start_without_connect
144
- raise IOError, 'HTTP session already opened' if @started
145
- if block_given?
146
- begin
147
- @started = true
148
- return yield(self)
149
- ensure
150
- do_finish
151
- end
152
- end
153
- @started = true
154
- self
155
- end
156
-
157
- def start_with_conditional_connect(&block)
158
- if WebMock::Config.instance.net_http_connect_on_start
159
- start_with_connect(&block)
160
- else
161
- start_without_connect(&block)
162
- end
163
- end
164
- alias_method :start_with_connect, :start
165
- alias_method :start, :start_with_conditional_connect
166
-
167
- def build_net_http_response(webmock_response, &block)
168
- response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
169
- response.instance_variable_set(:@body, webmock_response.body)
170
- webmock_response.headers.to_a.each do |name, values|
171
- values = [values] unless values.is_a?(Array)
172
- values.each do |value|
173
- response.add_field(name, value)
174
- end
175
- end
176
-
177
- response.instance_variable_set(:@read, true)
178
-
179
- response.extend Net::WebMockHTTPResponse
180
-
181
- raise Timeout::Error, "execution expired" if webmock_response.should_timeout
182
-
183
- webmock_response.raise_error_if_any
184
-
185
- yield response if block_given?
186
-
187
- response
188
- end
189
-
190
- def build_webmock_response(net_http_response)
191
- webmock_response = WebMock::Response.new
192
- webmock_response.status = [
193
- net_http_response.code.to_i,
194
- net_http_response.message]
195
- webmock_response.headers = net_http_response.to_hash
196
- webmock_response.body = net_http_response.body
197
- webmock_response
198
- end
199
-
200
-
201
- def check_right_http_connection
202
- unless @@alredy_checked_for_right_http_connection ||= false
203
- WebMock::NetHTTPUtility.puts_warning_for_right_http_if_needed
204
- @@alredy_checked_for_right_http_connection = true
205
- end
206
- end
207
- end
208
- Net::WebMockNetHTTP.version_1_2
209
-
210
220
  end
211
221
 
212
222
 
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.7.2' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.7.3' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -11,6 +11,51 @@ describe "Webmock with Net:HTTP" do
11
11
 
12
12
  let(:port){ WebMockServer.instance.port }
13
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
+
14
59
  describe "constants" do
15
60
  it "should still have const Get defined on replaced Net::HTTP" do
16
61
  Object.const_get("Net").const_get("HTTP").const_defined?("Get").should be_true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.7.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-16 00:00:00.000000000Z
12
+ date: 2011-08-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
16
- requirement: &2152397720 !ruby/object:Gem::Requirement
16
+ requirement: &2157028660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -24,10 +24,10 @@ dependencies:
24
24
  version: 2.2.5
25
25
  type: :runtime
26
26
  prerelease: false
27
- version_requirements: *2152397720
27
+ version_requirements: *2157028660
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: crack
30
- requirement: &2152396440 !ruby/object:Gem::Requirement
30
+ requirement: &2157027840 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ! '>='
@@ -35,10 +35,10 @@ dependencies:
35
35
  version: 0.1.7
36
36
  type: :runtime
37
37
  prerelease: false
38
- version_requirements: *2152396440
38
+ version_requirements: *2157027840
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: rspec
41
- requirement: &2152395720 !ruby/object:Gem::Requirement
41
+ requirement: &2157027140 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ! '>='
@@ -46,10 +46,10 @@ dependencies:
46
46
  version: 2.0.0
47
47
  type: :development
48
48
  prerelease: false
49
- version_requirements: *2152395720
49
+ version_requirements: *2157027140
50
50
  - !ruby/object:Gem::Dependency
51
51
  name: httpclient
52
- requirement: &2152395000 !ruby/object:Gem::Requirement
52
+ requirement: &2157026140 !ruby/object:Gem::Requirement
53
53
  none: false
54
54
  requirements:
55
55
  - - ! '>='
@@ -57,10 +57,10 @@ dependencies:
57
57
  version: 2.1.5.2
58
58
  type: :development
59
59
  prerelease: false
60
- version_requirements: *2152395000
60
+ version_requirements: *2157026140
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: patron
63
- requirement: &2152393780 !ruby/object:Gem::Requirement
63
+ requirement: &2157025680 !ruby/object:Gem::Requirement
64
64
  none: false
65
65
  requirements:
66
66
  - - ! '>='
@@ -68,10 +68,10 @@ dependencies:
68
68
  version: 0.4.9
69
69
  type: :development
70
70
  prerelease: false
71
- version_requirements: *2152393780
71
+ version_requirements: *2157025680
72
72
  - !ruby/object:Gem::Dependency
73
73
  name: em-http-request
74
- requirement: &2152393200 !ruby/object:Gem::Requirement
74
+ requirement: &2157025180 !ruby/object:Gem::Requirement
75
75
  none: false
76
76
  requirements:
77
77
  - - ! '>='
@@ -79,10 +79,10 @@ dependencies:
79
79
  version: 0.2.14
80
80
  type: :development
81
81
  prerelease: false
82
- version_requirements: *2152393200
82
+ version_requirements: *2157025180
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: curb
85
- requirement: &2152391900 !ruby/object:Gem::Requirement
85
+ requirement: &2157024720 !ruby/object:Gem::Requirement
86
86
  none: false
87
87
  requirements:
88
88
  - - ! '>='
@@ -90,10 +90,10 @@ dependencies:
90
90
  version: 0.7.8
91
91
  type: :development
92
92
  prerelease: false
93
- version_requirements: *2152391900
93
+ version_requirements: *2157024720
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: typhoeus
96
- requirement: &2152373560 !ruby/object:Gem::Requirement
96
+ requirement: &2157024260 !ruby/object:Gem::Requirement
97
97
  none: false
98
98
  requirements:
99
99
  - - ! '>='
@@ -101,10 +101,10 @@ dependencies:
101
101
  version: 0.2.4
102
102
  type: :development
103
103
  prerelease: false
104
- version_requirements: *2152373560
104
+ version_requirements: *2157024260
105
105
  - !ruby/object:Gem::Dependency
106
106
  name: minitest
107
- requirement: &2152372620 !ruby/object:Gem::Requirement
107
+ requirement: &2157023800 !ruby/object:Gem::Requirement
108
108
  none: false
109
109
  requirements:
110
110
  - - ! '>='
@@ -112,10 +112,10 @@ dependencies:
112
112
  version: 2.2.2
113
113
  type: :development
114
114
  prerelease: false
115
- version_requirements: *2152372620
115
+ version_requirements: *2157023800
116
116
  - !ruby/object:Gem::Dependency
117
117
  name: rdoc
118
- requirement: &2152371780 !ruby/object:Gem::Requirement
118
+ requirement: &2157023320 !ruby/object:Gem::Requirement
119
119
  none: false
120
120
  requirements:
121
121
  - - ! '>'
@@ -123,7 +123,7 @@ dependencies:
123
123
  version: 3.5.0
124
124
  type: :development
125
125
  prerelease: false
126
- version_requirements: *2152371780
126
+ version_requirements: *2157023320
127
127
  description: WebMock allows stubbing HTTP requests and setting expectations on HTTP
128
128
  requests.
129
129
  email: