forked from thoughtbot/capybara-webkit
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds syntax sugar for reading cookie values (fix thoughtbot#179)
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 "#[]" do | ||
it "should return the right cookie value for every given domain/path" do | ||
subject["cookie1", "example.org"].should == "1" | ||
subject["cookie1", "www.facebook.com"].should == "3" | ||
subject["cookie2", "sub1.example.org"].should == "4" | ||
end | ||
|
||
it "should not return cookie from other domain" do | ||
subject["cookie2", "www.example.org"].should == nil | ||
end | ||
|
||
it "should handle path precedence correctly" do | ||
subject["cookie1", "www.example.org"].should == "1" | ||
subject["cookie1", "www.example.org", "/dir1/123"].should == "2" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters