2016-03-07 19:52:19 -05:00
|
|
|
# frozen_string_literal: true
|
2018-02-28 19:11:41 -05:00
|
|
|
|
2010-09-17 19:56:32 -04:00
|
|
|
require 'spec_helper'
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2014-04-03 13:25:03 -04:00
|
|
|
RSpec.describe Capybara::Server do
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should spool up a rack server' do
|
|
|
|
@app = proc { |_env| [200, {}, ['Hello Server!']] }
|
2010-01-30 14:31:52 -05:00
|
|
|
@server = Capybara::Server.new(@app).boot
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2009-12-13 08:33:14 -05:00
|
|
|
@res = Net::HTTP.start(@server.host, @server.port) { |http| http.get('/') }
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@res.body).to include('Hello Server')
|
2009-12-13 08:33:14 -05:00
|
|
|
end
|
2010-03-12 13:14:10 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should do nothing when no server given' do
|
2012-10-30 09:26:19 -04:00
|
|
|
expect do
|
2010-03-12 13:14:10 -05:00
|
|
|
@server = Capybara::Server.new(nil).boot
|
2012-10-30 09:26:19 -04:00
|
|
|
end.not_to raise_error
|
2010-03-12 13:14:10 -05:00
|
|
|
end
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should bind to the specified host' do
|
2018-02-28 19:11:41 -05:00
|
|
|
# TODO: travis with jruby in container mode has an issue with this test
|
2018-07-10 17:18:39 -04:00
|
|
|
skip 'This platform has an issue with this test' if (ENV['TRAVIS'] && (RUBY_ENGINE == 'jruby')) || Gem.win_platform?
|
|
|
|
|
2013-09-11 17:26:37 -04:00
|
|
|
begin
|
2018-02-28 19:11:41 -05:00
|
|
|
app = proc { |_env| [200, {}, ['Hello Server!']] }
|
2013-09-11 17:26:37 -04:00
|
|
|
|
|
|
|
Capybara.server_host = '127.0.0.1'
|
|
|
|
server = Capybara::Server.new(app).boot
|
|
|
|
res = Net::HTTP.get(URI("http://127.0.0.1:#{server.port}"))
|
|
|
|
expect(res).to eq('Hello Server!')
|
|
|
|
|
|
|
|
Capybara.server_host = '0.0.0.0'
|
|
|
|
server = Capybara::Server.new(app).boot
|
|
|
|
res = Net::HTTP.get(URI("http://127.0.0.1:#{server.port}"))
|
|
|
|
expect(res).to eq('Hello Server!')
|
|
|
|
ensure
|
|
|
|
Capybara.server_host = nil
|
|
|
|
end
|
2018-07-10 17:18:39 -04:00
|
|
|
end
|
2011-10-05 15:09:37 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should use specified port' do
|
2010-09-02 09:03:49 -04:00
|
|
|
Capybara.server_port = 22789
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
@app = proc { |_env| [200, {}, ['Hello Server!']] }
|
2010-09-02 09:03:49 -04:00
|
|
|
@server = Capybara::Server.new(@app).boot
|
2010-09-28 16:37:43 -04:00
|
|
|
|
2010-09-02 09:03:49 -04:00
|
|
|
@res = Net::HTTP.start(@server.host, 22789) { |http| http.get('/') }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@res.body).to include('Hello Server')
|
2010-09-02 09:03:49 -04:00
|
|
|
|
|
|
|
Capybara.server_port = nil
|
|
|
|
end
|
2010-09-28 16:37:43 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should use given port' do
|
|
|
|
@app = proc { |_env| [200, {}, ['Hello Server!']] }
|
2018-05-01 16:36:42 -04:00
|
|
|
@server = Capybara::Server.new(@app, port: 22790).boot
|
2012-07-13 09:19:20 -04:00
|
|
|
|
|
|
|
@res = Net::HTTP.start(@server.host, 22790) { |http| http.get('/') }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@res.body).to include('Hello Server')
|
2012-07-13 09:19:20 -04:00
|
|
|
|
|
|
|
Capybara.server_port = nil
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should find an available port' do
|
|
|
|
@app1 = proc { |_env| [200, {}, ['Hello Server!']] }
|
|
|
|
@app2 = proc { |_env| [200, {}, ['Hello Second Server!']] }
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2010-01-30 14:31:52 -05:00
|
|
|
@server1 = Capybara::Server.new(@app1).boot
|
|
|
|
@server2 = Capybara::Server.new(@app2).boot
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2009-12-13 08:33:14 -05:00
|
|
|
@res1 = Net::HTTP.start(@server1.host, @server1.port) { |http| http.get('/') }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@res1.body).to include('Hello Server')
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2009-12-13 08:33:14 -05:00
|
|
|
@res2 = Net::HTTP.start(@server2.host, @server2.port) { |http| http.get('/') }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@res2.body).to include('Hello Second Server')
|
2009-12-13 08:33:14 -05:00
|
|
|
end
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should support SSL' do
|
2018-05-01 16:36:42 -04:00
|
|
|
begin
|
2018-07-10 17:18:39 -04:00
|
|
|
key = File.join(Dir.pwd, 'spec', 'fixtures', 'key.pem')
|
|
|
|
cert = File.join(Dir.pwd, 'spec', 'fixtures', 'certificate.pem')
|
2018-05-01 16:36:42 -04:00
|
|
|
Capybara.server = :puma, { Host: "ssl://#{Capybara.server_host}?key=#{key}&cert=#{cert}" }
|
|
|
|
app = proc { |_env| [200, {}, ['Hello SSL Server!']] }
|
|
|
|
server = Capybara::Server.new(app).boot
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Net::HTTP.start(server.host, server.port) { |http| http.get('/__idntify__') }
|
|
|
|
end.to raise_error(EOFError)
|
|
|
|
|
|
|
|
res = Net::HTTP.start(server.host, server.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |https|
|
|
|
|
https.get('/')
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(res.body).to include('Hello SSL Server!')
|
|
|
|
ensure
|
|
|
|
Capybara.server = :default
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'When Capybara.reuse_server is true' do
|
2016-01-18 17:01:12 -05:00
|
|
|
before do
|
|
|
|
@old_reuse_server = Capybara.reuse_server
|
|
|
|
Capybara.reuse_server = true
|
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
|
2016-01-18 17:01:12 -05:00
|
|
|
after do
|
|
|
|
Capybara.reuse_server = @old_reuse_server
|
|
|
|
end
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should use the existing server if it already running' do
|
|
|
|
@app = proc { |_env| [200, {}, ['Hello Server!']] }
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2016-01-18 17:01:12 -05:00
|
|
|
@server1 = Capybara::Server.new(@app).boot
|
|
|
|
@server2 = Capybara::Server.new(@app).boot
|
|
|
|
|
|
|
|
res = Net::HTTP.start(@server1.host, @server1.port) { |http| http.get('/') }
|
|
|
|
expect(res.body).to include('Hello Server')
|
|
|
|
|
|
|
|
res = Net::HTTP.start(@server2.host, @server2.port) { |http| http.get('/') }
|
|
|
|
expect(res.body).to include('Hello Server')
|
|
|
|
|
|
|
|
expect(@server1.port).to eq(@server2.port)
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'detects and waits for all reused server sessions pending requests' do
|
2017-05-25 22:18:38 -04:00
|
|
|
done = 0
|
2016-01-18 17:01:12 -05:00
|
|
|
|
|
|
|
app = proc do |env|
|
|
|
|
request = Rack::Request.new(env)
|
|
|
|
sleep request.params['wait_time'].to_f
|
2017-05-25 22:18:38 -04:00
|
|
|
done += 1
|
2018-07-10 17:18:39 -04:00
|
|
|
[200, {}, ['Hello Server!']]
|
2016-01-18 17:01:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
server1 = Capybara::Server.new(app).boot
|
|
|
|
server2 = Capybara::Server.new(app).boot
|
|
|
|
|
2018-02-28 19:11:41 -05:00
|
|
|
expect do
|
2017-05-25 22:18:38 -04:00
|
|
|
start_request(server1, 1.0)
|
|
|
|
start_request(server2, 3.0)
|
2016-01-18 17:01:12 -05:00
|
|
|
server1.wait_for_pending_requests
|
2018-02-28 19:11:41 -05:00
|
|
|
end.to change { done }.from(0).to(2)
|
2016-07-25 14:34:15 -04:00
|
|
|
expect(server2.send(:pending_requests?)).to eq(false)
|
2016-01-18 17:01:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
context 'When Capybara.reuse_server is false' do
|
2016-01-18 17:01:12 -05:00
|
|
|
before do
|
|
|
|
@old_reuse_server = Capybara.reuse_server
|
|
|
|
Capybara.reuse_server = false
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Capybara.reuse_server = @old_reuse_server
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should not reuse an already running server' do
|
|
|
|
@app = proc { |_env| [200, {}, ['Hello Server!']] }
|
2016-01-18 17:01:12 -05:00
|
|
|
|
|
|
|
@server1 = Capybara::Server.new(@app).boot
|
|
|
|
@server2 = Capybara::Server.new(@app).boot
|
|
|
|
|
|
|
|
res = Net::HTTP.start(@server1.host, @server1.port) { |http| http.get('/') }
|
|
|
|
expect(res.body).to include('Hello Server')
|
|
|
|
|
|
|
|
res = Net::HTTP.start(@server2.host, @server2.port) { |http| http.get('/') }
|
|
|
|
expect(res.body).to include('Hello Server')
|
|
|
|
|
|
|
|
expect(@server1.port).not_to eq(@server2.port)
|
|
|
|
end
|
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'detects and waits for only one sessions pending requests' do
|
2017-05-25 22:18:38 -04:00
|
|
|
done = 0
|
2016-01-18 17:01:12 -05:00
|
|
|
|
|
|
|
app = proc do |env|
|
|
|
|
request = Rack::Request.new(env)
|
|
|
|
sleep request.params['wait_time'].to_f
|
2017-05-25 22:18:38 -04:00
|
|
|
done += 1
|
2018-07-10 17:18:39 -04:00
|
|
|
[200, {}, ['Hello Server!']]
|
2016-01-18 17:01:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
server1 = Capybara::Server.new(app).boot
|
|
|
|
server2 = Capybara::Server.new(app).boot
|
|
|
|
|
2018-02-28 19:11:41 -05:00
|
|
|
expect do
|
2017-05-25 22:18:38 -04:00
|
|
|
start_request(server1, 1.0)
|
|
|
|
start_request(server2, 3.0)
|
2016-01-18 17:01:12 -05:00
|
|
|
server1.wait_for_pending_requests
|
2018-02-28 19:11:41 -05:00
|
|
|
end.to change { done }.from(0).to(1)
|
2016-07-25 14:34:15 -04:00
|
|
|
expect(server2.send(:pending_requests?)).to eq(true)
|
2018-02-28 19:11:41 -05:00
|
|
|
expect do
|
2017-05-25 22:18:38 -04:00
|
|
|
server2.wait_for_pending_requests
|
2018-02-28 19:11:41 -05:00
|
|
|
end.to change { done }.from(1).to(2)
|
2016-01-18 17:01:12 -05:00
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
end
|
2012-03-06 17:42:47 -05:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'should raise server errors when the server errors before the timeout' do
|
2012-03-06 17:42:47 -05:00
|
|
|
begin
|
2016-08-17 16:51:32 -04:00
|
|
|
Capybara.register_server :kaboom do
|
2012-03-06 17:42:47 -05:00
|
|
|
sleep 0.1
|
|
|
|
raise 'kaboom'
|
|
|
|
end
|
2016-08-17 16:51:32 -04:00
|
|
|
Capybara.server = :kaboom
|
2012-03-06 17:42:47 -05:00
|
|
|
|
2013-11-14 12:43:36 -05:00
|
|
|
expect do
|
2018-02-28 19:11:41 -05:00
|
|
|
Capybara::Server.new(proc { |e| }).boot
|
2013-11-14 12:43:36 -05:00
|
|
|
end.to raise_error(RuntimeError, 'kaboom')
|
2012-03-06 17:42:47 -05:00
|
|
|
ensure
|
2016-08-17 16:51:32 -04:00
|
|
|
Capybara.server = :default
|
2012-03-06 17:42:47 -05:00
|
|
|
end
|
|
|
|
end
|
2013-03-16 14:59:02 -04:00
|
|
|
|
2018-07-10 17:18:39 -04:00
|
|
|
it 'is not #responsive? when Net::HTTP raises a SystemCallError' do
|
2018-02-28 19:11:41 -05:00
|
|
|
app = -> { [200, {}, ['Hello, world']] }
|
2013-03-17 19:35:48 -04:00
|
|
|
server = Capybara::Server.new(app)
|
2018-04-27 14:01:47 -04:00
|
|
|
allow(Net::HTTP).to receive(:start).and_raise(SystemCallError.allocate)
|
2013-03-17 19:35:48 -04:00
|
|
|
expect(server.responsive?).to eq false
|
2013-03-16 14:59:02 -04:00
|
|
|
end
|
2015-07-06 19:19:47 -04:00
|
|
|
|
2018-05-01 16:36:42 -04:00
|
|
|
[EOFError, Net::ReadTimeout].each do |err|
|
|
|
|
it "should attempt an HTTPS connection if HTTP connection returns #{err}" do
|
|
|
|
app = -> { [200, {}, ['Hello, world']] }
|
|
|
|
ordered_errors = [Errno::ECONNREFUSED, err]
|
|
|
|
allow(Net::HTTP).to receive(:start).with(anything, anything, hash_excluding(:use_ssl)) do
|
|
|
|
raise ordered_errors.shift
|
|
|
|
end
|
|
|
|
response = Net::HTTPSuccess.allocate
|
|
|
|
allow(response).to receive(:body).and_return app.object_id.to_s
|
|
|
|
allow(Net::HTTP).to receive(:start).with(anything, anything, hash_including(use_ssl: true)).and_return(response).once
|
|
|
|
Capybara::Server.new(app).boot
|
|
|
|
expect(Net::HTTP).to have_received(:start).exactly(3).times
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-18 17:01:12 -05:00
|
|
|
def start_request(server, wait_time)
|
2015-07-06 19:19:47 -04:00
|
|
|
# Start request, but don't wait for it to finish
|
|
|
|
socket = TCPSocket.new(server.host, server.port)
|
2018-02-28 19:11:41 -05:00
|
|
|
socket.write "GET /?wait_time=#{wait_time} HTTP/1.0\r\n\r\n"
|
2017-05-25 22:18:38 -04:00
|
|
|
sleep 0.1
|
2015-07-06 19:19:47 -04:00
|
|
|
socket.close
|
|
|
|
sleep 0.1
|
|
|
|
end
|
2009-12-13 08:33:14 -05:00
|
|
|
end
|