webmock 1.8.11 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.9.0
4
+
5
+ * Added support for Typhoeus >= 0.5.0 and removed support for Typhoeus < 0.5.0.
6
+
7
+ Thanks to [Hans Hasselberg](https://github.com/i0rek)
8
+
3
9
  ## 1.8.11
4
10
 
5
11
  * Fix excon adapter to handle `:body => some_file_object`
@@ -14,53 +14,56 @@ if defined?(Typhoeus)
14
14
 
15
15
  def self.enable!
16
16
  @disabled = false
17
+ add_before_callback
17
18
  add_after_request_callback
18
- ::Typhoeus::Hydra.allow_net_connect = true
19
+ ::Typhoeus::Config.block_connection = true
19
20
  end
20
21
 
21
22
  def self.disable!
22
23
  @disabled = true
23
24
  remove_after_request_callback
24
- ::Typhoeus::Hydra.allow_net_connect = true
25
+ remove_before_callback
26
+ ::Typhoeus::Config.block_connection = false
25
27
  end
26
28
 
27
29
  def self.disabled?
28
30
  !!@disabled
29
31
  end
30
32
 
33
+ def self.add_before_callback
34
+ unless Typhoeus.before.include?(BEFORE_CALLBACK)
35
+ Typhoeus.before << BEFORE_CALLBACK
36
+ end
37
+ end
38
+
39
+ def self.remove_before_callback
40
+ Typhoeus.before.delete_if {|v| v == BEFORE_CALLBACK }
41
+ end
42
+
31
43
  def self.add_after_request_callback
32
- unless Typhoeus::Hydra.
33
- global_hooks[:after_request_before_on_complete].
34
- include?(AFTER_REQUEST_CALLBACK)
35
- Typhoeus::Hydra.
36
- global_hooks[:after_request_before_on_complete] << AFTER_REQUEST_CALLBACK
44
+ unless Typhoeus.on_complete.include?(AFTER_REQUEST_CALLBACK)
45
+ Typhoeus.on_complete << AFTER_REQUEST_CALLBACK
37
46
  end
38
47
  end
39
48
 
40
49
  def self.remove_after_request_callback
41
- Typhoeus::Hydra.global_hooks[:after_request_before_on_complete].
42
- delete_if {|v| v == AFTER_REQUEST_CALLBACK }
50
+ Typhoeus.on_complete.delete_if {|v| v == AFTER_REQUEST_CALLBACK }
43
51
  end
44
52
 
45
53
  def self.build_request_signature(req)
46
54
  uri = WebMock::Util::URI.heuristic_parse(req.url)
47
55
  uri.path = uri.normalized_path.gsub("[^:]//","/")
48
- if req.username
49
- uri.user = req.username
50
- uri.password = req.password
56
+ if req.options[:userpwd]
57
+ uri.user, uri.password = req.options[:userpwd].split(':')
51
58
  end
52
59
 
53
- body = req.body
54
-
55
- if req.params && req.method == :post
56
- body = request_body_for_post_request_with_params(req)
57
- end
60
+ body = req.options[:body]
58
61
 
59
62
  request_signature = WebMock::RequestSignature.new(
60
- req.method,
63
+ req.options[:method],
61
64
  uri.to_s,
62
65
  :body => body,
63
- :headers => req.headers
66
+ :headers => req.options[:headers]
64
67
  )
65
68
 
66
69
  req.instance_variable_set(:@__webmock_request_signature, request_signature)
@@ -68,45 +71,33 @@ if defined?(Typhoeus)
68
71
  request_signature
69
72
  end
70
73
 
71
- def self.request_body_for_post_request_with_params(req)
72
- params = req.params
73
- form = Typhoeus::Form.new(params)
74
- form.process!
75
- form.to_s
76
- end
77
-
78
74
  def self.build_webmock_response(typhoeus_response)
79
75
  webmock_response = WebMock::Response.new
80
76
  webmock_response.status = [typhoeus_response.code, typhoeus_response.status_message]
81
77
  webmock_response.body = typhoeus_response.body
82
- webmock_response.headers = typhoeus_response.headers_hash
78
+ webmock_response.headers = typhoeus_response.headers
83
79
  webmock_response
84
80
  end
85
81
 
86
- def self.stub_typhoeus(request_signature, webmock_response, typhoeus)
82
+ def self.generate_typhoeus_response(request_signature, webmock_response)
87
83
  response = if webmock_response.should_timeout
88
84
  ::Typhoeus::Response.new(
89
85
  :code => 0,
90
86
  :status_message => "",
91
87
  :body => "",
92
- :headers_hash => {},
93
- :curl_return_code => 28
88
+ :headers => {},
89
+ :return_code => :operation_timedout
94
90
  )
95
91
  else
96
92
  ::Typhoeus::Response.new(
97
93
  :code => webmock_response.status[0],
98
94
  :status_message => webmock_response.status[1],
99
95
  :body => webmock_response.body,
100
- :headers_hash => webmock_response.headers
96
+ :headers => webmock_response.headers
101
97
  )
102
98
  end
103
-
104
-
105
- typhoeus.stub(
106
- request_signature.method || :any,
107
- /.*/,
108
- :webmock_stub => true
109
- ).and_return(response)
99
+ response.mock = :webmock
100
+ response
110
101
  end
111
102
 
112
103
  def self.request_hash(request_signature)
@@ -118,12 +109,13 @@ if defined?(Typhoeus)
118
109
  hash
119
110
  end
120
111
 
121
- AFTER_REQUEST_CALLBACK = Proc.new do |request|
112
+ AFTER_REQUEST_CALLBACK = Proc.new do |response|
113
+ request = response.request
122
114
  request_signature = request.instance_variable_get(:@__webmock_request_signature)
123
115
  webmock_response =
124
116
  ::WebMock::HttpLibAdapters::TyphoeusAdapter.
125
- build_webmock_response(request.response)
126
- if request.response.mock?
117
+ build_webmock_response(response)
118
+ if response.mock
127
119
  WebMock::CallbackRegistry.invoke_callbacks(
128
120
  {:lib => :typhoeus},
129
121
  request_signature,
@@ -137,43 +129,29 @@ if defined?(Typhoeus)
137
129
  )
138
130
  end
139
131
  end
140
- end
141
- end
142
- end
143
-
144
-
145
- module Typhoeus
146
- class Hydra
147
- def queue_with_webmock(request)
148
- self.clear_webmock_stubs
149
-
150
- if WebMock::HttpLibAdapters::TyphoeusAdapter.disabled?
151
- return queue_without_webmock(request)
152
- end
153
132
 
154
- request_signature =
155
- ::WebMock::HttpLibAdapters::TyphoeusAdapter.build_request_signature(request)
156
-
157
- ::WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
158
-
159
- if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature)
160
- ::WebMock::HttpLibAdapters::TyphoeusAdapter.
161
- stub_typhoeus(request_signature, webmock_response, self)
162
- webmock_response.raise_error_if_any
163
- elsif !WebMock.net_connect_allowed?(request_signature.uri)
164
- raise WebMock::NetConnectNotAllowedError.new(request_signature)
133
+ BEFORE_CALLBACK = Proc.new do |request|
134
+ Typhoeus::Expectation.all.delete_if {|e| e.from == :webmock }
135
+ res = true
136
+
137
+ unless WebMock::HttpLibAdapters::TyphoeusAdapter.disabled?
138
+ request_signature = ::WebMock::HttpLibAdapters::TyphoeusAdapter.build_request_signature(request)
139
+ request.block_connection = false;
140
+
141
+ ::WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
142
+
143
+ if webmock_response = ::WebMock::StubRegistry.instance.response_for_request(request_signature)
144
+ # ::WebMock::HttpLibAdapters::TyphoeusAdapter.stub_typhoeus(request_signature, webmock_response, self)
145
+ response = ::WebMock::HttpLibAdapters::TyphoeusAdapter.generate_typhoeus_response(request_signature, webmock_response)
146
+ request.finish(response)
147
+ webmock_response.raise_error_if_any
148
+ res = false
149
+ elsif !WebMock.net_connect_allowed?(request_signature.uri)
150
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
151
+ end
152
+ end
153
+ res
165
154
  end
