webmock 1.9.1 → 1.9.2

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.
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.9.2
4
+
5
+ * Added support for Excon's :response_block parameter
6
+
7
+ Thanks to [Myron Marston](https://github.com/myronmarston) for reporting this issue.
8
+
3
9
  ## 1.9.1
4
10
 
5
11
  * Fix 'rack.errors' not being set for Rack apps
@@ -64,11 +64,11 @@ if defined?(Excon)
64
64
  :headers => mock.headers
65
65
  end
66
66
 
67
- def self.mock_response(real)
67
+ def self.mock_response(real, response_body_from_chunks)
68
68
  mock = WebMock::Response.new
69
69
  mock.status = real.status
70
70
  mock.headers = real.headers
71
- mock.body = real.body
71
+ mock.body = response_body_from_chunks || real.body
72
72
  mock
73
73
  end
74
74
 
@@ -81,7 +81,7 @@ if defined?(Excon)
81
81
 
82
82
  class ExconConnection < ::Excon::Connection
83
83
 
84
- def request_kernel(params, &block)
84
+ def request_kernel(params)
85
85
  mock_request = ExconAdapter.build_request params.dup
86
86
  WebMock::RequestRegistry.instance.requested_signatures.put(mock_request)
87
87
 
@@ -91,13 +91,31 @@ if defined?(Excon)
91
91
 
92
92
  if params.has_key?(:expects) && ![*params[:expects]].include?(response.status)
93
93
  raise(Excon::Errors.status_error(params, response))
94
- else
95
- response
94
+ elsif params.has_key?(:response_block)
95
+ content_length = remaining = response.body.bytesize
96
+ body = response.body
97
+ response.body = ""
98
+ i = 0
99
+ while i < body.length
100
+ params[:response_block].call(body[i, params[:chunk_size]], [remaining - params[:chunk_size], 0].max, content_length)
101
+ remaining -= params[:chunk_size]
102
+ i += params[:chunk_size]
103
+ end
96
104
  end
105
+ response
97
106
 
98
107
  elsif WebMock.net_connect_allowed?(mock_request.uri)
108
+ response_body_from_chunks = nil
109
+ if params.has_key?(:response_block)
110
+ response_body_from_chunks = ""
111
+ original_block = params[:response_block]
112
+ params[:response_block] = lambda {|chunk, remaining, total|
113
+ response_body_from_chunks << chunk
114
+ original_block.call(chunk, remaining, total)
115
+ }
116
+ end
99
117
  real_response = super
100
- ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response), :real_request => true)
118
+ ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response, response_body_from_chunks), :real_request => true)
101
119
  real_response
102
120
  else
103
121
  raise WebMock::NetConnectNotAllowedError.new(mock_request)
@@ -1,3 +1,3 @@
1
1
  module WebMock
2
- VERSION = '1.9.1' unless defined?(::WebMock::VERSION)
2
+ VERSION = '1.9.2' unless defined?(::WebMock::VERSION)
3
3
  end
@@ -16,6 +16,37 @@ describe "Excon" do
16
16
  lambda { Excon.get('http://example.com', :expects => 204) }.should raise_error(Excon::Errors::OK)
17
17
  end
18
18
 
19
+ context "with response_block" do
20
+ it "should support excon response_block for real requests" do
21
+ a = []
22
+ WebMock.allow_net_connect!
23
+ r = Excon.get('http://httpstat.us/200', :response_block => lambda {|e,remaining, total| a << e}, :chunk_size => 1)
24
+ a.should == ["2", "0", "0", " ", "O", "K"]
25
+ r.body.should == ""
26
+ end
27
+
28
+ it "should support excon response_block" do
29
+ a = []
30
+ stub_request(:get, "http://example.com/").to_return(:body => "abc")
31
+ r = Excon.get('http://example.com', :response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
32
+ a.should == ['a', 'b', 'c']
33
+ r.body.should == ""
34
+ end
35
+
36
+ it "should invoke callbacks with response body even if a real request is made" do
37
+ a = []
38
+ WebMock.allow_net_connect!
39
+ response = nil
40
+ WebMock.after_request { |_, res|
41
+ response = res
42
+ }
43
+ r = Excon.get('http://httpstat.us/200', :response_block => lambda {|e,remaining, total| a << e}, :chunk_size => 1)
44
+ response.body.should == "200 OK"
45
+ a.should == ["2", "0", "0", " ", "O", "K"]
46
+ r.body.should == ""
47
+ end
48
+ end
49
+
19
50
  let(:file) { File.new(__FILE__) }
20
51
  let(:file_contents) { File.new(__FILE__).read }
21
52
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webmock
3
3
  version: !ruby/object:Gem::Version
4
- hash: 49
4
+ hash: 55
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 9
9
- - 1
10
- version: 1.9.1
9
+ - 2
10
+ version: 1.9.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Bartosz Blimke