Permalink
Browse files

Merge branch 'release/0.11.0.1'

  • Loading branch information...
2 parents f2c979a + f74227a commit 189419501476ca7e3b9296fc9f7b1b58a1f11c3a @seangeo seangeo committed Apr 20, 2012
@@ -14,6 +14,7 @@ def initialize(options = {})
$stdout
@ignore_ssl_errors = options[:ignore_ssl_errors]
@skip_image_loading = options[:skip_image_loading]
+ @blacklist_file = options[:blacklist_file]
start_server
connect
end
@@ -109,6 +110,14 @@ def render(path, width, height)
command "Render", path, width, height
end
+ def set_timeout(timeout_in_seconds)
+ command "SetTimeout", timeout_in_seconds
+ end
+
+ def get_timeout
+ command("GetTimeout").to_i
+ end
+
def set_cookie(cookie)
command "SetCookie", cookie
end
@@ -169,6 +178,10 @@ def server_pipe_and_pid(server_path)
cmdline = [server_path]
cmdline << "--ignore-ssl-errors" if @ignore_ssl_errors
cmdline << "--skip-image-loading" if @skip_image_loading
+ if @blacklist_file
+ cmdline << "--blacklist-file"
+ cmdline << @blacklist_file
+ end
pipe = IO.popen(cmdline.join(" "))
[pipe, pipe.pid]
end
@@ -223,7 +236,12 @@ def check
if result.nil?
raise WebkitNoResponseError, "No response received from the server."
elsif result != 'ok'
- raise WebkitInvalidResponseError, read_response
+ case response = read_response
+ when "timeout"
+ raise Capybara::TimeoutError
+ else
+ raise WebkitInvalidResponseError, response
+ end
end
result
@@ -1,7 +1,7 @@
module Capybara
module Driver
class Webkit
- VERSION = '0.11.0'.freeze
+ VERSION = '0.11.0.1'.freeze
end
end
end
View
@@ -2,12 +2,15 @@
require 'self_signed_ssl_cert'
require 'stringio'
require 'capybara/driver/webkit/browser'
+require 'capybara/driver/webkit/socket_debugger'
require 'socket'
require 'base64'
describe Capybara::Driver::Webkit::Browser do
+ let(:blacklist_file) { File.join(File.dirname(__FILE__), 'support/test_blacklist.txt') }
let(:browser) { Capybara::Driver::Webkit::Browser.new }
+ let(:browser_with_blacklist) { Capybara::Driver::Webkit::Browser.new(blacklist_file: blacklist_file)}
let(:browser_ignore_ssl_err) {
Capybara::Driver::Webkit::Browser.new(:ignore_ssl_errors => true)
}
@@ -155,6 +158,178 @@
end
end
+ context "uses the url blacklist" do
+ before(:each) do
+ # set up minimal HTTP server
+ @host = "127.0.0.1"
+ @server = TCPServer.new(@host, 0)
+ @port = @server.addr[1]
+
+ @server_thread = Thread.new(@server) do |serv|
+ while conn = serv.accept do
+ # read request
+ request = []
+ until (line = conn.readline.strip).empty?
+ request << line
+ end
+
+ request = request.join("\n")
+ content = if request =~ %r{GET /frame}
+ "<p>Inner</p>"
+ else
+ <<-HTML
+ <iframe src="http://example.com/path" id="frame1"></iframe>
+ <iframe src="http://example.org/path/to/file" id="frame2"></iframe>
+ <iframe src="/frame" id="frame3"></iframe>
+ HTML
+ end
+
+ html = <<-HTML
+ <html>
+ <head>
+ </head>
+ <body>
+ #{content}
+ </body>
+ </html>
+ HTML
+ conn.write "HTTP/1.1 200 OK\r\n"
+ conn.write "Content-Type: text/html\r\n"
+ conn.write "Content-Length: %i\r\n" % html.size
+ conn.write "\r\n"
+ conn.write html
+ conn.write("\r\n\r\n")
+ conn.close
+ end
+ end
+ end
+
+ after(:each) do
+ @server_thread.kill
+ @server.close
+ end
+
+ it "should not fetch urls blocked by host" do
+ browser_with_blacklist.visit("http://#{@host}:#{@port}")
+ browser_with_blacklist.frame_focus('frame1')
+ browser_with_blacklist.find('//body').should be_empty
+ end
+
+ it "should not fetch urls blocked by full paths" do
+ browser_with_blacklist.visit("http://#{@host}:#{@port}")
+ browser_with_blacklist.frame_focus('frame2')
+ browser_with_blacklist.find('//body').should be_empty
+ end
+
+ it "should not block non-blacklisted urls" do
+ browser_with_blacklist.visit("http://#{@host}:#{@port}")
+ browser_with_blacklist.frame_focus('frame3')
+ browser_with_blacklist.find('//p').should_not be_empty
+ end
+ end
+
+ context "timeout on long requests" do
+ before(:each) do
+ # set up minimal HTTPS server
+ @host = "127.0.0.1"
+ @server = TCPServer.new(@host, 0)
+ @port = @server.addr[1]
+
+ # set up SSL layer
+
+ @server_thread = Thread.new(@server) do |serv|
+ while conn = serv.accept do
+ begin
+ # read request
+ request = []
+ until (line = conn.readline.strip).empty?
+ request << line
+ end
+
+ request = request.join("\n")
+
+ if request =~ %r{POST /form}
+ sleep(4)
+ else
+ sleep(2)
+ end
+
+ # write response
+ html = <<-HTML
+ <html>
+ <body>
+ <form action="/form" method="post">
+ <input type="submit" value="Submit"/>
+ </form>
+ </body>
+ </html>
+ HTML
+ conn.write "HTTP/1.1 200 OK\r\n"
+ conn.write "Content-Type:text/html\r\n"
+ conn.write "Content-Length: %i\r\n" % html.size
+ conn.write "\r\n"
+ conn.write html
+ conn.write("\r\n\r\n")
+ conn.close
+ rescue
+ conn.close
+ end
+ end
+ end
+ end
+
+ after(:each) do
+ @server_thread.kill
+ @server.close
+ end
+
+ it "should not raise a timeout error when zero" do
+ browser.set_timeout(0)
+ lambda { browser.visit("http://#{@host}:#{@port}/") }.should_not raise_error(Capybara::TimeoutError)
+ end
+
+ it "should raise a timeout error" do
+ browser.set_timeout(1)
+ lambda { browser.visit("http://#{@host}:#{@port}/") }.should raise_error(Capybara::TimeoutError)
+ end
+
+ it "should not raise an error when the timeout is high enough" do
+ browser.set_timeout(10)
+ lambda { browser.visit("http://#{@host}:#{@port}/") }.should_not raise_error(Capybara::TimeoutError)
+ end
+
+ it "should set the timeout for each request" do
+ browser.set_timeout(10)
+ lambda { browser.visit("http://#{@host}:#{@port}/") }.should_not raise_error(Capybara::TimeoutError)
+ browser.set_timeout(1)
+ lambda { browser.visit("http://#{@host}:#{@port}/") }.should raise_error(Capybara::TimeoutError)
+ end
+
+ it "should set the timeout for each request" do
+ browser.set_timeout(1)
+ lambda { browser.visit("http://#{@host}:#{@port}/") }.should raise_error(Capybara::TimeoutError)
+ browser.reset!
+ browser.set_timeout(10)
+ lambda { browser.visit("http://#{@host}:#{@port}/") }.should_not raise_error(Capybara::TimeoutError)
+ end
+
+ it "should raise a timeout on a slow form" do
+ browser.set_timeout(3)
+ browser.visit("http://#{@host}:#{@port}/")
+ browser.status_code.should == 200
+ browser.set_timeout(1)
+ browser.command("Node", "click", browser.find("//input").first)
+ lambda { browser.status_code }.should raise_error(Capybara::TimeoutError)
+ end
+
+ it "get timeout" do
+ browser.set_timeout(10)
+ browser.get_timeout.should == 10
+ browser.set_timeout(3)
+ browser.get_timeout.should == 3
+ end
+ end
+
describe "forking", :skip_on_windows => true do
it "only shuts down the server from the main process" do
browser.reset!
View
@@ -825,11 +825,25 @@
end
end
+ context "empty document is ok" do
+ before(:all) do
+ @app = lambda do |env|
+ [200,
+ { 'Content-Type' => 'text/html', 'Content-Length' => "0" },
+ [""]]
+ end
+ end
+
+ it "should have the right status code" do
+ subject.status_code.should == 200
+ end
+ end
+
context "error app" do
before(:all) do
@app = lambda do |env|
if env['PATH_INFO'] == "/error"
- [404, {}, []]
+ [404, {'Content-Type' => 'text/html'}, []]
else
body = <<-HTML
<html><body>
@@ -863,7 +877,7 @@ def wait_for_error_to_complete
if env['PATH_INFO'] == "/error"
body = "error"
sleep(1)
- [304, {}, []]
+ [404, {'Content-Type' => 'text/html'}, []]
else
body = <<-HTML
<html><body>
@@ -0,0 +1,2 @@
+http://example.org/path/to/file
+http://example.com
@@ -22,6 +22,8 @@
#include "ConsoleMessages.h"
#include "RequestedUrl.h"
#include "CurrentUrl.h"
+#include "SetTimeout.h"
+#include "GetTimeout.h"
CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
m_page = page;
View
@@ -6,6 +6,8 @@
#include "Command.h"
#include <QTcpSocket>
+#include <QTimer>
+#include <iostream>
Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
QObject(parent) {
@@ -15,8 +17,15 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) :
m_commandParser = new CommandParser(socket, m_commandFactory, this);
m_pageSuccess = true;
m_commandWaiting = false;
+ m_commandTimedOut = false;
+
+ m_timer = new QTimer(this);
+ m_timer->setSingleShot(true);
+ connect(m_timer, SIGNAL(timeout()), this, SLOT(pageLoadTimeout()));
+
connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext()));
connect(m_commandParser, SIGNAL(commandReady(Command *)), this, SLOT(commandReady(Command *)));
+ connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadStarted()));
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}
@@ -34,29 +43,53 @@ void Connection::startCommand() {
m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_page, this);
connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
m_runningCommand->start();
+ } else if (m_commandTimedOut) {
+ writeCommandTimeout();
} else {
writePageLoadFailure();
}
}
+void Connection::pageLoadStarted() {
+ int timeout = m_page->getTimeout();
+ if (timeout > 0) {
+ m_timer->start(timeout * 1000);
+ }
+}
+
void Connection::pendingLoadFinished(bool success) {
+ m_timer->stop();
m_pageSuccess = success;
if (m_commandWaiting)
startCommand();
+ else if (m_commandTimedOut)
+ writeCommandTimeout();
}
-void Connection::writePageLoadFailure() {
- m_pageSuccess = true;
- QString message = m_page->failureString();
- writeResponse(new Response(false, message));
+void Connection::pageLoadTimeout() {
+ m_commandTimedOut = true;
+
+ if (m_runningCommand) {
+ disconnect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
+ m_runningCommand->deleteLater();
+ m_runningCommand = NULL;
+ }
+
+ m_page->triggerAction(QWebPage::Stop);
}
void Connection::finishCommand(Response *response) {
- m_runningCommand->deleteLater();
+ if (m_runningCommand) {
+ m_runningCommand->deleteLater();
+ m_runningCommand = NULL;
+ }
writeResponse(response);
}
void Connection::writeResponse(Response *response) {
+ m_pageSuccess = true;
+ m_commandTimedOut = false;
+
if (response->isSuccess())
m_socket->write("ok\n");
else
@@ -69,3 +102,11 @@ void Connection::writeResponse(Response *response) {
delete response;
}
+void Connection::writePageLoadFailure() {
+ QString message = m_page->failureString();
+ writeResponse(new Response(false, message));
+}
+
+void Connection::writeCommandTimeout() {
+ writeResponse(new Response(false, "timeout"));
+}
Oops, something went wrong.

0 comments on commit 1894195

Please sign in to comment.