webmock 1.4.0 → 1.5.0
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.
- data/CHANGELOG.md +23 -0
- data/README.md +28 -2
- data/Rakefile +5 -11
- data/VERSION +1 -1
- data/lib/webmock/adapters/rspec.rb +4 -4
- data/lib/webmock/config.rb +1 -0
- data/lib/webmock/http_lib_adapters/net_http.rb +3 -0
- data/lib/webmock/request_registry.rb +1 -2
- data/lib/webmock/response.rb +4 -8
- data/lib/webmock/webmock.rb +3 -1
- data/spec/curb_spec.rb +2 -2
- data/spec/curb_spec_helper.rb +1 -0
- data/spec/em_http_request_spec.rb +1 -1
- data/spec/em_http_request_spec_helper.rb +1 -1
- data/spec/httpclient_spec.rb +1 -1
- data/spec/net_http_spec.rb +50 -9
- data/spec/net_http_spec_helper.rb +4 -1
- data/spec/network_connection.rb +25 -0
- data/spec/patron_spec.rb +2 -3
- data/spec/patron_spec_helper.rb +1 -0
- data/spec/response_spec.rb +36 -14
- data/spec/spec_helper.rb +15 -6
- data/spec/{webmock_spec.rb → webmock_shared.rb} +25 -6
- data/webmock.gemspec +11 -10
- metadata +30 -10
- data/spec/spec.opts +0 -1
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,28 @@
|
|
1
1
|
#Changelog
|
2
2
|
|
3
|
+
## 1.5.0
|
4
|
+
|
5
|
+
* Support for dynamically evaluated raw responses recorded with `curl -is` <br/>
|
6
|
+
i.e.
|
7
|
+
|
8
|
+
`curl -is www.example.com > /tmp/www.example.com.txt`
|
9
|
+
stub_request(:get, "www.example.com").to_return(lambda { |request| File.new("/tmp/#{request.uri.host.to_s}.txt" }))
|
10
|
+
|
11
|
+
* `:net_http_connect_on_start` option can be passed to `WebMock.allow_net_connect!` and `WebMock.disable_net_connect!` methods, i.e.
|
12
|
+
|
13
|
+
WebMock.allow_net_connect!(:net_http_connect_on_start => true)
|
14
|
+
|
15
|
+
This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`. Check 'Connecting on Net::HTTP.start' in README for more information.
|
16
|
+
|
17
|
+
Thanks to Alastair Brunton for reporting the issue and for fix suggestions.
|
18
|
+
|
19
|
+
* Fixed an issue where Patron spec tried to remove system temporary directory.
|
20
|
+
Thanks to Hans de Graaff
|
21
|
+
|
22
|
+
* WebMock specs now use RSpec 2
|
23
|
+
|
24
|
+
* `rake spec NO_CONNECTION=true` can now be used to only run WebMock specs which do not make real network connections
|
25
|
+
|
3
26
|
## 1.4.0
|
4
27
|
|
5
28
|
* Curb support!!! Thanks to the awesome work of Pete Higgins!
|
data/README.md
CHANGED
@@ -207,14 +207,19 @@ You can also use WebMock outside a test framework:
|
|
207
207
|
to_return { |request| {:body => request.body} }
|
208
208
|
|
209
209
|
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
210
|
-
|
210
|
+
|
211
211
|
### Responses dynamically evaluated from lambda
|
212
|
-
|
212
|
+
|
213
213
|
stub_request(:any, 'www.example.net').
|
214
214
|
to_return(lambda { |request| {:body => request.body} })
|
215
215
|
|
216
216
|
RestClient.post('www.example.net', 'abc') # ===> "abc\n"
|
217
217
|
|
218
|
+
### Dynamically evaluated raw responses recorded with `curl -is`
|
219
|
+
|
220
|
+
`curl -is www.example.com > /tmp/www.example.com.txt`
|
221
|
+
stub_request(:get, "www.example.com").to_return(lambda { |request| File.new("/tmp/#{request.uri.host.to_s}.txt" }))
|
222
|
+
|
218
223
|
### Responses with dynamically evaluated parts
|
219
224
|
|
220
225
|
stub_request(:any, 'www.example.net').
|
@@ -305,6 +310,25 @@ You can also use WebMock outside a test framework:
|
|
305
310
|
|
306
311
|
Net::HTTP.get('www.example.org', '/') # ===> Allowed.
|
307
312
|
|
313
|
+
## Connecting on Net::HTTP.start
|
314
|
+
|
315
|
+
HTTP protocol has 3 steps: connect, request and response (or 4 with close). Most Ruby HTTP client libraries
|
316
|
+
treat connect as a part of request step, with the exception of `Net::HTTP` which
|
317
|
+
allows opening connection to the server separately to the request, by using `Net::HTTP.start`.
|
318
|
+
|
319
|
+
WebMock API was also designed with connect being part of request step, and it only allows stubbing
|
320
|
+
requests, not connections. When `Net::HTTP.start` is called, WebMock doesn't know yet whether
|
321
|
+
a request is stubbed or not. WebMock by default delays a connection until the request is invoked,
|
322
|
+
so when there is no request, `Net::HTTP.start` doesn't do anything.
|
323
|
+
**This means that WebMock breaks the Net::HTTP behaviour by default!**
|
324
|
+
|
325
|
+
To workaround this issue, WebMock offers `:net_http_connect_on_start` option,
|
326
|
+
which can be passed to `WebMock.allow_net_connect!` and `WebMock#disable_net_connect!` methods, i.e.
|
327
|
+
|
328
|
+
WebMock.allow_net_connect!(:net_http_connect_on_start => true)
|
329
|
+
|
330
|
+
This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
|
331
|
+
|
308
332
|
## Setting Expectations
|
309
333
|
|
310
334
|
### Setting expectations in Test::Unit
|
@@ -532,6 +556,8 @@ People who submitted patches and new features or suggested improvements. Many th
|
|
532
556
|
* Charles Li
|
533
557
|
* Ryan Bigg
|
534
558
|
* Pete Higgins
|
559
|
+
* Hans de Graaff
|
560
|
+
* Alastair Brunton
|
535
561
|
|
536
562
|
## Background
|
537
563
|
|
data/Rakefile
CHANGED
@@ -12,7 +12,7 @@ begin
|
|
12
12
|
gem.authors = ["Bartosz Blimke"]
|
13
13
|
gem.add_dependency "addressable", ">= 2.2.2"
|
14
14
|
gem.add_dependency "crack", ">=0.1.7"
|
15
|
-
gem.add_development_dependency "rspec", ">=
|
15
|
+
gem.add_development_dependency "rspec", ">= 2.0.0"
|
16
16
|
gem.add_development_dependency "httpclient", ">= 2.1.5.2"
|
17
17
|
gem.add_development_dependency "patron", ">= 0.4.9" unless RUBY_PLATFORM =~ /java/
|
18
18
|
gem.add_development_dependency "em-http-request", ">= 0.2.14" unless RUBY_PLATFORM =~ /java/
|
@@ -23,16 +23,10 @@ rescue LoadError
|
|
23
23
|
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
24
24
|
end
|
25
25
|
|
26
|
-
require
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
33
|
-
spec.libs << 'lib' << 'spec'
|
34
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
35
|
-
spec.rcov = true
|
26
|
+
require "rspec/core/rake_task"
|
27
|
+
RSpec::Core::RakeTask.new do |t|
|
28
|
+
t.rspec_opts = ["-c", "-f progress", "-r ./spec/spec_helper.rb"]
|
29
|
+
t.pattern = 'spec/**/*_spec.rb'
|
36
30
|
end
|
37
31
|
|
38
32
|
require 'rake/testtask'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.5.0
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'webmock'
|
2
2
|
|
3
3
|
# RSpec 1.x and 2.x compatibility
|
4
|
-
if defined?(
|
5
|
-
RSPEC_NAMESPACE = RSPEC_CONFIGURER =
|
4
|
+
if defined?(RSpec)
|
5
|
+
RSPEC_NAMESPACE = RSPEC_CONFIGURER = RSpec
|
6
6
|
elsif defined?(Spec)
|
7
7
|
RSPEC_NAMESPACE = Spec
|
8
8
|
RSPEC_CONFIGURER = Spec::Runner
|
9
9
|
else
|
10
10
|
begin
|
11
11
|
require 'rspec'
|
12
|
-
RSPEC_NAMESPACE = RSPEC_CONFIGURER =
|
12
|
+
RSPEC_NAMESPACE = RSPEC_CONFIGURER = RSpec
|
13
13
|
rescue LoadError
|
14
14
|
require 'spec'
|
15
15
|
RSPEC_NAMESPACE = Spec
|
@@ -30,4 +30,4 @@ RSPEC_CONFIGURER.configure { |config|
|
|
30
30
|
end
|
31
31
|
}
|
32
32
|
|
33
|
-
WebMock::AssertionFailure.error_class = RSPEC_NAMESPACE::Expectations::ExpectationNotMetError
|
33
|
+
WebMock::AssertionFailure.error_class = RSPEC_NAMESPACE::Expectations::ExpectationNotMetError
|
data/lib/webmock/config.rb
CHANGED
@@ -81,6 +81,9 @@ module Net #:nodoc: all
|
|
81
81
|
WebMock::NetHTTPUtility.puts_warning_for_right_http_if_needed
|
82
82
|
@@alredy_checked_for_right_http_connection = true
|
83
83
|
end
|
84
|
+
if WebMock::Config.instance.net_http_connect_on_start
|
85
|
+
return connect_without_webmock
|
86
|
+
end
|
84
87
|
nil
|
85
88
|
end
|
86
89
|
alias_method :connect_without_webmock, :connect
|
data/lib/webmock/response.rb
CHANGED
@@ -80,7 +80,7 @@ module WebMock
|
|
80
80
|
@should_timeout = options[:should_timeout]
|
81
81
|
end
|
82
82
|
|
83
|
-
def evaluate
|
83
|
+
def evaluate(request_signature)
|
84
84
|
self.body = @body.call(request_signature) if @body.is_a?(Proc)
|
85
85
|
self.headers = @headers.call(request_signature) if @headers.is_a?(Proc)
|
86
86
|
self.status = @status.call(request_signature) if @status.is_a?(Proc)
|
@@ -136,13 +136,9 @@ module WebMock
|
|
136
136
|
@responder = responder
|
137
137
|
end
|
138
138
|
|
139
|
-
def
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
def evaluate!(request_signature)
|
144
|
-
self.options = @responder.call(request_signature)
|
145
|
-
self
|
139
|
+
def evaluate(request_signature)
|
140
|
+
options = @responder.call(request_signature)
|
141
|
+
Response.new(options)
|
146
142
|
end
|
147
143
|
end
|
148
144
|
end
|
data/lib/webmock/webmock.rb
CHANGED
@@ -27,14 +27,16 @@ module WebMock
|
|
27
27
|
}
|
28
28
|
end
|
29
29
|
|
30
|
-
def self.allow_net_connect!
|
30
|
+
def self.allow_net_connect!(options = {})
|
31
31
|
Config.instance.allow_net_connect = true
|
32
|
+
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
|
32
33
|
end
|
33
34
|
|
34
35
|
def self.disable_net_connect!(options = {})
|
35
36
|
Config.instance.allow_net_connect = false
|
36
37
|
Config.instance.allow_localhost = options[:allow_localhost]
|
37
38
|
Config.instance.allow = options[:allow]
|
39
|
+
Config.instance.net_http_connect_on_start = options[:net_http_connect_on_start]
|
38
40
|
end
|
39
41
|
|
40
42
|
def self.net_connect_allowed?(uri = nil)
|
data/spec/curb_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
require '
|
2
|
+
require 'webmock_shared'
|
3
3
|
|
4
4
|
unless RUBY_PLATFORM =~ /java/
|
5
5
|
require 'curb_spec_helper'
|
6
6
|
|
7
|
-
|
7
|
+
shared_examples_for "Curb" do
|
8
8
|
include CurbSpecHelper
|
9
9
|
|
10
10
|
it_should_behave_like "WebMock"
|
data/spec/curb_spec_helper.rb
CHANGED
@@ -12,7 +12,7 @@ module EMHttpRequestSpecHelper
|
|
12
12
|
EventMachine.run {
|
13
13
|
request = EventMachine::HttpRequest.new("#{uri.omit(:userinfo).normalize.to_s}")
|
14
14
|
http = request.send(:setup_request, method, {
|
15
|
-
:timeout =>
|
15
|
+
:timeout => 10,
|
16
16
|
:body => options[:body],
|
17
17
|
:query => options[:query],
|
18
18
|
'authorization' => [uri.user, uri.password],
|
data/spec/httpclient_spec.rb
CHANGED
data/spec/net_http_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
require '
|
2
|
+
require 'webmock_shared'
|
3
3
|
require 'ostruct'
|
4
4
|
require 'net_http_spec_helper'
|
5
5
|
|
@@ -13,7 +13,7 @@ describe "Webmock with Net:HTTP" do
|
|
13
13
|
stub_http_request(:get, "www.example.com").to_return(:body => "abc"*100000)
|
14
14
|
Net::HTTP.start("www.example.com") { |query| query.get("/") }.body.should == "abc"*100000
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
it "should handle multiple values for the same response header" do
|
18
18
|
stub_http_request(:get, "www.example.com").to_return(:headers => { 'Set-Cookie' => ['foo=bar', 'bar=bazz'] })
|
19
19
|
response = Net::HTTP.get_response(URI.parse("http://www.example.com/"))
|
@@ -52,23 +52,23 @@ describe "Webmock with Net:HTTP" do
|
|
52
52
|
}.should raise_error("both of body argument and HTTPRequest#body set")
|
53
53
|
end
|
54
54
|
|
55
|
-
it "should handle real requests with readable body" do
|
55
|
+
it "should handle real requests with readable body", :net_connect => true do
|
56
56
|
WebMock.allow_net_connect!
|
57
57
|
req = Net::HTTP::Post.new("/")
|
58
58
|
Net::HTTP.start("www.example.com") {|http|
|
59
59
|
http.request(req, StringIO.new("my_params"))
|
60
60
|
}.body.should =~ /Example Web Page/
|
61
61
|
end
|
62
|
-
|
63
|
-
it "should handle requests with block passed to read_body" do
|
62
|
+
|
63
|
+
it "should handle requests with block passed to read_body", :net_connect => true do
|
64
64
|
body = ""
|
65
65
|
WebMock.allow_net_connect!
|
66
66
|
req = Net::HTTP::Get.new("/")
|
67
67
|
Net::HTTP.start("www.example.com") do |http|
|
68
|
-
http.request(req) do |res|
|
68
|
+
http.request(req) do |res|
|
69
69
|
res.read_body do |str|
|
70
70
|
body << str
|
71
|
-
end
|
71
|
+
end
|
72
72
|
end
|
73
73
|
end
|
74
74
|
body.should =~ /Example Web Page/
|
@@ -80,13 +80,54 @@ describe "Webmock with Net:HTTP" do
|
|
80
80
|
response.body.should be_a(Net::ReadAdapter)
|
81
81
|
end
|
82
82
|
|
83
|
-
it "should return a Net::ReadAdapter from response.body when a real request is made with a block and #read_body" do
|
83
|
+
it "should return a Net::ReadAdapter from response.body when a real request is made with a block and #read_body", :net_connect => true do
|
84
84
|
WebMock.allow_net_connect!
|
85
85
|
response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
|
86
86
|
response.body.should be_a(Net::ReadAdapter)
|
87
87
|
end
|
88
88
|
|
89
|
-
describe
|
89
|
+
describe "connecting on Net::HTTP.start" do
|
90
|
+
before(:each) do
|
91
|
+
@http = Net::HTTP.new('www.google.com', 443)
|
92
|
+
@http.use_ssl = true
|
93
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
94
|
+
end
|
95
|
+
describe "when net http is allowed" do
|
96
|
+
it "should not connect to the server until the request", :net_connect => true do
|
97
|
+
WebMock.allow_net_connect!
|
98
|
+
@http.start {|conn|
|
99
|
+
conn.peer_cert.should be_nil
|
100
|
+
}
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should connect to the server on start", :net_connect => true do
|
104
|
+
WebMock.allow_net_connect!(:net_http_connect_on_start => true)
|
105
|
+
@http.start {|conn|
|
106
|
+
cert = OpenSSL::X509::Certificate.new conn.peer_cert
|
107
|
+
cert.should be_a(OpenSSL::X509::Certificate)
|
108
|
+
}
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "when net http is disabled and allowed only for some hosts" do
|
113
|
+
it "should not connect to the server until the request", :net_connect => true do
|
114
|
+
WebMock.disable_net_connect!(:allow => "www.google.com")
|
115
|
+
@http.start {|conn|
|
116
|
+
conn.peer_cert.should be_nil
|
117
|
+
}
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should connect to the server on start", :net_connect => true do
|
121
|
+
WebMock.disable_net_connect!(:allow => "www.google.com", :net_http_connect_on_start => true)
|
122
|
+
@http.start {|conn|
|
123
|
+
cert = OpenSSL::X509::Certificate.new conn.peer_cert
|
124
|
+
cert.should be_a(OpenSSL::X509::Certificate)
|
125
|
+
}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe 'after_request callback support', :net_connect => true do
|
90
131
|
let(:expected_body_regex) { /You have reached this web page by typing.*example\.com/ }
|
91
132
|
|
92
133
|
before(:each) do
|
@@ -20,7 +20,10 @@ module NetHTTPSpecHelper
|
|
20
20
|
|
21
21
|
req.basic_auth uri.user, uri.password if uri.user
|
22
22
|
http = Net::HTTP.new(uri.host, uri.port)
|
23
|
-
|
23
|
+
if uri.scheme == "https"
|
24
|
+
http.use_ssl = true
|
25
|
+
http.ssl_timeout = 10
|
26
|
+
end
|
24
27
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
25
28
|
response = http.start {|http|
|
26
29
|
http.request(req, options[:body], &block)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module NetworkConnection
|
2
|
+
def self.connect_to(host, port, timeout=nil)
|
3
|
+
addr = Socket.getaddrinfo(host, nil)
|
4
|
+
sock = Socket.new(Socket.const_get(addr[0][0]), Socket::SOCK_STREAM, 0)
|
5
|
+
|
6
|
+
if timeout
|
7
|
+
secs = Integer(timeout)
|
8
|
+
usecs = Integer((timeout - secs) * 1_000_000)
|
9
|
+
optval = [secs, usecs].pack("l_2")
|
10
|
+
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, optval
|
11
|
+
sock.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDTIMEO, optval
|
12
|
+
end
|
13
|
+
sock.connect(Socket.pack_sockaddr_in(port, addr[0][3]))
|
14
|
+
sock
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.is_network_available?
|
18
|
+
begin
|
19
|
+
self.connect_to("192.0.32.10", 80, 5)
|
20
|
+
true
|
21
|
+
rescue
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/patron_spec.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
require '
|
3
|
-
|
2
|
+
require 'webmock_shared'
|
4
3
|
|
5
4
|
unless RUBY_PLATFORM =~ /java/
|
6
5
|
require 'patron_spec_helper'
|
@@ -21,7 +20,7 @@ unless RUBY_PLATFORM =~ /java/
|
|
21
20
|
describe "file requests" do
|
22
21
|
|
23
22
|
before(:each) do
|
24
|
-
@dir_path = Dir.
|
23
|
+
@dir_path = Dir.mktmpdir('webmock-')
|
25
24
|
@file_path = File.join(@dir_path, "webmock_temp_test_file")
|
26
25
|
FileUtils.rm_rf(@file_path) if File.exists?(@file_path)
|
27
26
|
end
|
data/spec/patron_spec_helper.rb
CHANGED
data/spec/response_spec.rb
CHANGED
@@ -58,14 +58,14 @@ describe WebMock::Response do
|
|
58
58
|
@response.raise_error_if_any
|
59
59
|
}.should raise_error(ArgumentError, "Exception from WebMock")
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
it "should raise error if any assigned as instance" do
|
63
63
|
@response = WebMock::Response.new(:exception => ArgumentError.new("hello world"))
|
64
64
|
lambda {
|
65
65
|
@response.raise_error_if_any
|
66
66
|
}.should raise_error(ArgumentError, "hello world")
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
it "should raise error if any assigned as string" do
|
70
70
|
@response = WebMock::Response.new(:exception => "hello world")
|
71
71
|
lambda {
|
@@ -78,7 +78,7 @@ describe WebMock::Response do
|
|
78
78
|
end
|
79
79
|
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
describe "timeout" do
|
83
83
|
|
84
84
|
it "should know if it should timeout" do
|
@@ -170,7 +170,7 @@ describe WebMock::Response do
|
|
170
170
|
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
|
171
171
|
"Content-Type"=>"text/html; charset=UTF-8",
|
172
172
|
"Content-Length"=>"438",
|
173
|
-
"Connection"=>"Keep-Alive",
|
173
|
+
"Connection"=>"Keep-Alive",
|
174
174
|
"Accept"=>"image/jpeg, image/png"
|
175
175
|
}
|
176
176
|
end
|
@@ -195,17 +195,17 @@ describe WebMock::Response do
|
|
195
195
|
|
196
196
|
it "should have evaluated body" do
|
197
197
|
@response = WebMock::Response.new(:body => lambda {|request| request.body})
|
198
|
-
@response.evaluate
|
198
|
+
@response.evaluate(@request_signature).body.should == "abc"
|
199
199
|
end
|
200
200
|
|
201
201
|
it "should have evaluated headers" do
|
202
202
|
@response = WebMock::Response.new(:headers => lambda {|request| request.headers})
|
203
|
-
@response.evaluate
|
203
|
+
@response.evaluate(@request_signature).headers.should == {'A' => 'a'}
|
204
204
|
end
|
205
205
|
|
206
206
|
it "should have evaluated status" do
|
207
207
|
@response = WebMock::Response.new(:status => lambda {|request| 302})
|
208
|
-
@response.evaluate
|
208
|
+
@response.evaluate(@request_signature).status.should == [302, ""]
|
209
209
|
end
|
210
210
|
|
211
211
|
end
|
@@ -216,7 +216,7 @@ describe WebMock::Response do
|
|
216
216
|
|
217
217
|
describe "evaluating response options" do
|
218
218
|
|
219
|
-
it "should
|
219
|
+
it "should evaluate new response with evaluated options" do
|
220
220
|
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc", :headers => {'A' => 'a'})
|
221
221
|
response = WebMock::DynamicResponse.new(lambda {|request|
|
222
222
|
{
|
@@ -225,19 +225,41 @@ describe WebMock::Response do
|
|
225
225
|
:status => 302
|
226
226
|
}
|
227
227
|
})
|
228
|
-
response.evaluate
|
229
|
-
|
230
|
-
|
231
|
-
|
228
|
+
evaluated_response = response.evaluate(request_signature)
|
229
|
+
evaluated_response.body.should == "abc"
|
230
|
+
evaluated_response.headers.should == {'A' => 'a'}
|
231
|
+
evaluated_response.status.should == [302, ""]
|
232
232
|
end
|
233
233
|
|
234
234
|
it "should be equal to static response after evaluation" do
|
235
235
|
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", :body => "abc")
|
236
236
|
response = WebMock::DynamicResponse.new(lambda {|request| {:body => request.body}})
|
237
|
-
response.evaluate
|
238
|
-
|
237
|
+
evaluated_response = response.evaluate(request_signature)
|
238
|
+
evaluated_response.should == WebMock::Response.new(:body => "abc")
|
239
239
|
end
|
240
240
|
|
241
|
+
describe "when raw response is evaluated" do
|
242
|
+
before(:each) do
|
243
|
+
@files = {
|
244
|
+
"www.example.com" => File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
|
245
|
+
}
|
246
|
+
@request_signature = WebMock::RequestSignature.new(:get, "www.example.com")
|
247
|
+
end
|
248
|
+
|
249
|
+
describe "as a file" do
|
250
|
+
it "should return response" do
|
251
|
+
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s] })
|
252
|
+
response.evaluate(@request_signature).body.size.should == 438
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
describe "as a string" do
|
257
|
+
it "should return response" do
|
258
|
+
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s].read })
|
259
|
+
response.evaluate(@request_signature).body.size.should == 438
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
241
263
|
end
|
242
264
|
|
243
265
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,23 +8,31 @@ end
|
|
8
8
|
|
9
9
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
10
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
11
|
-
require '
|
12
|
-
require 'spec/autorun'
|
11
|
+
require 'rspec'
|
13
12
|
|
14
13
|
require 'webmock/rspec'
|
15
14
|
|
15
|
+
require 'network_connection'
|
16
|
+
|
16
17
|
require 'json'
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.include WebMock::API
|
21
|
+
unless NetworkConnection.is_network_available?
|
22
|
+
warn("No network connectivity. Only examples which do not make real network connections will run.")
|
23
|
+
no_network_connection = true
|
24
|
+
end
|
25
|
+
if ENV["NO_CONNECTION"] || no_network_connection
|
26
|
+
config.filter_run_excluding :net_connect => true
|
27
|
+
end
|
20
28
|
end
|
21
29
|
|
22
30
|
def fail()
|
23
|
-
raise_error(
|
31
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError)
|
24
32
|
end
|
25
33
|
|
26
34
|
def fail_with(message)
|
27
|
-
raise_error(
|
35
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError, message)
|
28
36
|
end
|
29
37
|
|
30
38
|
class Proc
|
@@ -56,3 +64,4 @@ def client_specific_request_string(string)
|
|
56
64
|
end
|
57
65
|
string
|
58
66
|
end
|
67
|
+
|
@@ -18,7 +18,7 @@ describe "WebMock version" do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
|
21
|
-
|
21
|
+
shared_examples_for "WebMock" do
|
22
22
|
before(:each) do
|
23
23
|
WebMock.disable_net_connect!
|
24
24
|
WebMock::RequestRegistry.instance.reset_webmock
|
@@ -26,7 +26,7 @@ describe "WebMock", :shared => true do
|
|
26
26
|
|
27
27
|
describe "when web connect" do
|
28
28
|
|
29
|
-
describe "is allowed" do
|
29
|
+
describe "is allowed", :net_connect => true do
|
30
30
|
before(:each) do
|
31
31
|
WebMock.allow_net_connect!
|
32
32
|
end
|
@@ -122,7 +122,7 @@ describe "WebMock", :shared => true do
|
|
122
122
|
}.should raise_error(WebMock::NetConnectNotAllowedError, client_specific_request_string("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/"))
|
123
123
|
end
|
124
124
|
|
125
|
-
it "should allow a real request to allowed host" do
|
125
|
+
it "should allow a real request to allowed host", :net_connect => true do
|
126
126
|
http_request(:get, "http://www.example.org/").status.should == "200"
|
127
127
|
end
|
128
128
|
end
|
@@ -691,6 +691,25 @@ describe "WebMock", :shared => true do
|
|
691
691
|
end
|
692
692
|
end
|
693
693
|
|
694
|
+
describe "replying raw responses evaluated dynamically" do
|
695
|
+
before(:each) do
|
696
|
+
@files = {
|
697
|
+
"www.example.com" => File.new(File.expand_path(File.dirname(__FILE__)) + "/example_curl_output.txt")
|
698
|
+
}
|
699
|
+
end
|
700
|
+
|
701
|
+
it "should return response from evaluated file" do
|
702
|
+
stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s] })
|
703
|
+
http_request(:get, "http://www.example.com/").body.size.should == 438
|
704
|
+
end
|
705
|
+
|
706
|
+
it "should return response from evaluated string" do
|
707
|
+
stub_http_request(:get, "www.example.com").to_return(lambda {|request| @files[request.uri.host.to_s].read })
|
708
|
+
http_request(:get, "http://www.example.com/").body.size.should == 438
|
709
|
+
end
|
710
|
+
|
711
|
+
end
|
712
|
+
|
694
713
|
describe "sequences of responses" do
|
695
714
|
|
696
715
|
it "should return responses one by one if declared in array" do
|
@@ -1362,7 +1381,7 @@ describe "WebMock", :shared => true do
|
|
1362
1381
|
end
|
1363
1382
|
|
1364
1383
|
|
1365
|
-
describe "when net connect allowed" do
|
1384
|
+
describe "when net connect allowed", :net_connect => true do
|
1366
1385
|
before(:each) do
|
1367
1386
|
WebMock.allow_net_connect!
|
1368
1387
|
end
|
@@ -1466,7 +1485,7 @@ describe "WebMock", :shared => true do
|
|
1466
1485
|
|
1467
1486
|
end
|
1468
1487
|
|
1469
|
-
describe "for real requests" do
|
1488
|
+
describe "for real requests", :net_connect => true do
|
1470
1489
|
before(:each) do
|
1471
1490
|
WebMock.reset_webmock
|
1472
1491
|
WebMock.allow_net_connect!
|
@@ -1500,7 +1519,7 @@ describe "WebMock", :shared => true do
|
|
1500
1519
|
@called.should == 2
|
1501
1520
|
end
|
1502
1521
|
|
1503
|
-
it "should invoke callbacks only for real requests if requested" do
|
1522
|
+
it "should invoke callbacks only for real requests if requested", :net_connect => true do
|
1504
1523
|
WebMock.after_request(:real_requests_only => true) { @called = true }
|
1505
1524
|
http_request(:get, "http://www.example.com/")
|
1506
1525
|
@called.should == nil
|
data/webmock.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{webmock}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Bartosz Blimke"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-11-02}
|
13
13
|
s.description = %q{WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.}
|
14
14
|
s.email = %q{bartosz.blimke@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -63,6 +63,7 @@ Gem::Specification.new do |s|
|
|
63
63
|
"spec/httpclient_spec_helper.rb",
|
64
64
|
"spec/net_http_spec.rb",
|
65
65
|
"spec/net_http_spec_helper.rb",
|
66
|
+
"spec/network_connection.rb",
|
66
67
|
"spec/other_net_http_libs_spec.rb",
|
67
68
|
"spec/patron_spec.rb",
|
68
69
|
"spec/patron_spec_helper.rb",
|
@@ -72,7 +73,6 @@ Gem::Specification.new do |s|
|
|
72
73
|
"spec/request_signature_spec.rb",
|
73
74
|
"spec/request_stub_spec.rb",
|
74
75
|
"spec/response_spec.rb",
|
75
|
-
"spec/spec.opts",
|
76
76
|
"spec/spec_helper.rb",
|
77
77
|
"spec/util/hash_counter_spec.rb",
|
78
78
|
"spec/util/headers_spec.rb",
|
@@ -87,7 +87,7 @@ Gem::Specification.new do |s|
|
|
87
87
|
"spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb",
|
88
88
|
"spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb",
|
89
89
|
"spec/vendor/right_http_connection-1.2.4/setup.rb",
|
90
|
-
"spec/
|
90
|
+
"spec/webmock_shared.rb",
|
91
91
|
"test/test_helper.rb",
|
92
92
|
"test/test_webmock.rb",
|
93
93
|
"webmock.gemspec"
|
@@ -95,7 +95,7 @@ Gem::Specification.new do |s|
|
|
95
95
|
s.homepage = %q{http://github.com/bblimke/webmock}
|
96
96
|
s.rdoc_options = ["--charset=UTF-8"]
|
97
97
|
s.require_paths = ["lib"]
|
98
|
-
s.rubygems_version = %q{1.3.
|
98
|
+
s.rubygems_version = %q{1.3.7}
|
99
99
|
s.summary = %q{Library for stubbing HTTP requests in Ruby.}
|
100
100
|
s.test_files = [
|
101
101
|
"spec/curb_spec.rb",
|
@@ -106,6 +106,7 @@ Gem::Specification.new do |s|
|
|
106
106
|
"spec/httpclient_spec_helper.rb",
|
107
107
|
"spec/net_http_spec.rb",
|
108
108
|
"spec/net_http_spec_helper.rb",
|
109
|
+
"spec/network_connection.rb",
|
109
110
|
"spec/other_net_http_libs_spec.rb",
|
110
111
|
"spec/patron_spec.rb",
|
111
112
|
"spec/patron_spec_helper.rb",
|
@@ -125,7 +126,7 @@ Gem::Specification.new do |s|
|
|
125
126
|
"spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb",
|
126
127
|
"spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb",
|
127
128
|
"spec/vendor/right_http_connection-1.2.4/setup.rb",
|
128
|
-
"spec/
|
129
|
+
"spec/webmock_shared.rb",
|
129
130
|
"test/test_helper.rb",
|
130
131
|
"test/test_webmock.rb"
|
131
132
|
]
|
@@ -134,10 +135,10 @@ Gem::Specification.new do |s|
|
|
134
135
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
135
136
|
s.specification_version = 3
|
136
137
|
|
137
|
-
if Gem::Version.new(Gem::
|
138
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
138
139
|
s.add_runtime_dependency(%q<addressable>, [">= 2.2.2"])
|
139
140
|
s.add_runtime_dependency(%q<crack>, [">= 0.1.7"])
|
140
|
-
s.add_development_dependency(%q<rspec>, [">=
|
141
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
141
142
|
s.add_development_dependency(%q<httpclient>, [">= 2.1.5.2"])
|
142
143
|
s.add_development_dependency(%q<patron>, [">= 0.4.9"])
|
143
144
|
s.add_development_dependency(%q<em-http-request>, [">= 0.2.14"])
|
@@ -145,7 +146,7 @@ Gem::Specification.new do |s|
|
|
145
146
|
else
|
146
147
|
s.add_dependency(%q<addressable>, [">= 2.2.2"])
|
147
148
|
s.add_dependency(%q<crack>, [">= 0.1.7"])
|
148
|
-
s.add_dependency(%q<rspec>, [">=
|
149
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
149
150
|
s.add_dependency(%q<httpclient>, [">= 2.1.5.2"])
|
150
151
|
s.add_dependency(%q<patron>, [">= 0.4.9"])
|
151
152
|
s.add_dependency(%q<em-http-request>, [">= 0.2.14"])
|
@@ -154,7 +155,7 @@ Gem::Specification.new do |s|
|
|
154
155
|
else
|
155
156
|
s.add_dependency(%q<addressable>, [">= 2.2.2"])
|
156
157
|
s.add_dependency(%q<crack>, [">= 0.1.7"])
|
157
|
-
s.add_dependency(%q<rspec>, [">=
|
158
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
158
159
|
s.add_dependency(%q<httpclient>, [">= 2.1.5.2"])
|
159
160
|
s.add_dependency(%q<patron>, [">= 0.4.9"])
|
160
161
|
s.add_dependency(%q<em-http-request>, [">= 0.2.14"])
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webmock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 3
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 1
|
7
|
-
-
|
8
|
+
- 5
|
8
9
|
- 0
|
9
|
-
version: 1.
|
10
|
+
version: 1.5.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Bartosz Blimke
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-11-02 00:00:00 +00:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: addressable
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
27
30
|
segments:
|
28
31
|
- 2
|
29
32
|
- 2
|
@@ -35,9 +38,11 @@ dependencies:
|
|
35
38
|
name: crack
|
36
39
|
prerelease: false
|
37
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
38
42
|
requirements:
|
39
43
|
- - ">="
|
40
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 21
|
41
46
|
segments:
|
42
47
|
- 0
|
43
48
|
- 1
|
@@ -49,23 +54,27 @@ dependencies:
|
|
49
54
|
name: rspec
|
50
55
|
prerelease: false
|
51
56
|
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
52
58
|
requirements:
|
53
59
|
- - ">="
|
54
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 15
|
55
62
|
segments:
|
56
|
-
- 1
|
57
63
|
- 2
|
58
|
-
-
|
59
|
-
|
64
|
+
- 0
|
65
|
+
- 0
|
66
|
+
version: 2.0.0
|
60
67
|
type: :development
|
61
68
|
version_requirements: *id003
|
62
69
|
- !ruby/object:Gem::Dependency
|
63
70
|
name: httpclient
|
64
71
|
prerelease: false
|
65
72
|
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
66
74
|
requirements:
|
67
75
|
- - ">="
|
68
76
|
- !ruby/object:Gem::Version
|
77
|
+
hash: 119
|
69
78
|
segments:
|
70
79
|
- 2
|
71
80
|
- 1
|
@@ -78,9 +87,11 @@ dependencies:
|
|
78
87
|
name: patron
|
79
88
|
prerelease: false
|
80
89
|
requirement: &id005 !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
81
91
|
requirements:
|
82
92
|
- - ">="
|
83
93
|
- !ruby/object:Gem::Version
|
94
|
+
hash: 29
|
84
95
|
segments:
|
85
96
|
- 0
|
86
97
|
- 4
|
@@ -92,9 +103,11 @@ dependencies:
|
|
92
103
|
name: em-http-request
|
93
104
|
prerelease: false
|
94
105
|
requirement: &id006 !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
95
107
|
requirements:
|
96
108
|
- - ">="
|
97
109
|
- !ruby/object:Gem::Version
|
110
|
+
hash: 11
|
98
111
|
segments:
|
99
112
|
- 0
|
100
113
|
- 2
|
@@ -106,9 +119,11 @@ dependencies:
|
|
106
119
|
name: curb
|
107
120
|
prerelease: false
|
108
121
|
requirement: &id007 !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
109
123
|
requirements:
|
110
124
|
- - ">="
|
111
125
|
- !ruby/object:Gem::Version
|
126
|
+
hash: 19
|
112
127
|
segments:
|
113
128
|
- 0
|
114
129
|
- 7
|
@@ -172,6 +187,7 @@ files:
|
|
172
187
|
- spec/httpclient_spec_helper.rb
|
173
188
|
- spec/net_http_spec.rb
|
174
189
|
- spec/net_http_spec_helper.rb
|
190
|
+
- spec/network_connection.rb
|
175
191
|
- spec/other_net_http_libs_spec.rb
|
176
192
|
- spec/patron_spec.rb
|
177
193
|
- spec/patron_spec_helper.rb
|
@@ -181,7 +197,6 @@ files:
|
|
181
197
|
- spec/request_signature_spec.rb
|
182
198
|
- spec/request_stub_spec.rb
|
183
199
|
- spec/response_spec.rb
|
184
|
-
- spec/spec.opts
|
185
200
|
- spec/spec_helper.rb
|
186
201
|
- spec/util/hash_counter_spec.rb
|
187
202
|
- spec/util/headers_spec.rb
|
@@ -196,7 +211,7 @@ files:
|
|
196
211
|
- spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb
|
197
212
|
- spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb
|
198
213
|
- spec/vendor/right_http_connection-1.2.4/setup.rb
|
199
|
-
- spec/
|
214
|
+
- spec/webmock_shared.rb
|
200
215
|
- test/test_helper.rb
|
201
216
|
- test/test_webmock.rb
|
202
217
|
- webmock.gemspec
|
@@ -210,23 +225,27 @@ rdoc_options:
|
|
210
225
|
require_paths:
|
211
226
|
- lib
|
212
227
|
required_ruby_version: !ruby/object:Gem::Requirement
|
228
|
+
none: false
|
213
229
|
requirements:
|
214
230
|
- - ">="
|
215
231
|
- !ruby/object:Gem::Version
|
232
|
+
hash: 3
|
216
233
|
segments:
|
217
234
|
- 0
|
218
235
|
version: "0"
|
219
236
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
237
|
+
none: false
|
220
238
|
requirements:
|
221
239
|
- - ">="
|
222
240
|
- !ruby/object:Gem::Version
|
241
|
+
hash: 3
|
223
242
|
segments:
|
224
243
|
- 0
|
225
244
|
version: "0"
|
226
245
|
requirements: []
|
227
246
|
|
228
247
|
rubyforge_project:
|
229
|
-
rubygems_version: 1.3.
|
248
|
+
rubygems_version: 1.3.7
|
230
249
|
signing_key:
|
231
250
|
specification_version: 3
|
232
251
|
summary: Library for stubbing HTTP requests in Ruby.
|
@@ -239,6 +258,7 @@ test_files:
|
|
239
258
|
- spec/httpclient_spec_helper.rb
|
240
259
|
- spec/net_http_spec.rb
|
241
260
|
- spec/net_http_spec_helper.rb
|
261
|
+
- spec/network_connection.rb
|
242
262
|
- spec/other_net_http_libs_spec.rb
|
243
263
|
- spec/patron_spec.rb
|
244
264
|
- spec/patron_spec_helper.rb
|
@@ -258,6 +278,6 @@ test_files:
|
|
258
278
|
- spec/vendor/right_http_connection-1.2.4/lib/net_fix.rb
|
259
279
|
- spec/vendor/right_http_connection-1.2.4/lib/right_http_connection.rb
|
260
280
|
- spec/vendor/right_http_connection-1.2.4/setup.rb
|
261
|
-
- spec/
|
281
|
+
- spec/webmock_shared.rb
|
262
282
|
- test/test_helper.rb
|
263
283
|
- test/test_webmock.rb
|
data/spec/spec.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|