Commit 087b7df3 authored by Cédric Boutillier's avatar Cédric Boutillier

New upstream version 1.3.0+git20170806+dfsg1

parent 1c27e795
fakeweb (master branch pending release)
* fix a variety of NoMethodErrors when Net::HTTP#read_timeout= or
Net::HTTP#continue_timeout= is called after a Net::HTTP object has handled a
first fake request under newer versions of Ruby and/or some gems (Mechanize,
aws-sdk, etc.) [Marcel Massana, Trevor Rowe, Chris Kampmeier]
* add ability to accept a StringIO as a fake response body
[Larry Diehl, Andy Delcambre, Chris Kampmeier]
* fix an exception when an IO is passed in as a fake response body
[Chris Kampmeier]
* stub responses with no bodies (e.g. to a HEAD request) now return nil for
#body, not an empty string, to match Net::HTTP's behavior [Myron Marston]
* fix warnings on post-1.8 versions of Ruby [Peter Hellberg, Kenichi Kamiya]
* fix that the options hash passed to FakeWeb.register_uri would sometimes be
modified [Chris Kampmeier]
* raise an ArgumentError when FakeWeb.register_uri's third argument can't be
handled as an options hash [David Cornu, Josef Strzibny, Chris Kampmeier]
* raise the more-specific ArgumentError instead of StandardError when the object
passed to FakeWeb.register_uri's :response option can't be handled; this
should be relatively backwards-compatible because ArgumentError descends from
StandardError [Chris Kampmeier]
* deprecation warnings now detect the caller more robustly to work with JRuby's
backtraces [Chris Kampmeier]
fakeweb (1.3.0)
* improve response header registration so you can pass an array to set a header
......
......@@ -9,8 +9,7 @@ platform :rbx do
# rubysl-test-unit 2.0.2's gemspec relaxed its dependency on minitest to allow
# any version (previously, it specified "~> 4.7"). Minitest 5 doesn't have a
# Test::Unit compatibility layer like 4.x, so it doesn't work with Test::Unit
# at all (see e.g. https://github.com/seattlerb/minitest/issues/358). So, I'm
# holding this one back until we find out what's going on.
gem "rubysl-test-unit", "2.0.1"
# at all. rubysl-test-unit 2.0.3 fixed this.
gem "rubysl-test-unit", ["~> 2.0", "!= 2.0.2"]
gem "psych", "~> 2.0"
end
......@@ -2,7 +2,7 @@ require 'rubygems'
require 'rake'
task :print_header do
version_string = `rvm current`.strip
version_string = `command -v rvm >/dev/null && rvm current`.strip
version_string = RUBY_DESCRIPTION if !$?.success?
puts "\n# Starting tests using \e[1m#{version_string}\e[0m\n\n"
end
......@@ -28,21 +28,15 @@ Rake::TestTask.new(:test) do |test|
test.verbose = false
test.warning = true
# This quells a JRuby warning from SimpleCov: "tracing (e.g.
# set_trace_func) will not capture all events without --debug flag".
# This flag changes two internal options: it enables the runtime's
# "full-trace" mode, then sets its "compile mode" to OFF so that
# all code runs through the interpreter. (See test_helper.rb for
# the Java and Ruby code exposing these.)
#
# Note that --debug is unrelated to lots of similar-sounding flags
# common to many implementations (-d, -D, --debugger, etc.) and so we
# don't need to set any of those. For details, see JRuby's
# To measure code coverage under JRuby, we need to pass --debug (enabling the
# runtime's "full-trace" mode) and --dev (setting its "compile mode" to OFF so
# all code runs through the interpreter). For details, see JRuby's
# util/cli/ArgumentProcessor.java.
test.ruby_opts << "--debug" if RUBY_PLATFORM == "java"
test.ruby_opts << "--debug" << "--dev" if RUBY_PLATFORM == "java"
end
Rake::Task["test"].enhance ["test:preflight"]
Rake::Task["test"].clear_comments.add_description <<-DESC.gsub(/^ /, "")
Rake::Task["test"].clear_comments if Rake::Task["test"].respond_to?(:clear_comments)
Rake::Task["test"].add_description <<-DESC.gsub(/^ /, "")
Run preflight checks, then all tests (default task).
Set COVERAGE_REPORT=1 to produce an HTML-formatted code-coverage
......@@ -66,7 +60,7 @@ task :clean do
end
if RUBY_VERSION >= "1.8.7"
rdoc_options = %w(--show-hash --charset utf-8 --github)
rdoc_options = %w(--show-hash --charset=UTF-8)
begin
require 'sdoc'
rdoc_options += %w(--format sdoc)
......
......@@ -19,41 +19,65 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = root_docs
s.files = Dir["lib/**/*.rb"] + root_docs
s.require_paths = ["lib"]
s.rdoc_options = ["--charset=UTF-8"]
s.rdoc_options = ["--show-hash --charset=UTF-8"]
# Mocha's README says "versions 0.10.2, 0.10.3 & 0.11.0 of the
# Mocha gem were broken. Please do not use these versions."
broken_mocha_spec = ["!= 0.11.0", "!= 0.10.3", "!= 0.10.2"]
# Mocha's README says "versions 0.10.2, 0.10.3 & 0.11.0 of the Mocha gem were
# broken. Please do not use these versions."
exclude_broken_mocha_spec = ["!= 0.11.0", "!= 0.10.3", "!= 0.10.2"]
if RUBY_VERSION <= "1.8.6"
# Mocha 0.11.1 introduced a call to #define_method with a block
# parameter (like this: define_method { |*args, &blk| ... }),
# causing a syntax error in 1.8.6. It's still there as of the
# latest release, 0.13.3. Older versions of Mocha work great,
# though; 0.9.5 is the oldest I've tested so far.
s.add_development_dependency "mocha", [">= 0.9.5", "< 0.11.1"] + broken_mocha_spec
# Rake 0.9.1 had the same issue with 1.8.6, but it was fixed for
# the next release. Later on, Rake 0.9.6 and 10.0.3 were both
# released with code using String#end_with?, which only works in
# 1.8.7+; both times, 1.8.6-compatibility was restored for the
# next release.
s.add_development_dependency "rake", [">= 0.8.7", "!= 0.9.1", "!= 0.9.6", "!= 10.0.3"]
# Mocha 0.11.1 introduced a call to #define_method with a block parameter
# (like this: define_method { |*args, &blk| ... }), causing a syntax error
# in 1.8.6. It's still there as of the latest release, 0.13.3. Older
# versions of Mocha work great, though; 0.9.5 is the oldest I've tested so
# far.
mocha_spec = [">= 0.9.5", "< 0.11.1"]
else
# Otherwise, prefer up-to-date Mocha
mocha_spec = ["~> 1.0"]
end
s.add_development_dependency "mocha", mocha_spec + exclude_broken_mocha_spec
# * Rake 0.9.1 had the same syntax error on 1.8.6 as Mocha, but it was fixed
# for the next release.
# * Rake 0.9.6 and 10.0.3 were both released with code using String#end_with?,
# which only works in 1.8.7+; both times, 1.8.6-compatibility was restored
# for the next release.
# * Rake 10.2 and 10.2.1 removed Ruby 1.8 compatibility; 10.2.2 restored it.
# Then Rake 11.0 removed it again for good.
if RUBY_VERSION <= "1.8.6"
rake_spec = [">= 0.8.7", "!= 0.9.1", "!= 0.9.6", "!= 10.0.3",
"!= 10.2", "!= 10.2.1", "< 11.0"]
elsif RUBY_VERSION == "1.8.7"
rake_spec = [">= 0.8.7", "!= 10.2", "!= 10.2.1", "< 11.0"]
elsif RUBY_VERSION < "1.9.3"
# Rake's gemspec started requiring Ruby 1.9.3+ as of 11.0
rake_spec = ["~> 10.0"]
else
# Otherwise, prefer up-to-date dev tools
s.add_development_dependency "mocha", ["~> 0.14"] + broken_mocha_spec
s.add_development_dependency "rake", ["~> 10.0"]
# Otherwise, prefer up-to-date Rake
rake_spec = ["~> 12.0"]
end
s.add_development_dependency "rake", rake_spec
if RUBY_VERSION >= "1.8.7"
# ZenTest (autotest) wants at least RubyGems 1.8, which is 1.8.7+
# only, as is RDoc, the main dependency of sdoc.
s.add_development_dependency "ZenTest", ["~> 4.9"]
s.add_development_dependency "sdoc"
# RDoc 4.3.0 only works on Ruby 1.9.3+
if RUBY_VERSION < "1.9.3"
s.add_development_dependency "rdoc", ["< 4.3.0"]
end
end
if RUBY_VERSION >= "1.9.0"
# To monitor our tests' code coverage, the SimpleCov gem uses Ruby's built-in
# Coverage module, which first shipped with Ruby 1.9.0. SimpleCov doesn't work
# very well on pre-1.9.3, though.
if RUBY_VERSION >= "1.9.3"
s.add_development_dependency "simplecov", ["~> 0.7"]
s.add_development_dependency "simplecov-console", ["~> 0.1"]
......@@ -64,7 +88,8 @@ Gem::Specification.new do |s|
end
if RUBY_PLATFORM == "java"
s.add_development_dependency "jruby-openssl", ["~> 0.8"]
if RUBY_VERSION >= "2.2.0"
# Test::Unit is no longer distributed with Ruby as of 2.2.0
s.add_development_dependency "test-unit", ["~> 3.2"]
end
end
require 'singleton'
require 'fake_web/ext/net_http'
require 'fake_web/registry'
require 'fake_web/response'
......
......@@ -5,22 +5,12 @@ require 'stringio'
module Net #:nodoc: all
class BufferedIO
def initialize_with_fakeweb(*args)
initialize_without_fakeweb(*args)
case @io
when Socket, OpenSSL::SSL::SSLSocket, StringIO, IO
# usable as-is
when String
if !@io.include?("\0") && File.exist?(@io) && !File.directory?(@io)
@io = File.open(@io, "r")
else
@io = StringIO.new(@io)
end
else
raise ArgumentError, "Unable to create fake socket from #{io}"
eval <<-RUBY
def initialize_with_fakeweb(*args#{", **opts" if RUBY_VERSION >= "2.4.0" })
initialize_without_fakeweb(*args#{", **opts" if RUBY_VERSION >= "2.4.0" })
@io = FakeWeb::Utility.io_from_fake_response_object(@io)
end
end
RUBY
alias_method :initialize_without_fakeweb, :initialize
alias_method :initialize, :initialize_with_fakeweb
end
......
require 'singleton'
module FakeWeb
class Registry #:nodoc:
include Singleton
......@@ -14,6 +16,9 @@ module FakeWeb
def register_uri(method, uri, options)
uri_map[normalize_uri(uri)][method] = [*[options]].flatten.collect do |option|
if !option.respond_to?(:keys)
raise ArgumentError.new("Expected options hash: #{option.inspect}")
end
FakeWeb::Responder.new(method, uri, option, option[:times])
end
end
......
......@@ -9,7 +9,12 @@ module FakeWeb
end
def closed?
@closed ||= true
@closed ||= false
@closed
end
def close
@closed = true
end
def readuntil(*args)
......
......@@ -46,6 +46,21 @@ module FakeWeb
request.content_length = request.body.length unless request.body.nil?
end
def self.io_from_fake_response_object(obj)
case obj
when Socket, OpenSSL::SSL::SSLSocket, StringIO, IO
obj # usable as-is
when String
if !obj.include?("\0") && File.exist?(obj) && !File.directory?(obj)
File.open(obj, "r")
else
StringIO.new(obj)
end
else
raise ArgumentError, "Unable to create fake socket from #{obj}"
end
end
def self.puts_warning_for_net_http_around_advice_libs_if_needed
libs = {"Samuel" => defined?(Samuel)}
warnings = libs.select { |_, loaded| loaded }.map do |name, _|
......
......@@ -24,7 +24,7 @@ module FakeWebTestHelper
formatters << Console
formatters << HTMLFormatter if html_report_requested?
end
MultiFormatter[*formatters]
MultiFormatter.new(formatters)
end
def this_process_responsible_for_coverage_reporting?
......@@ -44,7 +44,10 @@ module FakeWebTestHelper
end
def coverage_supported_by_this_ruby?
RUBY_VERSION >= "1.9.0" && RUBY_ENGINE != "rbx"
return false if defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
# Ruby's built-in Coverage first shipped with Ruby 1.9.0, but SimpleCov
# doesn't work very well on pre-1.9.3.
RUBY_VERSION >= "1.9.3"
end
extend self
......
......@@ -16,6 +16,20 @@ class TestFakeWeb < Test::Unit::TestCase
end
end
def test_register_uri_with_string_for_third_argument
exception = assert_raises ArgumentError do
FakeWeb.register_uri(:get, "http://example.com", "response")
end
assert_match 'Expected options hash: "response"', exception.message
end
def test_register_uri_with_array_of_strings_for_third_argument
exception = assert_raises ArgumentError do
FakeWeb.register_uri(:get, "http://example.com", ["first response"])
end
assert_match 'Expected options hash: "first response"', exception.message
end
def test_registered_uri_with_wrong_number_of_arguments
assert_raises ArgumentError do
FakeWeb.registered_uri?
......@@ -34,9 +48,9 @@ class TestFakeWeb < Test::Unit::TestCase
end
end
def test_register_uri_without_domain_name
def test_register_uri_with_invalid_uri
assert_raises URI::InvalidURIError do
FakeWeb.register_uri(:get, 'test_example2.txt', fixture_path("test_example.txt"))
FakeWeb.register_uri(:get, '#~invalid~#', :body => "OK")
end
end
......
......@@ -78,21 +78,12 @@ module FakeWebTestHelper
ruby_opts = []
ruby_opts << "-w" if defined?($-w) && $-w
# When you start JRuby with --debug, it does this:
#
# # src/org/jruby/util/cli/ArgumentProcessor.java:371
# RubyInstanceConfig.FULL_TRACE_ENABLED = true;
# config.setCompileMode(RubyInstanceConfig.CompileMode.OFF);
#
# This checks the same settings from Rubyland. See our Rakefile for
# some background on --debug.
# TODO: is there a good way to retrieve the command-line options
# used to start JRuby? --debug doesn't seem to have an analogue of
# $-w, $-d, etc.
# Propagate JRuby options needed to measure code coverage. See JRuby's
# util/cli/ArgumentProcessor.java for details.
if RUBY_PLATFORM == "java" &&
JRuby.runtime.instance_config.class.FULL_TRACE_ENABLED &&
JRuby.runtime.instance_config.compile_mode.to_s == "OFF"
ruby_opts << "--debug"
ruby_opts << "--debug" << "--dev"
end
ruby_opts
......@@ -101,6 +92,7 @@ module FakeWebTestHelper
def remove_warnings_from_gems_and_stdlib(string)
code_paths = [RbConfig::CONFIG["libdir"],
File.expand_path(File.join(File.dirname(__FILE__), "vendor")),
"./test/vendor",
Gem.path].flatten
splitter = string.respond_to?(:lines) ? :lines : :to_a
string.send(splitter).reject { |line|
......@@ -119,7 +111,19 @@ module FakeWebTestHelper
OpenSSL::SSL::SSLSocket.expects(:===).with(socket).returns(true).at_least_once
OpenSSL::SSL::SSLSocket.expects(:new).with(socket, instance_of(OpenSSL::SSL::SSLContext)).returns(socket).at_least_once
socket.stubs(:sync_close=).returns(true)
socket.expects(:connect).with().at_least_once
# MRI's Net::HTTP switched from stdlib's timeout.rb to an internal
# implementation using io/wait and connect_nonblock in Ruby 2.3.
# See https://github.com/ruby/ruby/commit/bab5bf0c79ba
#
# Although recent versions of Rubinius report themselves as Ruby 2.3,
# they do not yet incorporate this change.
if RUBY_VERSION >= "2.3.0" && RUBY_ENGINE != "rbx"
socket.expects(:connect_nonblock).with(any_parameters).at_least_once
else
socket.expects(:connect).with().at_least_once
end
if RUBY_VERSION >= "2.0.0" && RUBY_PLATFORM != "java"
socket.expects(:session).with().at_least_once
end
......
......@@ -45,4 +45,30 @@ class TestRegisteringWithIO < Test::Unit::TestCase
Net::BufferedIO.new(unsupported)
end
end
def test_creating_net_buffered_io_with_ruby_24_method_signature
# These keyword arguments were added to BufferedIO.new's params in Ruby 2.4
call_with_keyword_args = lambda do
eval <<-RUBY
Net::BufferedIO.new("", read_timeout: 1, continue_timeout: 1, debug_output: nil)
RUBY
end
if RUBY_VERSION >= "2.4.0"
# Should not raise
call_with_keyword_args.call
elsif RUBY_VERSION >= "2.0.0"
# From Ruby 2.0 to 2.3, keyword arguments are generally valid syntax, but
# had not been added to BufferedIO.new's method signature
assert_raises(ArgumentError) { call_with_keyword_args.call }
elsif RUBY_VERSION >= "1.9.0"
# Ruby 1.9 will interpret the arguments as a new-style options hash,
# which is also not in the method signature
assert_raises(ArgumentError) { call_with_keyword_args.call }
else
# Ruby 1.8 won't know how to parse this, since it had neither new-style
# hashes nor keyword arguments
assert_raises(SyntaxError) { call_with_keyword_args.call }
end
end
end
require 'test_helper'
class TestTimeouts < Test::Unit::TestCase
class TestStubSocketCompatibility < Test::Unit::TestCase
def test_sending_timeout_accessors_after_starting_session
# this tests an HTTPS request because #ssl_timeout= raises otherwise
FakeWeb.register_uri(:get, "https://example.com", :status => [200, "OK"])
......@@ -30,4 +30,22 @@ class TestTimeouts < Test::Unit::TestCase
socket = http.instance_variable_get(:@socket)
assert_equal RUBY_VERSION >= "1.9.3", socket.respond_to?(:continue_timeout=)
end
def test_stub_socket_responds_to_close_and_always_returns_true
FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
http = Net::HTTP.new("example.com", 80)
http.get("/")
socket = http.instance_variable_get(:@socket)
assert_equal socket.close, true
end
def test_stub_socket_tracks_closed_state
FakeWeb.register_uri(:get, "http://example.com", :status => [200, "OK"])
http = Net::HTTP.new("example.com", 80)
http.get("/")
socket = http.instance_variable_get(:@socket)
assert !socket.closed?
socket.close
assert socket.closed?
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment