Skip to content
Browse files

Add support for :allow to take in hosts with ports.

* Added WebMockServer to run for specs. It relies on stdlib accessible libraries.
* Ensure the WebMockServer has a response body otherwise em-http-request thinks its an error even if the Content-Length is 0

#90
  • Loading branch information...
1 parent a251cb3 commit 03bda1c2473d67ba80a2781ae6049e8d9338d86f @zdennis zdennis committed May 4, 2011
Showing with 98 additions and 11 deletions.
  1. +1 −1 lib/webmock/webmock.rb
  2. +9 −0 spec/spec_helper.rb
  3. +47 −0 spec/support/webmock_server.rb
  4. +41 −10 spec/webmock_shared.rb
View
2 lib/webmock/webmock.rb
@@ -43,7 +43,7 @@ def self.net_connect_allowed?(uri = nil)
end
Config.instance.allow_net_connect ||
(Config.instance.allow_localhost && WebMock::Util::URI.is_uri_localhost?(uri)) ||
- Config.instance.allow && Config.instance.allow.include?(uri.host)
+ Config.instance.allow && (Config.instance.allow.include?(uri.host) || Config.instance.allow.include?("#{uri.host}:#{uri.port}"))
end
def self.reset!
View
9 spec/spec_helper.rb
@@ -13,6 +13,7 @@
require 'webmock/rspec'
require 'network_connection'
+require "support/webmock_server"
RSpec.configure do |config|
config.include WebMock::API
@@ -24,6 +25,14 @@
config.filter_run_excluding :net_connect => true
end
+ config.before(:all) do
+ WebMockServer.instance.start
+ end
+
+ config.after(:all) do
+ WebMockServer.instance.stop
+ end
+
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
View
47 spec/support/webmock_server.rb
@@ -0,0 +1,47 @@
+require 'webrick'
+require 'logger'
+class WebMockServer
+ include Singleton
+
+ attr_reader :port
+
+ def host_with_port
+ "localhost:#{port}"
+ end
+
+ def start
+ server = WEBrick::GenericServer.new(:Port => 0, :Logger => Logger.new("/dev/null"))
+ server.logger.level = 0
+ @port = server.config[:Port]
+
+ @pid = fork do
+ server.logger.level = 0
+ trap("INT"){ server.shutdown }
+
+ server.start do |socket|
+ socket.puts <<-EOT.gsub(/^\s+\|/, '')
+ |HTTP/1.0 200 OK
+ |Date: Fri, 31 Dec 1999 23:59:59 GMT
+ |Content-Type: text/html
+ |Content-Length: 11
+ |
+ |hello world
+ EOT
+ end
+ end
+
+ loop do
+ begin
+ s = TCPSocket.new("localhost", port)
+ sleep 0.1
+ break
+ rescue Errno::ECONNREFUSED
+ sleep 0.1
+ end
+ end
+ end
+
+ def stop
+ Process.kill("INT", @pid) if @pid
+ end
+end
View
51 spec/webmock_shared.rb
@@ -110,23 +110,54 @@ class MyException < StandardError; end;
end
describe "is not allowed with exception for allowed domains" do
+ let(:host_with_port){ WebMockServer.instance.host_with_port }
+
before(:each) do
- WebMock.disable_net_connect!(:allow => ["www.example.org"])
+ WebMock.disable_net_connect!(:allow => ["www.example.org", host_with_port])
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"
+ context "when the host is not allowed" do
+ 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, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
+ end
end
- it "should raise exception if request was not stubbed" do
- lambda {
- http_request(:get, "http://www.example.com/")
- }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
+ context "when the host with port is not allowed" do
+ it "should return stubbed response if request was stubbed" do
+ stub_http_request(:get, "http://localhost:2345").to_return(:body => "abc")
+ http_request(:get, "http://localhost:2345/").body.should == "abc"
+ end
+
+ it "should raise exception if request was not stubbed" do
+ lambda {
+ http_request(:get, "http://localhost:2345/")
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://localhost:2345/))
+ end
end
- it "should allow a real request to allowed host", :net_connect => true do
- http_request(:get, "http://www.example.org/").status.should == "302"
+ context "when the host is allowed" do
+ it "should raise exception if request was not stubbed" do
+ lambda {
+ http_request(:get, "http://www.example.com/")
+ }.should raise_error(WebMock::NetConnectNotAllowedError, %r(Real HTTP connections are disabled. Unregistered request: GET http://www.example.com/))
+ end
+
+ it "should allow a real request to allowed host", :net_connect => true do
+ http_request(:get, "http://www.example.org/").status.should == "302"
+ end
+ end
+
+ context "when the host with port is allowed" do
+ it "should allow a real request to allowed host", :net_connect => true do
+ http_request(:get, "http://#{host_with_port}/").status.should == "200"
+ end
end
end
end

0 comments on commit 03bda1c

Please sign in to comment.
Something went wrong with that request. Please try again.