166
- queue_without_webmock(request)
167
- end
168
-
169
- alias_method :queue_without_webmock, :queue
170
- alias_method :queue, :queue_with_webmock
171
-
172
- def clear_webmock_stubs
173
- self.stubs = [] unless self.stubs
174
- self.stubs.delete_if {|s|
175
- s.instance_variable_get(:@options)[:webmock_stub]
176
- }
177
155
  end
178
156
  end
179
157
  end
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.8.11' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.9.0' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -6,6 +6,11 @@ unless RUBY_PLATFORM =~ /java/
6
6
 
7
7
  describe "Typhoeus::Hydra" do
8
8
  include TyphoeusHydraSpecHelper
9
+ let(:hydra) { Typhoeus::Hydra.new }
10
+
11
+ before do
12
+ Typhoeus::Expectation.clear
13
+ end
9
14
 
10
15
  include_context "with WebMock"
11
16
 
@@ -17,13 +22,17 @@ unless RUBY_PLATFORM =~ /java/
17
22
 
18
23
  describe "when params are used" do
19
24
  it "should take into account params for POST request" do
20
- stub_request(:post, "www.example.com").with(:body => {:hello => 'world'})
21
- Typhoeus::Request.post("www.example.com", :params => {:hello => 'world'})
25
+ stub_request(:post, "www.example.com").with(:params => {:hello => 'world'})
26
+ request = Typhoeus::Request.new("http://www.example.com", :method => :post, :params => {:hello => 'world'})
27
+ hydra.queue(request)
28
+ hydra.run
22
29
  end
