Skip to content

Commit

Permalink
Merge branch 'master' of http://github.com/bblimke/webmock into curb_…
Browse files Browse the repository at this point in the history
…support
  • Loading branch information
phiggins committed Sep 7, 2010
2 parents e2cc327 + 8f98b75 commit f05eebe
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 13 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#Changelog

## 1.3.5

* External requests can be disabled while allowing selected hosts. Thanks to Charles Li and Ryan Bigg

This feature was available before only for localhost with `:allow_localhost => true`

WebMock.disable_net_connect!(:allow => "www.example.org")

Net::HTTP.get('www.something.com', '/') # ===> Failure

Net::HTTP.get('www.example.org', '/') # ===> Allowed.

* Fixed Net::HTTP adapter so that it preserves the original behavior of Net::HTTP.

When making a request with a block that calls #read_body on the request,
Net::HTTP causes the body to be set to a Net::ReadAdapter, but WebMock was causing the body to be set to a string.

## 1.3.4

* Fixed Net::HTTP adapter to handle cases where a block with `read_body` call is passed to `request`.
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ You can also use WebMock outside a test framework:

Net::HTTP.get('localhost:9887', '/') # ===> Allowed. Perhaps to Selenium?

### External requests can be disabled while allowing any hostname

WebMock.disable_net_connect!(:allow => "www.example.org")

Net::HTTP.get('www.something.com', '/') # ===> Failure

Net::HTTP.get('www.example.org', '/') # ===> Allowed.

## Setting Expectations

### Setting expectations in Test::Unit
Expand Down Expand Up @@ -525,6 +533,8 @@ People who submitted patches and new features or suggested improvements. Many th
* Nathaniel Bibler
* Martyn Loughran
* Muness Alrubaie
* Charles Li
* Ryan Bigg

## Background

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.4
1.3.5
1 change: 1 addition & 0 deletions lib/webmock/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class Config

attr_accessor :allow_net_connect
attr_accessor :allow_localhost
attr_accessor :allow
end
end
9 changes: 1 addition & 8 deletions lib/webmock/http_lib_adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ def readuntil(*args)

end

module StubResponse
def read_body(*args, &block)
yield @body if block_given?
@body
end
end

module Net #:nodoc: all

class BufferedIO
Expand Down Expand Up @@ -105,7 +98,7 @@ def build_net_http_response(webmock_response, &block)

response.instance_variable_set(:@read, true)

response.extend StubResponse
response.extend WebMock::Net::HTTPResponse

raise Timeout::Error, "execution expired" if webmock_response.should_timeout

Expand Down
6 changes: 4 additions & 2 deletions lib/webmock/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ def allow_net_connect!
def disable_net_connect!(options = {})
Config.instance.allow_net_connect = false
Config.instance.allow_localhost = options[:allow_localhost]
Config.instance.allow = options[:allow]
end

def net_connect_allowed?(uri = nil)
if uri.is_a?(String)
uri = WebMock::Util::URI.normalize_uri(uri)
end
Config.instance.allow_net_connect ||
(Config.instance.allow_localhost && uri.is_a?(Addressable::URI) && (uri.host == 'localhost' || uri.host == '127.0.0.1'))
Config.instance.allow_net_connect ||
(Config.instance.allow_localhost && uri.is_a?(Addressable::URI) && (uri.host == 'localhost' || uri.host == '127.0.0.1')) ||
Config.instance.allow && Config.instance.allow.include?(uri.host)
end

def registered_request?(request_signature)
Expand Down
12 changes: 12 additions & 0 deletions spec/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@
body.should =~ /Example Web Page/
end

it "should return a Net::ReadAdapter from response.body when a stubbed request is made with a block and #read_body" do
WebMock.stub_request(:get, 'http://example.com/').to_return(:body => "the body")
response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
response.body.should be_a(Net::ReadAdapter)
end

it "should return a Net::ReadAdapter from response.body when a real request is made with a block and #read_body" do
WebMock.allow_net_connect!
response = Net::HTTP.new('example.com', 80).request_get('/') { |r| r.read_body { } }
response.body.should be_a(Net::ReadAdapter)
end

describe 'after_request callback support' do
let(:expected_body_regex) { /You have reached this web page by typing.*example\.com/ }

Expand Down
21 changes: 21 additions & 0 deletions spec/webmock_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,27 @@ class MyException < StandardError; end;
}.should raise_error(connection_refused_exception_class)
end
end

describe "is not allowed with exception for allowed domains" do
before(:each) do
WebMock.disable_net_connect!(:allow => ["www.example.org"])
end

it "should return stubbed response if request was stubbed" do
stub_http_request(:get, "www.example.com").to_return(:body => "abc")
http_request(:get, "http://www.example.com/").body.should == "abc"
end

it "should raise exception if request was not stubbed" do
lambda {
http_request(:get, "http://www.example.com/")
}.should raise_error(WebMock::NetConnectNotAllowedError, client_specific_request_string("Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/"))
end

it "should allow a real request to cms.local" do
http_request(:get, "http://www.example.org/").status.should == "200"
end
end
end

describe "when matching requests" do
Expand Down
4 changes: 2 additions & 2 deletions webmock.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

Gem::Specification.new do |s|
s.name = %q{webmock}
s.version = "1.3.4"
s.version = "1.3.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Bartosz Blimke"]
s.date = %q{2010-08-10}
s.date = %q{2010-09-06}
s.description = %q{WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.}
s.email = %q{bartosz.blimke@gmail.com}
s.extra_rdoc_files = [
Expand Down

0 comments on commit f05eebe

Please sign in to comment.