webmock 1.24.6 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +0 -9
- data/CHANGELOG.md +37 -8
- data/Gemfile +1 -5
- data/README.md +22 -51
- data/Rakefile +0 -23
- data/lib/webmock.rb +0 -12
- data/lib/webmock/cucumber.rb +2 -0
- data/lib/webmock/http_lib_adapters/curb_adapter.rb +9 -3
- data/lib/webmock/http_lib_adapters/em_http_request_adapter.rb +220 -9
- data/lib/webmock/http_lib_adapters/excon_adapter.rb +2 -0
- data/lib/webmock/http_lib_adapters/http_rb/response.rb +1 -1
- data/lib/webmock/http_lib_adapters/httpclient_adapter.rb +0 -38
- data/lib/webmock/http_lib_adapters/manticore_adapter.rb +6 -2
- data/lib/webmock/http_lib_adapters/net_http.rb +2 -10
- data/lib/webmock/http_lib_adapters/patron_adapter.rb +7 -3
- data/lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb +6 -2
- data/lib/webmock/minitest.rb +2 -0
- data/lib/webmock/request_pattern.rb +16 -1
- data/lib/webmock/response.rb +0 -7
- data/lib/webmock/rspec.rb +2 -0
- data/lib/webmock/test_unit.rb +2 -0
- data/lib/webmock/util/headers.rb +4 -0
- data/lib/webmock/util/query_mapper.rb +1 -2
- data/lib/webmock/version.rb +1 -1
- data/lib/webmock/webmock.rb +0 -2
- data/spec/acceptance/curb/curb_spec_helper.rb +6 -3
- data/spec/acceptance/em_http_request/em_http_request_spec_helper.rb +4 -1
- data/spec/acceptance/excon/excon_spec_helper.rb +9 -3
- data/spec/acceptance/http_rb/http_rb_spec_helper.rb +7 -1
- data/spec/acceptance/httpclient/httpclient_spec_helper.rb +5 -2
- data/spec/acceptance/manticore/manticore_spec_helper.rb +5 -1
- data/spec/acceptance/net_http/net_http_spec.rb +2 -2
- data/spec/acceptance/net_http/net_http_spec_helper.rb +4 -1
- data/spec/acceptance/patron/patron_spec_helper.rb +6 -3
- data/spec/acceptance/shared/allowing_and_disabling_net_connect.rb +2 -2
- data/spec/acceptance/shared/request_expectations.rb +64 -7
- data/spec/acceptance/shared/stubbing_requests.rb +51 -1
- data/spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb +11 -8
- data/spec/spec_helper.rb +0 -4
- data/spec/unit/request_pattern_spec.rb +1 -1
- data/spec/unit/util/query_mapper_spec.rb +0 -16
- data/webmock.gemspec +5 -7
- metadata +187 -236
- data/Appraisals +0 -3
- data/gemfiles/ruby_1_8.gemfile +0 -21
- data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_0_x.rb +0 -147
- data/lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb +0 -228
data/Appraisals
DELETED
data/gemfiles/ruby_1_8.gemfile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
# This file was generated by Appraisal
|
2
|
-
|
3
|
-
source "http://rubygems.org/"
|
4
|
-
|
5
|
-
gem "addressable", ">= 2.3.6", "< 2.4.0"
|
6
|
-
|
7
|
-
gem "curb", "< 0.9.2"
|
8
|
-
|
9
|
-
group :development do
|
10
|
-
gem "rake", '~> 10.5.0'
|
11
|
-
end
|
12
|
-
|
13
|
-
group :test do
|
14
|
-
gem "minitest_tu_shim", "1.3.2"
|
15
|
-
end
|
16
|
-
|
17
|
-
platforms :jruby do
|
18
|
-
gem "jruby-openssl"
|
19
|
-
end
|
20
|
-
|
21
|
-
gemspec :path => "../"
|
@@ -1,147 +0,0 @@
|
|
1
|
-
if defined?(EventMachine::HttpRequest)
|
2
|
-
module WebMock
|
3
|
-
module HttpLibAdapters
|
4
|
-
class EmHttpRequestAdapter < HttpLibAdapter
|
5
|
-
adapter_for :em_http_request
|
6
|
-
|
7
|
-
OriginalHttpRequest = EventMachine::HttpRequest unless const_defined?(:OriginalHttpRequest)
|
8
|
-
|
9
|
-
def self.enable!
|
10
|
-
EventMachine.send(:remove_const, :HttpRequest)
|
11
|
-
EventMachine.send(:const_set, :HttpRequest, EventMachine::WebMockHttpRequest)
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.disable!
|
15
|
-
EventMachine.send(:remove_const, :HttpRequest)
|
16
|
-
EventMachine.send(:const_set, :HttpRequest, OriginalHttpRequest)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
module EventMachine
|
24
|
-
class WebMockHttpRequest < EventMachine::HttpRequest
|
25
|
-
|
26
|
-
include HttpEncoding
|
27
|
-
|
28
|
-
class WebMockHttpClient < EventMachine::HttpClient
|
29
|
-
|
30
|
-
def setup(response, uri, error = nil)
|
31
|
-
@last_effective_url = @uri = uri
|
32
|
-
if error
|
33
|
-
on_error(error)
|
34
|
-
fail(self)
|
35
|
-
else
|
36
|
-
EM.next_tick do
|
37
|
-
receive_data(response)
|
38
|
-
succeed(self)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def unbind
|
44
|
-
end
|
45
|
-
|
46
|
-
def close_connection
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def send_request(&block)
|
51
|
-
request_signature = build_request_signature
|
52
|
-
|
53
|
-
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
54
|
-
|
55
|
-
if webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
56
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
57
|
-
{:lib => :em_http_request}, request_signature, webmock_response)
|
58
|
-
client = WebMockHttpClient.new(nil)
|
59
|
-
client.on_error("WebMock timeout error") if webmock_response.should_timeout
|
60
|
-
client.setup(make_raw_response(webmock_response), @uri,
|
61
|
-
webmock_response.should_timeout ? "WebMock timeout error" : nil)
|
62
|
-
client
|
63
|
-
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
64
|
-
http = super
|
65
|
-
http.callback {
|
66
|
-
if WebMock::CallbackRegistry.any_callbacks?
|
67
|
-
webmock_response = build_webmock_response(http)
|
68
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
69
|
-
{:lib => :em_http_request, :real_request => true}, request_signature,
|
70
|
-
webmock_response)
|
71
|
-
end
|
72
|
-
}
|
73
|
-
http
|
74
|
-
else
|
75
|
-
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
private
|
80
|
-
|
81
|
-
def build_webmock_response(http)
|
82
|
-
webmock_response = WebMock::Response.new
|
83
|
-
webmock_response.status = [http.response_header.status, http.response_header.http_reason]
|
84
|
-
webmock_response.headers = http.response_header
|
85
|
-
webmock_response.body = http.response
|
86
|
-
webmock_response
|
87
|
-
end
|
88
|
-
|
89
|
-
def build_request_signature
|
90
|
-
if @req
|
91
|
-
options = @req.options
|
92
|
-
method = @req.method
|
93
|
-
uri = @req.uri.dup
|
94
|
-
else
|
95
|
-
options = @options
|
96
|
-
method = @method
|
97
|
-
uri = @uri.dup
|
98
|
-
end
|
99
|
-
|
100
|
-
if options[:authorization] || options['authorization']
|
101
|
-
auth = (options[:authorization] || options['authorization'])
|
102
|
-
userinfo = auth.join(':')
|
103
|
-
userinfo = WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)
|
104
|
-
options.reject! {|k,v| k.to_s == 'authorization' } #we added it to url userinfo
|
105
|
-
uri.userinfo = userinfo
|
106
|
-
end
|
107
|
-
|
108
|
-
uri.query = encode_query(@req.uri, options[:query]).slice(/\?(.*)/, 1)
|
109
|
-
|
110
|
-
body = options[:body] || options['body']
|
111
|
-
body = form_encode_body(body) if body.is_a?(Hash)
|
112
|
-
|
113
|
-
WebMock::RequestSignature.new(
|
114
|
-
method.downcase.to_sym,
|
115
|
-
uri.to_s,
|
116
|
-
:body => body,
|
117
|
-
:headers => (options[:head] || options['head'])
|
118
|
-
)
|
119
|
-
end
|
120
|
-
|
121
|
-
|
122
|
-
def make_raw_response(response)
|
123
|
-
response.raise_error_if_any
|
124
|
-
|
125
|
-
status, headers, body = response.status, response.headers, response.body
|
126
|
-
|
127
|
-
response_string = []
|
128
|
-
response_string << "HTTP/1.1 #{status[0]} #{status[1]}"
|
129
|
-
|
130
|
-
headers.each do |header, value|
|
131
|
-
value = value.join(", ") if value.is_a?(Array)
|
132
|
-
|
133
|
-
# WebMock's internal processing will not handle the body
|
134
|
-
# correctly if the header indicates that it is chunked, unless
|
135
|
-
# we also create all the chunks.
|
136
|
-
# It's far easier just to remove the header.
|
137
|
-
next if header =~ /transfer-encoding/i && value =~/chunked/i
|
138
|
-
|
139
|
-
response_string << "#{header}: #{value}"
|
140
|
-
end if headers
|
141
|
-
|
142
|
-
response_string << "" << body
|
143
|
-
response_string.join("\n")
|
144
|
-
end
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
@@ -1,228 +0,0 @@
|
|
1
|
-
if defined?(EventMachine::HttpClient)
|
2
|
-
module WebMock
|
3
|
-
module HttpLibAdapters
|
4
|
-
class EmHttpRequestAdapter < HttpLibAdapter
|
5
|
-
adapter_for :em_http_request
|
6
|
-
|
7
|
-
OriginalHttpClient = EventMachine::HttpClient unless const_defined?(:OriginalHttpClient)
|
8
|
-
OriginalHttpConnection = EventMachine::HttpConnection unless const_defined?(:OriginalHttpConnection)
|
9
|
-
|
10
|
-
def self.enable!
|
11
|
-
EventMachine.send(:remove_const, :HttpConnection)
|
12
|
-
EventMachine.send(:const_set, :HttpConnection, EventMachine::WebMockHttpConnection)
|
13
|
-
EventMachine.send(:remove_const, :HttpClient)
|
14
|
-
EventMachine.send(:const_set, :HttpClient, EventMachine::WebMockHttpClient)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.disable!
|
18
|
-
EventMachine.send(:remove_const, :HttpConnection)
|
19
|
-
EventMachine.send(:const_set, :HttpConnection, OriginalHttpConnection)
|
20
|
-
EventMachine.send(:remove_const, :HttpClient)
|
21
|
-
EventMachine.send(:const_set, :HttpClient, OriginalHttpClient)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
module EventMachine
|
28
|
-
if defined?(Synchrony) && HTTPMethods.instance_methods.include?(:aget)
|
29
|
-
# have to make the callbacks fire on the next tick in order
|
30
|
-
# to avoid the dreaded "double resume" exception
|
31
|
-
module HTTPMethods
|
32
|
-
%w[get head post delete put].each do |type|
|
33
|
-
class_eval %[
|
34
|
-
def #{type}(options = {}, &blk)
|
35
|
-
f = Fiber.current
|
36
|
-
|
37
|
-
conn = setup_request(:#{type}, options, &blk)
|
38
|
-
conn.callback { EM.next_tick { f.resume(conn) } }
|
39
|
-
conn.errback { EM.next_tick { f.resume(conn) } }
|
40
|
-
|
41
|
-
Fiber.yield
|
42
|
-
end
|
43
|
-
]
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class WebMockHttpConnection < HttpConnection
|
49
|
-
def activate_connection(client)
|
50
|
-
request_signature = client.request_signature
|
51
|
-
|
52
|
-
if client.stubbed_webmock_response
|
53
|
-
conn = HttpStubConnection.new rand(10000)
|
54
|
-
post_init
|
55
|
-
|
56
|
-
@deferred = false
|
57
|
-
@conn = conn
|
58
|
-
|
59
|
-
conn.parent = self
|
60
|
-
conn.pending_connect_timeout = @connopts.connect_timeout
|
61
|
-
conn.comm_inactivity_timeout = @connopts.inactivity_timeout
|
62
|
-
|
63
|
-
finalize_request(client)
|
64
|
-
@conn.set_deferred_status :succeeded
|
65
|
-
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
66
|
-
super
|
67
|
-
else
|
68
|
-
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def drop_client
|
73
|
-
@clients.shift
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
class WebMockHttpClient < EventMachine::HttpClient
|
78
|
-
include HttpEncoding
|
79
|
-
|
80
|
-
def uri
|
81
|
-
@req.uri
|
82
|
-
end
|
83
|
-
|
84
|
-
def setup(response, uri, error = nil)
|
85
|
-
@last_effective_url = @uri = uri
|
86
|
-
if error
|
87
|
-
on_error(error)
|
88
|
-
@conn.drop_client
|
89
|
-
fail(self)
|
90
|
-
else
|
91
|
-
@conn.receive_data(response)
|
92
|
-
succeed(self)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def send_request(head, body)
|
97
|
-
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
|
98
|
-
|
99
|
-
if stubbed_webmock_response
|
100
|
-
WebMock::CallbackRegistry.invoke_callbacks({:lib => :em_http_request}, request_signature, stubbed_webmock_response)
|
101
|
-
@uri ||= nil
|
102
|
-
EM.next_tick {
|
103
|
-
setup(make_raw_response(stubbed_webmock_response), @uri,
|
104
|
-
stubbed_webmock_response.should_timeout ? "WebMock timeout error" : nil)
|
105
|
-
}
|
106
|
-
self
|
107
|
-
elsif WebMock.net_connect_allowed?(request_signature.uri)
|
108
|
-
super
|
109
|
-
else
|
110
|
-
raise WebMock::NetConnectNotAllowedError.new(request_signature)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def unbind(reason = nil)
|
115
|
-
if !stubbed_webmock_response && WebMock::CallbackRegistry.any_callbacks?
|
116
|
-
webmock_response = build_webmock_response
|
117
|
-
WebMock::CallbackRegistry.invoke_callbacks(
|
118
|
-
{:lib => :em_http_request, :real_request => true},
|
119
|
-
request_signature,
|
120
|
-
webmock_response)
|
121
|
-
end
|
122
|
-
@request_signature = nil
|
123
|
-
remove_instance_variable(:@stubbed_webmock_response)
|
124
|
-
|
125
|
-
super
|
126
|
-
end
|
127
|
-
|
128
|
-
def request_signature
|
129
|
-
@request_signature ||= build_request_signature
|
130
|
-
end
|
131
|
-
|
132
|
-
def stubbed_webmock_response
|
133
|
-
unless defined?(@stubbed_webmock_response)
|
134
|
-
@stubbed_webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
|
135
|
-
end
|
136
|
-
|
137
|
-
@stubbed_webmock_response
|
138
|
-
end
|
139
|
-
|
140
|
-
def get_response_cookie(name)
|
141
|
-
name = name.to_s
|
142
|
-
|
143
|
-
raw_cookie = response_header.cookie
|
144
|
-
raw_cookie = [raw_cookie] if raw_cookie.is_a? String
|
145
|
-
|
146
|
-
cookie = raw_cookie.select { |c| c.start_with? name }.first
|
147
|
-
cookie and cookie.split('=', 2)[1]
|
148
|
-
end
|
149
|
-
|
150
|
-
private
|
151
|
-
|
152
|
-
def build_webmock_response
|
153
|
-
webmock_response = WebMock::Response.new
|
154
|
-
webmock_response.status = [response_header.status, response_header.http_reason]
|
155
|
-
webmock_response.headers = response_header
|
156
|
-
webmock_response.body = response
|
157
|
-
webmock_response
|
158
|
-
end
|
159
|
-
|
160
|
-
def build_request_signature
|
161
|
-
headers, body = @req.headers, @req.body
|
162
|
-
|
163
|
-
@conn.middleware.select {|m| m.respond_to?(:request) }.each do |m|
|
164
|
-
headers, body = m.request(self, headers, body)
|
165
|
-
end
|
166
|
-
|
167
|
-
method = @req.method
|
168
|
-
uri = @req.uri.clone
|
169
|
-
auth = @req.headers[:'proxy-authorization']
|
170
|
-
query = @req.query
|
171
|
-
|
172
|
-
if auth
|
173
|
-
userinfo = auth.join(':')
|
174
|
-
userinfo = WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)
|
175
|
-
if @req
|
176
|
-
@req.proxy.reject! {|k,v| t.to_s == 'authorization' }
|
177
|
-
else
|
178
|
-
options.reject! {|k,v| k.to_s == 'authorization' } #we added it to url userinfo
|
179
|
-
end
|
180
|
-
uri.userinfo = userinfo
|
181
|
-
end
|
182
|
-
|
183
|
-
uri.query = encode_query(@req.uri, query).slice(/\?(.*)/, 1)
|
184
|
-
|
185
|
-
body = form_encode_body(body) if body.is_a?(Hash)
|
186
|
-
|
187
|
-
WebMock::RequestSignature.new(
|
188
|
-
method.downcase.to_sym,
|
189
|
-
uri.to_s,
|
190
|
-
:body => body || (@req.file && File.read(@req.file)),
|
191
|
-
:headers => headers
|
192
|
-
)
|
193
|
-
end
|
194
|
-
|
195
|
-
def make_raw_response(response)
|
196
|
-
response.raise_error_if_any
|
197
|
-
|
198
|
-
status, headers, body = response.status, response.headers, response.body
|
199
|
-
headers ||= {}
|
200
|
-
|
201
|
-
response_string = []
|
202
|
-
response_string << "HTTP/1.1 #{status[0]} #{status[1]}"
|
203
|
-
|
204
|
-
headers["Content-Length"] = body.bytesize unless headers["Content-Length"]
|
205
|
-
headers.each do |header, value|
|
206
|
-
if header =~ /set-cookie/i
|
207
|
-
[value].flatten.each do |cookie|
|
208
|
-
response_string << "#{header}: #{cookie}"
|
209
|
-
end
|
210
|
-
else
|
211
|
-
value = value.join(", ") if value.is_a?(Array)
|
212
|
-
|
213
|
-
# WebMock's internal processing will not handle the body
|
214
|
-
# correctly if the header indicates that it is chunked, unless
|
215
|
-
# we also create all the chunks.
|
216
|
-
# It's far easier just to remove the header.
|
217
|
-
next if header =~ /transfer-encoding/i && value =~/chunked/i
|
218
|
-
|
219
|
-
response_string << "#{header}: #{value}"
|
220
|
-
end
|
221
|
-
end if headers
|
222
|
-
|
223
|
-
response_string << "" << body
|
224
|
-
response_string.join("\n")
|
225
|
-
end
|
226
|
-
end
|
227
|
-
end
|
228
|
-
end
|