Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
morenocarullo committed Oct 26, 2011
2 parents 4be10dc + 8d2251d commit 8d113f8
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/capybara/driver/webkit.rb
Expand Up @@ -2,6 +2,7 @@
require "capybara/driver/webkit/node"
require "capybara/driver/webkit/browser"
require "capybara/driver/webkit/socket_debugger"
require "capybara/driver/webkit/cookie_jar"

class Capybara::Driver::Webkit
class WebkitInvalidResponseError < StandardError
Expand Down Expand Up @@ -104,6 +105,10 @@ def server_port
@rack_server.port
end

def cookies
@cookie_jar ||= CookieJar.new(browser)
end

private

def url(path)
Expand Down
55 changes: 55 additions & 0 deletions lib/capybara/driver/webkit/cookie_jar.rb
@@ -0,0 +1,55 @@
require 'webrick'

# A simple cookie jar implementation.
# Does not take special cookie attributes
# into account like expire, max-age, httponly, secure
class Capybara::Driver::Webkit::CookieJar
attr_reader :browser

def initialize(browser)
@browser = browser
end

def [](*args)
cookie = find(*args)
cookie && cookie.value
end

def find(name, domain = nil, path = "/")
# we are sorting by path size because more specific paths take
# precendence
cookies.sort_by { |c| -c.path.size }.find { |c|
c.name.downcase == name.downcase &&
(!domain || valid_domain?(c, domain)) &&
(!path || valid_path?(c, path))
}
end

protected

def valid_domain?(cookie, domain)
ends_with?(("." + domain).downcase,
normalize_domain(cookie.domain).downcase)
end

def normalize_domain(domain)
domain = "." + domain unless domain[0,1] == "."
domain
end

def valid_path?(cookie, path)
starts_with?(path, cookie.path)
end

def ends_with?(str, suffix)
str[-suffix.size..-1] == suffix
end

def starts_with?(str, prefix)
str[0, prefix.size] == prefix
end

def cookies
browser.get_cookies.map { |c| WEBrick::Cookie.parse_set_cookie(c) }
end
end
48 changes: 48 additions & 0 deletions spec/cookie_jar_spec.rb
@@ -0,0 +1,48 @@
require 'spec_helper'
require 'capybara/driver/webkit/cookie_jar'

describe Capybara::Driver::Webkit::CookieJar do
let(:browser) {
browser = double("Browser")
browser.stub(:get_cookies) { [
"cookie1=1; domain=.example.org; path=/",
"cookie1=2; domain=.example.org; path=/dir1/",
"cookie1=3; domain=.facebook.com; path=/",
"cookie2=4; domain=.sub1.example.org; path=/",
] }
browser
}

subject { Capybara::Driver::Webkit::CookieJar.new(browser) }

describe "#find" do
it "returns a cookie object" do
subject.find("cookie1", "www.facebook.com").domain.should == ".facebook.com"
end

it "returns the right cookie for every given domain/path" do
subject.find("cookie1", "example.org").value.should == "1"
subject.find("cookie1", "www.facebook.com").value.should == "3"
subject.find("cookie2", "sub1.example.org").value.should == "4"
end

it "does not return a cookie from other domain" do
subject.find("cookie2", "www.example.org").should == nil
end

it "respects path precedence rules" do
subject.find("cookie1", "www.example.org").value.should == "1"
subject.find("cookie1", "www.example.org", "/dir1/123").value.should == "2"
end
end

describe "#[]" do
it "returns the first matching cookie's value" do
subject["cookie1", "example.org"].should == "1"
end

it "returns nil if no cookie is found" do
subject["notexisting"].should == nil
end
end
end
36 changes: 33 additions & 3 deletions spec/driver_spec.rb
Expand Up @@ -20,7 +20,7 @@
p_id = "greeting"
msg = "hello"
iframe = "<iframe id=\"f\" src=\"/?iframe=true\"></iframe>"
end
end
body = <<-HTML
<html>
<head>
Expand Down Expand Up @@ -842,7 +842,7 @@ def wait_for_error_to_complete
[body]]
end
end

it "raises a webkit error for the requested url" do
make_the_server_go_away
expect {
Expand All @@ -857,7 +857,7 @@ def make_the_server_come_back
subject.browser.instance_variable_get(:@socket).unstub!(:puts)
subject.browser.instance_variable_get(:@socket).unstub!(:print)
end

def make_the_server_go_away
subject.browser.instance_variable_get(:@socket).stub!(:gets).and_return(nil)
subject.browser.instance_variable_get(:@socket).stub!(:puts)
Expand Down Expand Up @@ -947,6 +947,10 @@ def echoed_cookie
cookie["domain"].should include "127.0.0.1"
cookie["path"].should == "/"
end

it "allows reading access to cookies using a nice syntax" do
subject.cookies["cookie"].should == "abc"
end
end

context "with socket debugger" do
Expand Down Expand Up @@ -1018,6 +1022,32 @@ def set_automatic_reload(value)
end
end

context "css overflow app" do
before(:all) do
@app = lambda do |env|
body = <<-HTML
<html>
<head>
<style type="text/css">
#overflow { overflow: hidden }
</style>
</head>
<body>
<div id="overflow">Overflow</div>
</body>
</html>
HTML
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end

it "handles overflow hidden" do
subject.find("//div[@id='overflow']").first.text.should == "Overflow"
end
end

context "javascript redirect app" do
before(:all) do
@app = lambda do |env|
Expand Down
1 change: 1 addition & 0 deletions src/WebPage.cpp
Expand Up @@ -20,6 +20,7 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
this, SLOT(frameCreated(QWebFrame *)));
connect(this, SIGNAL(unsupportedContent(QNetworkReply*)),
this, SLOT(handleUnsupportedContent(QNetworkReply*)));
this->setViewportSize(QSize(1680, 1050));
}

void WebPage::setCustomNetworkAccessManager() {
Expand Down

0 comments on commit 8d113f8

Please sign in to comment.