forked from brutuscat/medusa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
151 additions
and
12 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
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,35 @@ | ||
require 'delegate' | ||
require 'webrick/cookie' | ||
|
||
class WEBrick::Cookie | ||
def expired? | ||
!!expires && expires < Time.now | ||
end | ||
end | ||
|
||
module Anemone | ||
class CookieStore < DelegateClass(Hash) | ||
|
||
def initialize(cookies = nil) | ||
@cookies = {} | ||
cookies.each { |name, value| @cookies[name] = WEBrick::Cookie.new(name, value) } if cookies | ||
super(@cookies) | ||
end | ||
|
||
def merge!(set_cookie_str) | ||
begin | ||
cookie_hash = WEBrick::Cookie.parse_set_cookies(set_cookie_str).inject({}) do |hash, cookie| | ||
hash[cookie.name] = cookie if !!cookie | ||
hash | ||
end | ||
@cookies.merge! cookie_hash | ||
rescue | ||
end | ||
end | ||
|
||
def to_s | ||
@cookies.values.reject { |cookie| cookie.expired? }.map { |cookie| "#{cookie.name}=#{cookie.value}" }.join(';') | ||
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
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
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,27 @@ | ||
require File.dirname(__FILE__) + '/spec_helper' | ||
|
||
module Anemone | ||
describe CookieStore do | ||
|
||
it "should start out empty if no cookies are specified" do | ||
CookieStore.new.empty?.should be true | ||
end | ||
|
||
it "should accept a Hash of cookies in the constructor" do | ||
CookieStore.new({'test' => 'cookie'})['test'].value.should == 'cookie' | ||
end | ||
|
||
it "should be able to merge an HTTP cookie string" do | ||
cs = CookieStore.new({'a' => 'a', 'b' => 'b'}) | ||
cs.merge! "a=A; path=/, c=C; path=/" | ||
cs['a'].value.should == 'A' | ||
cs['b'].value.should == 'b' | ||
cs['c'].value.should == 'C' | ||
end | ||
|
||
it "should have a to_s method to turn the cookies into a string for the HTTP Cookie header" do | ||
CookieStore.new({'a' => 'a', 'b' => 'b'}).to_s.should == 'a=a;b=b' | ||
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
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