forked from bblimke/webmock
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into uri_parsing_caching
- Loading branch information
Showing
21 changed files
with
895 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module WebMock | ||
class CallbackRegistry | ||
@@callbacks = [] | ||
|
||
def self.add_callback(options, block) | ||
@@callbacks << {:options => options, :block => block} | ||
end | ||
|
||
def self.callbacks | ||
@@callbacks | ||
end | ||
|
||
def self.invoke_callbacks(options, request_signature, response) | ||
return if @@callbacks.empty? | ||
CallbackRegistry.callbacks.each do |callback| | ||
except = callback[:options][:except] | ||
real_only = callback[:options][:real_requests_only] | ||
unless except && except.include?(options[:lib]) | ||
if !real_only || options[:real_request] | ||
callback[:block].call(request_signature, response) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def self.reset | ||
@@callbacks = [] | ||
end | ||
|
||
def self.any_callbacks? | ||
!@@callbacks.empty? | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# This code is entierly copied from VCR (http://github.com/myronmarston/vcr) by courtesy of Myron Marston | ||
|
||
# A Net::HTTP response that has already been read raises an IOError when #read_body | ||
# is called with a destination string or block. | ||
# | ||
# This causes a problem when VCR records a response--it reads the body before yielding | ||
# the response, and if the code that is consuming the HTTP requests uses #read_body, it | ||
# can cause an error. | ||
# | ||
# This is a bit of a hack, but it allows a Net::HTTP response to be "re-read" | ||
# 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 self.extended(object) | ||
body_object = object.instance_variable_get(:@body) | ||
object.instance_variable_set(:@__orig_body__, | ||
case body_object | ||
when String then body_object | ||
else raise ArgumentError.new("Unexpected body object: #{body_object}") | ||
end | ||
) | ||
end | ||
|
||
def read_body(dest = nil, &block) | ||
if @__orig_body__ | ||
if dest && block | ||
raise ArgumentError.new("both arg and block given for HTTP method") | ||
elsif dest | ||
dest << @__orig_body__ | ||
elsif block | ||
@body = ::Net::ReadAdapter.new(block) | ||
@body << @__orig_body__ | ||
@body | ||
else | ||
@body = @__orig_body__ | ||
end | ||
else | ||
super | ||
end | ||
ensure | ||
# allow subsequent calls to #read_body to proceed as normal, without our hack... | ||
@__orig_body__ = nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.