Skip to content

Commit

Permalink
Net::HTTP adapter is switched on/off by WebMock.enable!/disable!
Browse files Browse the repository at this point in the history
  • Loading branch information
bblimke committed Aug 13, 2011
1 parent 247a63c commit 73a2fe3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
39 changes: 35 additions & 4 deletions lib/webmock/http_lib_adapters/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@
require 'stringio'
require File.join(File.dirname(__FILE__), 'net_http_response')


module WebMock
module HttpLibAdapters
class NetHttpAdapter < HttpLibAdapter
adapter_for :net_http

OriginalNetHTTP = Net::HTTP unless const_defined?(:OriginalNetHTTP)
OriginalNetBufferedIO = Net::BufferedIO unless const_defined?(:OriginalNetBufferedIO)

def self.enable!
Net.send(:remove_const, :BufferedIO)
Net.send(:remove_const, :HTTP)
Net.send(:remove_const, :HTTPSession)
Net.send(:const_set, :HTTP, Net::WebMockNetHTTP)
Net.send(:const_set, :HTTPSession, Net::WebMockNetHTTP)
Net.send(:const_set, :BufferedIO, Net::WebMockNetBufferedIO)
end

def self.disable!
Net.send(:remove_const, :BufferedIO)
Net.send(:remove_const, :HTTP)
Net.send(:remove_const, :HTTPSession)
Net.send(:const_set, :HTTP, OriginalNetHTTP)
Net.send(:const_set, :HTTPSession, OriginalNetHTTP)
Net.send(:const_set, :BufferedIO, OriginalNetBufferedIO)
end
end
end
end

class StubSocket #:nodoc:

def initialize(*args)
Expand All @@ -19,7 +49,7 @@ def readuntil(*args)

module Net #:nodoc: all

class BufferedIO
class WebMockNetBufferedIO < BufferedIO
def initialize_with_webmock(io, debug_output = nil)
@read_timeout = 60
@rbuf = ''
Expand All @@ -37,7 +67,7 @@ def initialize_with_webmock(io, debug_output = nil)
alias_method :initialize, :initialize_with_webmock
end

class HTTP
class WebMockNetHTTP < HTTP
class << self
def socket_type_with_webmock
StubSocket
Expand Down Expand Up @@ -65,7 +95,7 @@ def request_with_webmock(request, body = nil, &block)
WebMock::CallbackRegistry.invoke_callbacks(
{:lib => :net_http, :real_request => true}, request_signature, webmock_response)
end
response.extend WebMock::Net::HTTPResponse
response.extend Net::WebMockHTTPResponse
block.call response if block
response
end
Expand Down Expand Up @@ -122,7 +152,7 @@ def build_net_http_response(webmock_response, &block)

response.instance_variable_set(:@read, true)

response.extend WebMock::Net::HTTPResponse
response.extend Net::WebMockHTTPResponse

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

Expand Down Expand Up @@ -151,6 +181,7 @@ def check_right_http_connection
end
end
end
Net::WebMockNetHTTP.version_1_2

end

Expand Down
30 changes: 15 additions & 15 deletions lib/webmock/http_lib_adapters/net_http_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
# after it has aleady been read. This attemps to preserve the behavior of
# #read_body, acting just as if it had never been read.

module WebMock
module Net
module HTTPResponse
def read_body(dest = nil, &block)
return super if @__read_body_previously_called
return @body if dest.nil? && block.nil?
raise ArgumentError.new("both arg and block given for HTTP method") if dest && block
return nil if @body.nil?

dest ||= ::Net::ReadAdapter.new(block)
dest << @body
@body = dest
ensure
# allow subsequent calls to #read_body to proceed as normal, without our hack...
@__read_body_previously_called = true
end
module Net
module WebMockHTTPResponse
def read_body(dest = nil, &block)
return super if @__read_body_previously_called
return @body if dest.nil? && block.nil?
raise ArgumentError.new("both arg and block given for HTTP method") if dest && block
return nil if @body.nil?

dest ||= ::Net::ReadAdapter.new(block)
dest << @body
@body = dest
ensure
# allow subsequent calls to #read_body to proceed as normal, without our hack...
@__read_body_previously_called = true
end
end
end

2 changes: 1 addition & 1 deletion spec/webmock_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ def call(request)
end

shared_examples_for "disabled WebMock" do
let(:url) { "http://#{WebMockServer.instance.host_with_port}"}
let(:url) { "http://#{WebMockServer.instance.host_with_port}/"}

it "should not register executed requests" do
http_request(:get, url)
Expand Down

0 comments on commit 73a2fe3

Please sign in to comment.