mirror of
https://github.com/lostisland/faraday.git
synced 2025-08-10 00:03:15 -04:00
script/test
spins up test server, runs test suite
This commit is contained in:
parent
47af24c4d9
commit
e6787c144b
@ -1,8 +1,4 @@
|
||||
before_script:
|
||||
- "nohup bundle exec ruby test/live_server.rb &"
|
||||
- "sleep 2"
|
||||
|
||||
env: LIVE=1
|
||||
script: ruby -rubygems script/test
|
||||
|
||||
language: ruby
|
||||
|
||||
|
13
Rakefile
13
Rakefile
@ -28,16 +28,9 @@ end
|
||||
|
||||
## standard tasks
|
||||
|
||||
Rake::TestTask.new(:test) do |test|
|
||||
test.libs << 'lib' << 'test'
|
||||
test.pattern = 'test/**/*_test.rb'
|
||||
test.verbose = true
|
||||
end
|
||||
|
||||
desc "Run tests including live tests against a local server on port 4567"
|
||||
task :"test:local" do
|
||||
ENV['LIVE'] = '1'
|
||||
Rake::Task[:test].invoke
|
||||
desc "Run all tests"
|
||||
task :test do
|
||||
exec 'script/test'
|
||||
end
|
||||
|
||||
desc "Open an irb session preloaded with this library"
|
||||
|
86
script/test
Executable file
86
script/test
Executable file
@ -0,0 +1,86 @@
|
||||
#!/usr/bin/env ruby -rubygems -w
|
||||
# Runs the test suite against a local server spawned automatically in a
|
||||
# thread. After tests are done, the server is shut down.
|
||||
#
|
||||
# If filename arguments are given, only those files are run. If arguments given
|
||||
# are not filenames, they are taken as words that filter the list of files to run.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# $ script/test
|
||||
# $ script/test test/env_test.rb
|
||||
# $ script/test excon typhoeus
|
||||
|
||||
require 'bundler'
|
||||
Bundler.setup
|
||||
|
||||
host = '127.0.0.1'
|
||||
logfile = 'log/test.log'
|
||||
test_glob = 'test/**/*_test.rb'
|
||||
|
||||
require 'fileutils'
|
||||
FileUtils.mkdir_p 'log'
|
||||
|
||||
# find available port
|
||||
require 'socket'
|
||||
port = begin
|
||||
server = TCPServer.new(host, 0)
|
||||
server.addr[1]
|
||||
ensure
|
||||
server.close if server
|
||||
end
|
||||
|
||||
server = nil
|
||||
|
||||
# start test server in a separate thread
|
||||
thread = Thread.new do
|
||||
old_verbose, $VERBOSE = $VERBOSE, nil
|
||||
begin
|
||||
require File.expand_path('../../test/live_server', __FILE__)
|
||||
ensure
|
||||
$VERBOSE = old_verbose
|
||||
end
|
||||
require 'webrick'
|
||||
log_io = File.open logfile, 'w'
|
||||
webrick_opts = {
|
||||
:Port => port, :Logger => WEBrick::Log::new(log_io),
|
||||
:AccessLog => [[log_io, "[%{X-Faraday-Adapter}i] %m %U -> %s %b"]]
|
||||
}
|
||||
Rack::Handler::WEBrick.run(Faraday::LiveServer, webrick_opts) {|serv| server = serv }
|
||||
end
|
||||
|
||||
# find files to test
|
||||
test_files = Dir[test_glob]
|
||||
if ARGV.any?
|
||||
all_files, test_files = test_files, []
|
||||
for arg in ARGV
|
||||
re = /(\b|_)#{arg}(\b|_)/
|
||||
test_files.concat all_files.select { |f| f =~ re }
|
||||
end
|
||||
end
|
||||
|
||||
require 'net/http'
|
||||
conn = Net::HTTP.new host, port
|
||||
conn.open_timeout = conn.read_timeout = 0.05
|
||||
|
||||
# test if test server is accepting requests
|
||||
responsive = lambda { |path|
|
||||
begin
|
||||
res = conn.start { conn.get(path) }
|
||||
res.is_a?(Net::HTTPSuccess)
|
||||
rescue Errno::ECONNREFUSED, Errno::EBADF, Timeout::Error
|
||||
false
|
||||
end
|
||||
}
|
||||
|
||||
require 'timeout'
|
||||
Timeout.timeout 20 do
|
||||
# block until test server is ready
|
||||
thread.join 0.05 until responsive.call('/echo')
|
||||
end
|
||||
|
||||
ENV['LIVE'] = "http://#{host}:#{port}"
|
||||
system 'ruby', '-Ilib:test', '-S', 'testrb', *test_files
|
||||
|
||||
server.respond_to?(:stop!) ? server.stop! : server.stop
|
||||
thread.join
|
@ -8,7 +8,7 @@ module Adapters
|
||||
# `#adapter_options` optional. extra arguments for building an adapter
|
||||
module Integration
|
||||
def self.apply(base, *extras)
|
||||
if Faraday::TestCase::LIVE_SERVER
|
||||
if base.live_server?
|
||||
([:Common] + extras).each {|name| base.send(:include, self.const_get(name)) }
|
||||
yield if block_given?
|
||||
elsif !defined? @warned
|
||||
@ -179,7 +179,10 @@ module Adapters
|
||||
end
|
||||
end
|
||||
|
||||
Faraday::Connection.new(Faraday::TestCase::LIVE_SERVER, options, &builder_block).tap do |conn|
|
||||
server = self.class.live_server
|
||||
url = 'http://%s:%d' % [server.host, server.port]
|
||||
|
||||
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
|
||||
|
@ -7,7 +7,7 @@ module Adapters
|
||||
def adapter() :rack end
|
||||
|
||||
def adapter_options
|
||||
[FaradayTestServer]
|
||||
[Faraday::LiveServer]
|
||||
end
|
||||
|
||||
# no Integration.apply because this doesn't require a server as a separate process
|
||||
|
@ -9,7 +9,8 @@ unless ENV['CI']
|
||||
end
|
||||
|
||||
require 'test/unit'
|
||||
require 'stringio'
|
||||
require 'webmock/test_unit'
|
||||
WebMock.disable_net_connect! :allow_localhost => true
|
||||
|
||||
if ENV['LEFTRIGHT']
|
||||
begin
|
||||
@ -29,14 +30,34 @@ else
|
||||
Debugger.start
|
||||
end
|
||||
|
||||
require 'stringio'
|
||||
require 'uri'
|
||||
|
||||
module Faraday
|
||||
class TestCase < Test::Unit::TestCase
|
||||
LIVE_SERVER = case ENV['LIVE']
|
||||
when /^http/ then ENV['LIVE']
|
||||
when nil then nil
|
||||
else 'http://127.0.0.1:4567'
|
||||
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? and @@live_server
|
||||
end
|
||||
end
|
||||
|
||||
class TestCase < Test::Unit::TestCase
|
||||
extend LiveServerConfig
|
||||
self.live_server = ENV['LIVE']
|
||||
|
||||
def test_default
|
||||
assert true
|
||||
end unless defined? ::MiniTest
|
||||
@ -52,6 +73,3 @@ module Faraday
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
require 'webmock/test_unit'
|
||||
WebMock.disable_net_connect!(:allow => Faraday::TestCase::LIVE_SERVER)
|
||||
|
@ -1,8 +1,10 @@
|
||||
require 'sinatra/base'
|
||||
|
||||
class FaradayTestServer < Sinatra::Base
|
||||
set :logging => false,
|
||||
:show_exceptions => false
|
||||
module Faraday
|
||||
class LiveServer < Sinatra::Base
|
||||
set :environment, :test
|
||||
disable :logging
|
||||
disable :protection
|
||||
|
||||
[:get, :post, :put, :patch, :delete, :options].each do |method|
|
||||
send(method, '/echo') do
|
||||
@ -48,7 +50,8 @@ class FaradayTestServer < Sinatra::Base
|
||||
"#{e.class}\n#{e.to_s}\n#{e.backtrace.join("\n")}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
FaradayTestServer.run!
|
||||
Faraday::LiveServer.run!
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user