webmock 1.10.2 → 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +6 -0
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +68 -53
- data/lib/webmock/version.rb +1 -1
- data/spec/acceptance/excon/excon_spec.rb +6 -6
- metadata +3 -3
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.11.0
|
4
|
+
|
5
|
+
* Excon >= 0.17 support.
|
6
|
+
|
7
|
+
Thanks to [Nathan Sutton](https://github.com/nate) for reporting this issue and to [Wesley Beary](https://github.com/geemus) and [Myron Marston](https://github.com/myronmarston) for help.
|
8
|
+
|
3
9
|
## 1.10.2
|
4
10
|
|
5
11
|
* '+' in request path is treated as plus, but in query params always as a space.
|
@@ -11,18 +11,71 @@ if defined?(Excon)
|
|
11
11
|
module HttpLibAdapters
|
12
12
|
|
13
13
|
class ExconAdapter < HttpLibAdapter
|
14
|
+
PARAMS_TO_DELETE = [:expects, :idempotent,
|
15
|
+
:instrumentor_name, :instrumentor,
|
16
|
+
:response_block, :request_block,
|
17
|
+
:__construction_args, :stack,
|
18
|
+
:connection, :response]
|
19
|
+
|
14
20
|
adapter_for :excon
|
15
21
|
|
16
22
|
def self.enable!
|
17
|
-
|
18
|
-
Excon.send(:const_set, :Connection, ExconConnection)
|
23
|
+
self.add_excon_stub
|
19
24
|
end
|
20
25
|
|
21
26
|
def self.disable!
|
22
|
-
|
23
|
-
|
27
|
+
self.remove_excon_stub
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.add_excon_stub
|
31
|
+
if not @stub
|
32
|
+
@original_excon_mock_default = ::Excon.defaults[:mock]
|
33
|
+
::Excon.defaults[:mock] = true
|
34
|
+
@stub = ::Excon.stub({}) do |params|
|
35
|
+
self.handle_request(params)
|
36
|
+
end
|
37
|
+
end
|
24
38
|
end
|
25
39
|
|
40
|
+
def self.remove_excon_stub
|
41
|
+
::Excon.defaults[:mock] = @original_excon_mock_default
|
42
|
+
@original_excon_mock_default = nil
|
43
|
+
Excon.stubs.delete(@stub)
|
44
|
+
@stub = nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.handle_request(params)
|
48
|
+
mock_request = self.build_request params.dup
|
49
|
+
WebMock::RequestRegistry.instance.requested_signatures.put(mock_request)
|
50
|
+
|
51
|
+
if mock_response = WebMock::StubRegistry.instance.response_for_request(mock_request)
|
52
|
+
self.perform_callbacks(mock_request, mock_response, :real_request => false)
|
53
|
+
response = self.real_response(mock_response)
|
54
|
+
response
|
55
|
+
elsif WebMock.net_connect_allowed?(mock_request.uri)
|
56
|
+
conn = new_excon_connection(params)
|
57
|
+
real_response = conn.request(scrub_params_from(params.merge(:mock => false)))
|
58
|
+
|
59
|
+
ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response), :real_request => true)
|
60
|
+
|
61
|
+
real_response.data
|
62
|
+
else
|
63
|
+
raise WebMock::NetConnectNotAllowedError.new(mock_request)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.new_excon_connection(params)
|
68
|
+
# Ensure the connection is constructed with the exact same args
|
69
|
+
# that the orginal connection was constructed with.
|
70
|
+
args = params.fetch(:__construction_args)
|
71
|
+
::Excon::Connection.new(scrub_params_from args.merge(:mock => false))
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.scrub_params_from(hash)
|
75
|
+
hash = hash.dup
|
76
|
+
PARAMS_TO_DELETE.each { |key| hash.delete(key) }
|
77
|
+
hash
|
78
|
+
end
|
26
79
|
|
27
80
|
def self.to_query(hash)
|
28
81
|
string = ""
|
@@ -58,17 +111,18 @@ if defined?(Excon)
|
|
58
111
|
def self.real_response(mock)
|
59
112
|
raise Excon::Errors::Timeout if mock.should_timeout
|
60
113
|
mock.raise_error_if_any
|
61
|
-
|
114
|
+
{
|
62
115
|
:body => mock.body,
|
63
116
|
:status => mock.status[0].to_i,
|
64
|
-
:headers => mock.headers
|
117
|
+
:headers => mock.headers || {}
|
118
|
+
}
|
65
119
|
end
|
66
120
|
|
67
|
-
def self.mock_response(real
|
121
|
+
def self.mock_response(real)
|
68
122
|
mock = WebMock::Response.new
|
69
123
|
mock.status = real.status
|
70
124
|
mock.headers = real.headers
|
71
|
-
mock.body =
|
125
|
+
mock.body = real.body
|
72
126
|
mock
|
73
127
|
end
|
74
128
|
|
@@ -76,54 +130,15 @@ if defined?(Excon)
|
|
76
130
|
return unless WebMock::CallbackRegistry.any_callbacks?
|
77
131
|
WebMock::CallbackRegistry.invoke_callbacks(options.merge(:lib => :excon), request, response)
|
78
132
|
end
|
79
|
-
|
80
133
|
end
|
134
|
+
end
|
135
|
+
end
|
81
136
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
WebMock::RequestRegistry.instance.requested_signatures.put(mock_request)
|
87
|
-
|
88
|
-
if mock_response = WebMock::StubRegistry.instance.response_for_request(mock_request)
|
89
|
-
ExconAdapter.perform_callbacks(mock_request, mock_response, :real_request => false)
|
90
|
-
response = ExconAdapter.real_response(mock_response)
|
91
|
-
|
92
|
-
if params.has_key?(:expects) && ![*params[:expects]].include?(response.status)
|
93
|
-
raise(Excon::Errors.status_error(params, 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
|
104
|
-
end
|
105
|
-
response
|
106
|
-
|
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
|
117
|
-
real_response = super
|
118
|
-
ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response, response_body_from_chunks), :real_request => true)
|
119
|
-
real_response
|
120
|
-
else
|
121
|
-
raise WebMock::NetConnectNotAllowedError.new(mock_request)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
137
|
+
Excon::Connection.class_eval do
|
138
|
+
def self.new(args)
|
139
|
+
super.tap do |instance|
|
140
|
+
instance.data[:__construction_args] = args
|
125
141
|
end
|
126
142
|
end
|
127
143
|
end
|
128
|
-
|
129
144
|
end
|
data/lib/webmock/version.rb
CHANGED
@@ -8,19 +8,19 @@ describe "Excon" do
|
|
8
8
|
|
9
9
|
it 'should allow Excon requests to use query hash paramters' do
|
10
10
|
stub_request(:get, "http://example.com/resource/?a=1&b=2").to_return(:body => "abc")
|
11
|
-
Excon.
|
11
|
+
Excon.new('http://example.com').get(:path => "resource/", :query => {:a => 1, :b => 2}).body.should == "abc"
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'should support Excon :expects options' do
|
15
15
|
stub_request(:get, "http://example.com/").to_return(:body => 'a')
|
16
|
-
lambda { Excon.
|
16
|
+
lambda { Excon.new('http://example.com').get(:expects => 204) }.should raise_error(Excon::Errors::OK)
|
17
17
|
end
|
18
18
|
|
19
19
|
context "with response_block" do
|
20
20
|
it "should support excon response_block for real requests" do
|
21
21
|
a = []
|
22
22
|
WebMock.allow_net_connect!
|
23
|
-
r = Excon.
|
23
|
+
r = Excon.new('http://httpstat.us/200').get(:response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
|
24
24
|
a.should == ["2", "0", "0", " ", "O", "K"]
|
25
25
|
r.body.should == ""
|
26
26
|
end
|
@@ -28,7 +28,7 @@ describe "Excon" do
|
|
28
28
|
it "should support excon response_block" do
|
29
29
|
a = []
|
30
30
|
stub_request(:get, "http://example.com/").to_return(:body => "abc")
|
31
|
-
r = Excon.
|
31
|
+
r = Excon.new('http://example.com').get(:response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
|
32
32
|
a.should == ['a', 'b', 'c']
|
33
33
|
r.body.should == ""
|
34
34
|
end
|
@@ -40,7 +40,7 @@ describe "Excon" do
|
|
40
40
|
WebMock.after_request { |_, res|
|
41
41
|
response = res
|
42
42
|
}
|
43
|
-
r = Excon.
|
43
|
+
r = Excon.new('http://httpstat.us/200').get(:response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
|
44
44
|
response.body.should == "200 OK"
|
45
45
|
a.should == ["2", "0", "0", " ", "O", "K"]
|
46
46
|
r.body.should == ""
|
@@ -58,7 +58,7 @@ describe "Excon" do
|
|
58
58
|
yielded_request_body = req.body
|
59
59
|
end
|
60
60
|
|
61
|
-
Excon.
|
61
|
+
Excon.new("http://example.com").put(:path => "upload", :body => file)
|
62
62
|
|
63
63
|
yielded_request_body.should eq(file_contents)
|
64
64
|
end
|