23
30
 
24
31
  it "should take into account params for GET request" do
25
- stub_request(:get, "http://www.example.com/?hello=world")
26
- Typhoeus::Request.get("www.example.com", :params => {:hello => 'world'})
32
+ stub_request(:get, "http://www.example.com/?hello=world").to_return({})
33
+ request = Typhoeus::Request.new("http://www.example.com/?hello=world", :method => :get)
34
+ hydra.queue(request)
35
+ hydra.run
27
36
  end
28
37
  end
29
38
 
@@ -31,15 +40,16 @@ unless RUBY_PLATFORM =~ /java/
31
40
  it "should support native typhoeus timeouts" do
32
41
  stub_request(:any, "example.com").to_timeout
33
42
 
34
- response = Typhoeus::Request.get("http://example.com")
43
+ request = Typhoeus::Request.new("http://example.com", :method => :get)
44
+ hydra.queue(request)
45
+ hydra.run
35
46
 
36
- response.should be_timed_out
47
+ request.response.should be_timed_out
37
48
  end
38
49
  end
39
50
 
40
51
  describe "callbacks" do
41
52
  before(:each) do
42
- @hydra = Typhoeus::Hydra.new
43
53
  @request = Typhoeus::Request.new("http://example.com")
44
54
  end
45
55
 
@@ -48,11 +58,11 @@ unless RUBY_PLATFORM =~ /java/
48
58
  stub_request(:any, "example.com").to_return(:body => body)
49
59
 
50
60
  test = nil
51
- @hydra.on_complete do |c|
61
+ Typhoeus.on_complete do |c|
52
62
  test = c.body
53
63
  end
54
- @hydra.queue @request
55
- @hydra.run
64
+ hydra.queue @request
65
+ hydra.run
56
66
  test.should == body
57
67
  end
58
68
 
@@ -61,11 +71,11 @@ unless RUBY_PLATFORM =~ /java/
61
71
  stub_request(:any, "example.com").to_return(:status => [response_code, "Server On Fire"])
62
72
 
63
73
  test = nil
64
- @hydra.on_complete do |c|
74
+ Typhoeus.on_complete do |c|
65
75
  test = c.code
66
76
  end
67
- @hydra.queue @request
68
- @hydra.run
77
+ hydra.queue @request
78
+ hydra.run
69
79
  test.should == response_code
70
80
  end
71
81
  end
@@ -15,15 +15,16 @@ module TyphoeusHydraSpecHelper
15
15
  :timeout => 25000
16
16
  }
17
17
  )
18
- hydra = Typhoeus::Hydra.new(:initial_pool_size => 0)
18
+ hydra = Typhoeus::Hydra.new
19
19
  hydra.queue(request)
20
20
  hydra.run
21
+
21
22
  response = request.response
23
+ raise FakeTyphoeusHydraConnectError.new if response.return_code == :couldnt_connect
22
24
  raise FakeTyphoeusHydraTimeoutError.new if response.timed_out?
23
- raise FakeTyphoeusHydraConnectError.new if response.code == 0
24
25
  OpenStruct.new({
25
26
  :body => response.body,
26
- :headers => WebMock::Util::Headers.normalize_headers(join_array_values(response.headers_hash)),
27
+ :headers => WebMock::Util::Headers.normalize_headers(join_array_values(response.headers)),
27
28
  :status => response.code.to_s,
28
29
  :message => response.status_message
29
30
  })
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
23
23
  s.add_development_dependency 'em-http-request', '>= 1.0.2'
