2012-05-04 15:50:30 -04:00
|
|
|
require 'spec_helper'
|
2012-07-08 15:17:08 -04:00
|
|
|
require 'capybara/webkit/connection'
|
2012-05-04 15:50:30 -04:00
|
|
|
|
2012-07-08 15:17:08 -04:00
|
|
|
describe Capybara::Webkit::Connection do
|
2012-05-04 15:50:30 -04:00
|
|
|
it "boots a server to talk to" do
|
2012-11-16 23:44:03 -05:00
|
|
|
url = "http://#{@rack_server.host}:#{@rack_server.port}/"
|
2012-05-04 15:50:30 -04:00
|
|
|
connection.puts "Visit"
|
|
|
|
connection.puts 1
|
|
|
|
connection.puts url.to_s.bytesize
|
|
|
|
connection.print url
|
2013-08-11 10:36:53 -04:00
|
|
|
connection.gets.should eq "ok\n"
|
|
|
|
connection.gets.should eq "0\n"
|
2012-05-04 15:50:30 -04:00
|
|
|
connection.puts "Body"
|
|
|
|
connection.puts 0
|
2013-08-11 10:36:53 -04:00
|
|
|
connection.gets.should eq "ok\n"
|
2012-05-04 15:50:30 -04:00
|
|
|
response_length = connection.gets.to_i
|
|
|
|
response = connection.read(response_length)
|
|
|
|
response.should include("Hey there")
|
|
|
|
end
|
|
|
|
|
2013-02-02 18:32:20 -05:00
|
|
|
it 'forwards stderr to the given IO object' do
|
2014-02-13 12:28:38 -05:00
|
|
|
read_io, write_io = IO.pipe
|
|
|
|
redirected_connection = Capybara::Webkit::Connection.new(:stderr => write_io)
|
2012-11-22 10:30:43 -05:00
|
|
|
redirected_connection.puts "EnableLogging"
|
|
|
|
redirected_connection.puts 0
|
2014-02-13 12:28:38 -05:00
|
|
|
|
|
|
|
script = 'console.log("hello world")'
|
2012-05-04 15:50:30 -04:00
|
|
|
redirected_connection.puts "Execute"
|
|
|
|
redirected_connection.puts 1
|
|
|
|
redirected_connection.puts script.to_s.bytesize
|
|
|
|
redirected_connection.print script
|
2014-02-13 12:28:38 -05:00
|
|
|
|
|
|
|
expect(read_io).to include_response "hello world \n"
|
2012-05-04 15:50:30 -04:00
|
|
|
end
|
|
|
|
|
2013-02-02 18:38:10 -05:00
|
|
|
it 'does not forward stderr to nil' do
|
|
|
|
IO.should_not_receive(:copy_stream)
|
|
|
|
Capybara::Webkit::Connection.new(:stderr => nil)
|
|
|
|
end
|
|
|
|
|
2013-02-06 20:16:51 -05:00
|
|
|
it 'prints a deprecation warning if the stdout option is used' do
|
|
|
|
Capybara::Webkit::Connection.any_instance.should_receive(:warn)
|
|
|
|
Capybara::Webkit::Connection.new(:stdout => nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not forward stdout to nil if the stdout option is used' do
|
|
|
|
Capybara::Webkit::Connection.any_instance.stub(:warn)
|
|
|
|
IO.should_not_receive(:copy_stream)
|
|
|
|
Capybara::Webkit::Connection.new(:stdout => nil)
|
|
|
|
end
|
2013-02-02 18:38:10 -05:00
|
|
|
|
2012-05-04 15:50:30 -04:00
|
|
|
it "returns the server port" do
|
|
|
|
connection.port.should be_between 0x400, 0xffff
|
|
|
|
end
|
|
|
|
|
2012-07-16 10:52:10 -04:00
|
|
|
it 'sets appropriate options on its socket' do
|
2013-08-11 10:38:19 -04:00
|
|
|
socket = double('socket')
|
2012-08-10 13:30:34 -04:00
|
|
|
TCPSocket.stub(:open).and_return(socket)
|
2012-07-16 10:52:10 -04:00
|
|
|
if defined?(Socket::TCP_NODELAY)
|
2012-11-22 14:20:58 -05:00
|
|
|
socket.should_receive(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, true)
|
2012-07-16 10:52:10 -04:00
|
|
|
else
|
2012-08-10 13:30:34 -04:00
|
|
|
socket.should_not_receive(:setsockopt)
|
2012-07-16 10:52:10 -04:00
|
|
|
end
|
|
|
|
Capybara::Webkit::Connection.new
|
|
|
|
end
|
|
|
|
|
2012-05-04 15:50:30 -04:00
|
|
|
it "chooses a new port number for a new connection" do
|
2012-07-08 15:17:08 -04:00
|
|
|
new_connection = Capybara::Webkit::Connection.new
|
2012-05-04 15:50:30 -04:00
|
|
|
new_connection.port.should_not == connection.port
|
|
|
|
end
|
|
|
|
|
2012-07-08 15:17:08 -04:00
|
|
|
let(:connection) { Capybara::Webkit::Connection.new }
|
2012-05-04 15:50:30 -04:00
|
|
|
|
|
|
|
before(:all) do
|
|
|
|
@app = lambda do |env|
|
|
|
|
body = "<html><body>Hey there</body></html>"
|
|
|
|
[200,
|
|
|
|
{ 'Content-Type' => 'text/html', 'Content-Length' => body.size.to_s },
|
|
|
|
[body]]
|
|
|
|
end
|
|
|
|
@rack_server = Capybara::Server.new(@app)
|
|
|
|
@rack_server.boot
|
|
|
|
end
|
|
|
|
end
|