Removes legacy tests as replaced by RSpec (#939)

This commit is contained in:
Mattia 2019-03-08 11:00:56 +00:00 committed by GitHub
parent 626d68f6ff
commit 4102f3add4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 2 additions and 596 deletions

View File

@ -1,11 +1,7 @@
# frozen_string_literal: true
require 'rake/testtask'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task default: :test
desc 'Run all tests'
task test: [:spec]
task default: :spec

View File

@ -1,35 +0,0 @@
# frozen_string_literal: true
require File.expand_path('integration', __dir__)
module Adapters
class EMHttpTest < Faraday::TestCase
def adapter
:em_http
end
unless jruby? && ssl_mode?
Integration.apply(self, :Parallel, :NonStreaming, :ParallelNonStreaming) do
# https://github.com/eventmachine/eventmachine/pull/289
undef :test_timeout
def test_binds_local_socket
host = '1.2.3.4'
conn = create_connection request: { bind: { host: host } }
assert_equal host, conn.options[:bind][:host]
end
end
# https://github.com/eventmachine/eventmachine/issues/180
end
def test_custom_adapter_config
url = URI('https://example.com:1234')
adapter = Faraday::Adapter::EMHttp.new nil, inactivity_timeout: 20
req = adapter.create_request(url: url, request: {})
assert_equal 20, req.connopts.inactivity_timeout
end
end
end

View File

@ -1,35 +0,0 @@
# frozen_string_literal: true
require File.expand_path('integration', __dir__)
module Adapters
class EMSynchronyTest < Faraday::TestCase
def adapter
:em_synchrony
end
unless jruby?
Integration.apply(self, :Parallel, :NonStreaming, :ParallelNonStreaming) do
# https://github.com/eventmachine/eventmachine/pull/289
undef :test_timeout
def test_binds_local_socket
host = '1.2.3.4'
conn = create_connection request: { bind: { host: host } }
# put conn.get('/who-am-i').body
assert_equal host, conn.options[:bind][:host]
end
end
end
def test_custom_adapter_config
url = URI('https://example.com:1234')
adapter = Faraday::Adapter::EMSynchrony.new nil, inactivity_timeout: 20
req = adapter.create_request(url: url, request: {})
assert_equal 20, req.connopts.inactivity_timeout
end
end
end

View File

@ -1,147 +0,0 @@
# frozen_string_literal: true
require 'forwardable'
require File.expand_path('../helper', __dir__)
require File.expand_path('../shared', __dir__)
Faraday.require_lib 'autoload'
module Adapters
# Adapter integration tests. To use, implement two methods:
#
# `#adapter` required. returns a symbol for the adapter middleware name
# `#adapter_options` optional. extra arguments for building an adapter
module Integration
def self.apply(base, *extra_features)
if base.live_server
features = [:Common]
features.concat extra_features
features << :SSL if base.ssl_mode?
features.each { |name| base.send(:include, const_get(name)) }
yield if block_given?
elsif !defined? @warned
warn 'Warning: Not running integration tests against a live server.'
warn 'Start the server `ruby test/live_server.rb` and set the LIVE=1 env variable.'
warn 'See CONTRIBUTING for usage.'
@warned = true
end
end
module NonParallel
def test_no_parallel_support
connection = create_connection
response = nil
err = capture_warnings do
connection.in_parallel do
response = connection.get('echo').body
end
end
assert response
assert_match 'no parallel-capable adapter on Faraday stack', err
assert_match __FILE__, err
end
end
module ParallelNonStreaming
def test_callback_is_called_in_parallel_with_no_streaming_support
resp1 = nil
resp2 = nil
streamed1 = nil
streamed2 = nil
connection = create_connection
err = capture_warnings do
connection.in_parallel do
resp1, streamed1 = streaming_request(connection, :get, 'stream?a=1')
resp2, streamed2 = streaming_request(connection, :get, 'stream?b=2', chunk_size: 16 * 1024)
assert connection.in_parallel?
assert_nil resp1.body
assert_nil resp2.body
assert_equal [], streamed1
assert_equal [], streamed2
end
end
assert !connection.in_parallel?
assert_match(/Streaming .+ not yet implemented/, err)
opts = { streaming?: false, chunk_size: 16 * 1024 }
check_streaming_response(streamed1, opts.merge(prefix: '{"a"=>"1"}'))
check_streaming_response(streamed2, opts.merge(prefix: '{"b"=>"2"}'))
end
end
module NonStreaming
include Faraday::Shared
def test_GET_streaming
response, streamed = nil
err = capture_warnings do
response, streamed = streaming_request(create_connection, :get, 'stream')
end
assert_match(/Streaming .+ not yet implemented/, err)
check_streaming_response(streamed, streaming?: false)
assert_equal big_string, response.body
end
def test_non_GET_streaming
response, streamed = nil
err = capture_warnings do
response, streamed = streaming_request(create_connection, :post, 'stream')
end
assert_match(/Streaming .+ not yet implemented/, err)
check_streaming_response(streamed, streaming?: false)
assert_equal big_string, response.body
end
end
module SSL
def test_GET_ssl_fails_with_bad_cert
ca_file = 'tmp/faraday-different-ca-cert.crt'
conn = create_connection(ssl: { ca_file: ca_file })
err = assert_raises Faraday::SSLError do
conn.get('/ssl')
end
assert_includes err.message, 'certificate'
end
end
module Common
extend Forwardable
def_delegators :create_connection, :get, :head, :put, :post, :patch, :delete, :run_request
def adapter
raise NotImplementedError, 'Need to override #adapter'
end
# extra options to pass when building the adapter
def adapter_options
[]
end
def create_connection(options = {}, &optional_connection_config_blk)
builder_block = if adapter == :default
nil
else
proc do |b|
b.request :multipart
b.request :url_encoded
b.adapter adapter, *adapter_options, &optional_connection_config_blk
end
end
server = self.class.live_server
url = format("#{server.scheme}://#{server.host}:#{server.port}")
options[:ssl] ||= {}
options[:ssl][:ca_file] ||= ENV['SSL_FILE']
Faraday::Connection.new(url, options, &builder_block).tap do |conn|
conn.headers['X-Faraday-Adapter'] = adapter.to_s
adapter_handler = conn.builder.handlers.last
conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
end
end
end
end
end

View File

@ -1,42 +0,0 @@
# frozen_string_literal: true
require File.expand_path('integration', __dir__)
require File.expand_path('../live_server', __dir__)
module Adapters
class RackTest < Faraday::TestCase
def adapter
:rack
end
def adapter_options
[Faraday::LiveServer]
end
# no Integration.apply because this doesn't require a server as a separate process
include Integration::Common
include Integration::NonParallel
include Integration::NonStreaming
# Rack::MockResponse doesn't provide any way to access the reason phrase,
# so override the shared test from Common.
def test_GET_reason_phrase
response = get('echo')
assert_nil response.reason_phrase
end
# not using shared test because error is swallowed by Sinatra
def test_timeout
conn = create_connection(request: { timeout: 1, open_timeout: 1 })
begin
conn.get '/slow'
rescue Faraday::ClientError # rubocop:disable Lint/HandleExceptions
end
end
# test not applicable
undef test_connection_error
undef test_proxy
undef test_proxy_auth_fail
end
end

View File

@ -1,159 +0,0 @@
# frozen_string_literal: true
require File.expand_path('../helper', __dir__)
module Adapters
class TestMiddleware < Faraday::TestCase
Stubs = Faraday::Adapter.lookup_middleware(:test)::Stubs
def setup
@stubs = Stubs.new do |stub|
stub.get('/hello') do
[200, { 'Content-Type' => 'text/html' }, 'hello']
end
stub.get('/method-echo') do |env|
[200, { 'Content-Type' => 'text/html' }, env[:method].to_s]
end
stub.get(%r{\A/resources/\d+(?:\?|\z)}) do
[200, { 'Content-Type' => 'text/html' }, 'show']
end
stub.get(%r{\A/resources/(specified)\z}) do |_env, meta|
[200, { 'Content-Type' => 'text/html' }, "show #{meta[:match_data][1]}"]
end
stub.get('http://domain.test/hello') do
[200, { 'Content-Type' => 'text/html' }, 'domain: hello']
end
stub.get('http://wrong.test/hello') do
[200, { 'Content-Type' => 'text/html' }, 'wrong: hello']
end
stub.get('http://wrong.test/bait') do
[404, { 'Content-Type' => 'text/html' }]
end
end
@conn = Faraday.new do |builder|
builder.adapter :test, @stubs
end
@resp = @conn.get('/hello')
end
def test_middleware_with_simple_path_sets_status
assert_equal 200, @resp.status
end
def test_middleware_with_simple_path_sets_headers
assert_equal 'text/html', @resp.headers['Content-Type']
end
def test_middleware_with_simple_path_sets_body
assert_equal 'hello', @resp.body
end
def test_middleware_with_host_points_to_the_right_stub
assert_equal 'domain: hello', @conn.get('http://domain.test/hello').body
end
def test_middleware_can_be_called_several_times
assert_equal 'hello', @conn.get('/hello').body
end
def test_middleware_can_handle_regular_expression_path
assert_equal 'show', @conn.get('/resources/1').body
end
def test_middleware_can_handle_single_parameter_block
assert_equal 'get', @conn.get('/method-echo').body
end
def test_middleware_can_handle_regular_expression_path_with_captured_result
assert_equal 'show specified', @conn.get('/resources/specified').body
end
def test_middleware_with_get_params
@stubs.get('/param?a=1') { [200, {}, 'a'] }
assert_equal 'a', @conn.get('/param?a=1').body
end
def test_middleware_ignores_unspecified_get_params
@stubs.get('/optional?a=1') { [200, {}, 'a'] }
assert_equal 'a', @conn.get('/optional?a=1&b=1').body
assert_equal 'a', @conn.get('/optional?a=1').body
assert_raises Faraday::Adapter::Test::Stubs::NotFound do
@conn.get('/optional')
end
end
def test_middleware_with_http_headers
@stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
@stubs.get('/yo') { [200, {}, 'b'] }
assert_equal 'a', @conn.get('/yo') { |env| env.headers['X-HELLO'] = 'hello' }.body
assert_equal 'b', @conn.get('/yo').body
end
def test_middleware_allow_different_outcomes_for_the_same_request
@stubs.get('/hello') { [200, { 'Content-Type' => 'text/html' }, 'hello'] }
@stubs.get('/hello') { [200, { 'Content-Type' => 'text/html' }, 'world'] }
assert_equal 'hello', @conn.get('/hello').body
assert_equal 'world', @conn.get('/hello').body
end
def test_yields_env_to_stubs
@stubs.get '/hello' do |env|
assert_equal '/hello', env[:url].path
assert_equal 'foo.com', env[:url].host
assert_equal '1', env[:params]['a']
assert_equal 'text/plain', env[:request_headers]['Accept']
[200, {}, 'a']
end
@conn.headers['Accept'] = 'text/plain'
assert_equal 'a', @conn.get('http://foo.com/hello?a=1').body
end
def test_parses_params_with_default_encoder
@stubs.get '/hello' do |env|
assert_equal '1', env[:params]['a']['b']
[200, {}, 'a']
end
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end
def test_parses_params_with_nested_encoder
@stubs.get '/hello' do |env|
assert_equal '1', env[:params]['a']['b']
[200, {}, 'a']
end
@conn.options.params_encoder = Faraday::NestedParamsEncoder
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end
def test_parses_params_with_flat_encoder
@stubs.get '/hello' do |env|
assert_equal '1', env[:params]['a[b]']
[200, {}, 'a']
end
@conn.options.params_encoder = Faraday::FlatParamsEncoder
assert_equal 'a', @conn.get('http://foo.com/hello?a[b]=1').body
end
def test_raises_an_error_if_no_stub_is_found_for_request
assert_raises Stubs::NotFound do
@conn.get('/invalid') { [200, {}, []] }
end
end
def test_raises_an_error_if_no_stub_is_found_for_specified_host
assert_raises Stubs::NotFound do
@conn.get('http://domain.test/bait')
end
end
def test_raises_an_error_if_no_stub_is_found_for_request_without_this_header
@stubs.get('/yo', 'X-HELLO' => 'hello') { [200, {}, 'a'] }
assert_raises Faraday::Adapter::Test::Stubs::NotFound do
@conn.get('/yo')
end
end
end
end

View File

@ -1,74 +0,0 @@
# frozen_string_literal: true
require 'simplecov'
require 'coveralls'
SimpleCov.formatters = [SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter]
SimpleCov.start do
add_filter '/bundle/'
add_filter '/test/'
# minimum_coverage(87)
end
gem 'minitest' if defined? Bundler
require 'minitest/autorun'
require File.expand_path('../lib/faraday', __dir__)
require 'stringio'
require 'uri'
module Faraday
module LiveServerConfig
def live_server=(value)
@live_server = case value
when /^http/
URI(value)
when /./
URI('http://127.0.0.1:4567')
end
end
def live_server?
defined? @live_server
end
# Returns an object that responds to `host` and `port`.
def live_server
live_server? && @live_server
end
end
class TestCase < MiniTest::Test
extend LiveServerConfig
self.live_server = ENV['LIVE']
def capture_warnings
old = $stderr
$stderr = StringIO.new
begin
yield
$stderr.string
ensure
$stderr = old
end
end
def self.jruby?
defined? RUBY_ENGINE && (RUBY_ENGINE == 'jruby')
end
def self.rbx?
defined? RUBY_ENGINE && (RUBY_ENGINE == 'rbx')
end
def self.ruby_22_plus?
RUBY_VERSION > '2.2'
end
def self.ssl_mode?
ENV['SSL'] == 'yes'
end
end
end

View File

@ -1,84 +0,0 @@
# frozen_string_literal: true
require 'sinatra/base'
require_relative 'shared'
module Faraday
class LiveServer < Sinatra::Base
set :environment, :test
disable :logging
disable :protection
%i[get post put patch delete options].each do |method|
send(method, '/echo') do
kind = request.request_method.downcase
out = kind.dup
out << ' ?' << request.GET.inspect if request.GET.any?
out << ' ' << request.POST.inspect if request.POST.any?
content_type 'text/plain'
return out
end
end
%i[get post].each do |method|
send(method, '/stream') do
content_type :txt
stream do |out|
out << request.GET.inspect if request.GET.any?
out << request.POST.inspect if request.POST.any?
out << Faraday::Shared.big_string
end
end
end
%i[get post].each do |method|
send(method, '/empty_stream') do
content_type :txt
stream do |out|
out << ''
end
end
end
get '/echo_header' do
header = "HTTP_#{params[:name].tr('-', '_').upcase}"
request.env.fetch(header) { 'NONE' }
end
post '/file' do
if params[:uploaded_file].respond_to? :each_key
format("file #{params[:uploaded_file][:filename]} #{params[:uploaded_file][:type]} #{params[:uploaded_file][:tempfile].size}")
else
status 400
end
end
get '/multi' do
[200, { 'Set-Cookie' => 'one, two' }, '']
end
get '/who-am-i' do
request.env['REMOTE_ADDR']
end
get '/slow' do
sleep 10
[200, {}, 'ok']
end
get '/204' do
status 204 # no content
end
get '/ssl' do
request.secure?.to_s
end
error do |e|
"#{e.class}\n#{e}\n#{e.backtrace.join("\n")}"
end
end
end
Faraday::LiveServer.run! if $PROGRAM_NAME == __FILE__

View File

@ -1,14 +0,0 @@
# frozen_string_literal: true
module Faraday
module Shared
def self.big_string
kb = 1024
(32..126).map(&:chr).cycle.take(50 * kb).join
end
def big_string
Faraday::Shared.big_string
end
end
end