24
24
  s.add_development_dependency 'em-synchrony', '>= 1.0.0' if RUBY_VERSION >= "1.9"
25
25
  s.add_development_dependency 'curb', '>= 0.8.0' unless RUBY_PLATFORM =~ /java/
26
- s.add_development_dependency 'typhoeus', '>= 0.3.3' unless RUBY_PLATFORM =~ /java/
26
+ s.add_development_dependency 'typhoeus', '>= 0.5.0' unless RUBY_PLATFORM =~ /java/
27
27
  s.add_development_dependency 'excon', '>= 0.11.0'
28
28
  s.add_development_dependency 'minitest', '>= 2.2.2'
29
29
  s.add_development_dependency 'rdoc', ((RUBY_VERSION == '1.8.6') ? '<= 3.5.0' : '>3.5.0')
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 51
5
+ prerelease:
5
6
  segments:
6
7
  - 1
7
- - 8
8
- - 11
9
- version: 1.8.11
8
+ - 9
9
+ - 0
10
+ version: 1.9.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Bartosz Blimke
@@ -14,162 +15,183 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2012-09-29 00:00:00 +02:00
18
- default_executable:
18
+ date: 2012-11-07 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- type: :runtime
21
+ name: addressable
22
22
  version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
23
24
  requirements:
24
25
  - - ">="
25
26
  - !ruby/object:Gem::Version
27
+ hash: 9
26
28
  segments:
27
29
  - 2
28
30
  - 2
29
31
  - 7
30
32
  version: 2.2.7
31
- name: addressable
32
- requirement: *id001
33
+ type: :runtime
33
34
  prerelease: false
35
+ requirement: *id001
34
36
  - !ruby/object:Gem::Dependency
35
- type: :runtime
37
+ name: crack
36
38
  version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
37
40
  requirements:
38
41
  - - ">="
39
42
  - !ruby/object:Gem::Version
43
+ hash: 21
40
44
  segments:
41
45
  - 0
42
46
  - 1
43
47
  - 7
44
48
  version: 0.1.7
45
- name: crack
46
- requirement: *id002
49
+ type: :runtime
47
50
  prerelease: false
51
+ requirement: *id002
48
52
  - !ruby/object:Gem::Dependency
49
- type: :development
53
+ name: rspec
50
54
  version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
51
56
  requirements:
52
57
  - - ~>
53
58
  - !ruby/object:Gem::Version
59
+ hash: 23
54
60
  segments:
55
61
  - 2
56
62
  - 10
57
63
  version: "2.10"
58
- name: rspec
59
- requirement: *id003
64
+ type: :development
60
65
  prerelease: false
66
+ requirement: *id003
61
67
  - !ruby/object:Gem::Dependency
62
- type: :development
68
+ name: httpclient
63
69
  version_requirements: &id004 !ruby/object:Gem::Requirement
70
+ none: false
64
71
  requirements:
65
72
  - - ">="
66
73
  - !ruby/object:Gem::Version
74
+ hash: 15
67
75
  segments:
68
76
  - 2
69
77
  - 2
70
78
  - 4
71
79
  version: 2.2.4
72
- name: httpclient
73
- requirement: *id004
80
+ type: :development
74
81
  prerelease: false
82
+ requirement: *id004
75
83
  - !ruby/object:Gem::Dependency
76
- type: :development
84
+ name: patron
77
85
  version_requirements: &id005 !ruby/object:Gem::Requirement
86
+ none: false
78
87
  requirements:
79
88
  - - ">="
80
89
  - !ruby/object:Gem::Version
90
+ hash: 43
81
91
  segments:
82
92
  - 0
83
93
  - 4
84
94
  - 18
85
95
  version: 0.4.18
86
- name: patron
87
- requirement: *id005
96
+ type: :development
88
97
  prerelease: false
98
+ requirement: *id005
89
99
  - !ruby/object:Gem::Dependency
90
- type: :development
100
+ name: em-http-request
91
101
  version_requirements: &id006 !ruby/object:Gem::Requirement
102
+ none: false
92
103
  requirements:
93
104
  - - ">="
94
105
  - !ruby/object:Gem::Version
106
+ hash: 19
95
107
  segments:
96
108
  - 1
97
109
  - 0
98
110
  - 2
99
111
  version: 1.0.2
100
- name: em-http-request
101
- requirement: *id006
112
+ type: :development
102
113
  prerelease: false
114
+ requirement: *id006
103
115
  - !ruby/object:Gem::Dependency
104
- type: :development
116
+ name: curb
105
117
  version_requirements: &id007 !ruby/object:Gem::Requirement
118
+ none: false
106
119
  requirements:
107
120
  - - ">="
108
121
  - !ruby/object:Gem::Version
122
+ hash: 63
109
123
  segments:
110
124
  - 0
111
125
  - 8
112
126
  - 0
113
127
  version: 0.8.0
114
- name: curb
115
- requirement: *id007
128
+ type: :development
116
129
  prerelease: false
130
+ requirement: *id007
117
131
  - !ruby/object:Gem::Dependency
118
- type: :development
132
+ name: typhoeus
119
133
  version_requirements: &id008 !ruby/object:Gem::Requirement
134
+ none: false
120
135
  requirements:
121
136
  - - ">="
122
137
  - !ruby/object:Gem::Version
138
+ hash: 11
123
139
  segments:
124
140
  - 0
125
- - 3
126
- - 3
127
- version: 0.3.3
128
- name: typhoeus
129
- requirement: *id008
141
+ - 5
142
+ - 0
143
+ version: 0.5.0
144
+ type: :development
130
145
  prerelease: false
146
+ requirement: *id008
131
147
  - !ruby/object:Gem::Dependency
132
- type: :development
148
+ name: excon
133
149
  version_requirements: &id009 !ruby/object:Gem::Requirement
150
+ none: false
134
151
  requirements:
135
152
  - - ">="
136
153
  - !ruby/object:Gem::Version
154
+ hash: 51
137
155
  segments:
138
156
  - 0
139
157
  - 11
140
158
  - 0
141
159
  version: 0.11.0
142
- name: excon
143
- requirement: *id009
160
+ type: :development
144
161
  prerelease: false
162
+ requirement: *id009
145
163
  - !ruby/object:Gem::Dependency
146
- type: :development
164
+ name: minitest
147
165
  version_requirements: &id010 !ruby/object:Gem::Requirement
166
+ none: false
148
167
  requirements:
149
168
  - - ">="
150
169
  - !ruby/object:Gem::Version
170
+ hash: 3
151
171
  segments:
152
172
  - 2
153
173
  - 2
154
174
  - 2
155
175
  version: 2.2.2
156
- name: minitest
157
- requirement: *id010
176
+ type: :development
158
177
  prerelease: false
178
+ requirement: *id010
159
179
  - !ruby/object:Gem::Dependency
160
- type: :development
180
+ name: rdoc
161
181
  version_requirements: &id011 !ruby/object:Gem::Requirement
182
+ none: false
162
183
  requirements:
163
184
  - - ">"
164
185
  - !ruby/object:Gem::Version
186
+ hash: 19
165
187
  segments:
166
188
  - 3
167
189
  - 5
168
190
  - 0
169
191
  version: 3.5.0
170
- name: rdoc
171
- requirement: *id011
192
+ type: :development
172
193
  prerelease: false
194
+ requirement: *id011
173
195
  description: WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.
174
196
  email:
175
197
  - bartosz.blimke@gmail.com
@@ -295,7 +317,6 @@ files:
295
317
  - test/test_helper.rb
296
318
  - test/test_webmock.rb
297
319
  - webmock.gemspec
298
- has_rdoc: true
299
320
  homepage: http://github.com/bblimke/webmock
300
321
  licenses: []
301
322
 
@@ -305,23 +326,27 @@ rdoc_options: []
305
326
  require_paths:
306
327
  - lib
307
328
  required_ruby_version: !ruby/object:Gem::Requirement
329
+ none: false
308
330
  requirements:
309
331
  - - ">="
310
332
  - !ruby/object:Gem::Version
333
+ hash: 3
311
334
  segments:
312
335
  - 0
313
336
  version: "0"
314
337
  required_rubygems_version: !ruby/object:Gem::Requirement
338
+ none: false
315
339
  requirements:
316
340
  - - ">="
317
341
  - !ruby/object:Gem::Version
342
+ hash: 3
318
343
  segments:
319
344
  - 0
320
345
  version: "0"
321
346
  requirements: []
322
347
 
323
348
  rubyforge_project: webmock
324
- rubygems_version: 1.3.6
349
+ rubygems_version: 1.8.19
325
350
  signing_key:
326
351
  specification_version: 3
327
352
  summary: Library for stubbing HTTP requests in Ruby.