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
|
2009-12-13 08:33:14 -05:00
|
|
|
|
|
|
|
it "should spool up a rack server" do
|
2013-02-27 08:05:55 -05:00
|
|
|
@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
|
|
|
|
|
|
|
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
|
|
|
|
2011-10-05 15:09:37 -04:00
|
|
|
it "should bind to the specified host" do
|
2013-09-11 17:26:37 -04:00
|
|
|
begin
|
|
|
|
app = proc { |env| [200, {}, ['Hello Server!']] }
|
|
|
|
|
|
|
|
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
|
2011-10-05 15:09:37 -04:00
|
|
|
end
|
|
|
|
|
2010-09-02 09:03:49 -04:00
|
|
|
it "should use specified port" do
|
|
|
|
Capybara.server_port = 22789
|
|
|
|
|
2013-02-27 08:05:55 -05: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
|
|
|
|
2012-07-13 09:19:20 -04:00
|
|
|
it "should use given port" do
|
2013-02-27 08:05:55 -05:00
|
|
|
@app = proc { |env| [200, {}, ["Hello Server!"]]}
|
2012-07-13 09:19:20 -04:00
|
|
|
@server = Capybara::Server.new(@app, 22790).boot
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2009-12-13 08:33:14 -05:00
|
|
|
it "should find an available port" do
|
2013-02-27 08:05:55 -05:00
|
|
|
@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
|
|
|
|
2009-12-13 08:33:14 -05:00
|
|
|
it "should use the server if it already running" do
|
2013-02-27 08:05:55 -05:00
|
|
|
@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
|
|
|
@server1a = Capybara::Server.new(@app1).boot
|
|
|
|
@server1b = Capybara::Server.new(@app1).boot
|
|
|
|
@server2a = Capybara::Server.new(@app2).boot
|
|
|
|
@server2b = 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(@server1b.host, @server1b.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(@server2b.host, @server2b.port) { |http| http.get('/') }
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@res2.body).to include('Hello Second Server')
|
2010-09-15 12:19:50 -04:00
|
|
|
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(@server1a.port).to eq(@server1b.port)
|
|
|
|
expect(@server2a.port).to eq(@server2b.port)
|
2009-12-13 08:33:14 -05:00
|
|
|
end
|
2012-03-06 17:42:47 -05:00
|
|
|
|
|
|
|
it "should raise server errors when the server errors before the timeout" do
|
|
|
|
begin
|
|
|
|
Capybara.server do
|
|
|
|
sleep 0.1
|
|
|
|
raise 'kaboom'
|
|
|
|
end
|
|
|
|
|
2013-11-14 12:43:36 -05:00
|
|
|
expect do
|
2012-03-06 17:42:47 -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
|
|
|
|
# TODO refactor out the defaults so it's reliant on unset state instead of
|
|
|
|
# a one-time call in capybara.rb
|
|
|
|
Capybara.server {|app, port| Capybara.run_default_server(app, port)}
|
|
|
|
end
|
|
|
|
end
|
2013-03-16 14:59:02 -04:00
|
|
|
|
2013-03-17 19:35:48 -04:00
|
|
|
it "is not #responsive? when Net::HTTP raises a SystemCallError" do
|
|
|
|
app = lambda { [200, {}, ['Hello, world']] }
|
|
|
|
server = Capybara::Server.new(app)
|
2013-11-14 12:43:36 -05:00
|
|
|
expect(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
|
2009-12-13 08:33:14 -05:00
|
|
|
end
|