cookiemonster 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +5 -0
- data/Rakefile +10 -0
- data/cookiemonster.gemspec +25 -0
- data/lib/cookiemonster.rb +70 -0
- data/lib/cookiemonster/version.rb +3 -0
- data/spec/lib/raw_parse_spec.rb +88 -0
- data/spec/spec_helper.rb +4 -0
- metadata +67 -0
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "cookiemonster/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "cookiemonster"
|
7
|
+
s.version = CookieMonster::VERSION
|
8
|
+
s.authors = ["Matt Southerden"]
|
9
|
+
s.email = ["matt@localbubble.com"]
|
10
|
+
s.homepage = "http://github.com/mattsoutherden/cookiemonster"
|
11
|
+
s.summary = %q{Let CookieMonster munch your cookies}
|
12
|
+
s.description = %q{A library for working with Set-Cookie data in Ruby}
|
13
|
+
|
14
|
+
s.rubyforge_project = "cookiemonster"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "minitest"
|
23
|
+
|
24
|
+
# s.add_runtime_dependency "rest-client"
|
25
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "cookiemonster/version"
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
module CookieMonster
|
5
|
+
|
6
|
+
def self.parse(set_cookie_string)
|
7
|
+
Cookie.new self.parse_hash(set_cookie_string)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.parse_hash(set_cookie_string)
|
11
|
+
hash = set_cookie_string.split(/; ?/).inject({}) do |result, field|
|
12
|
+
field_name, field_value = field.split('=', 2)
|
13
|
+
result[field_name] = field_value
|
14
|
+
result
|
15
|
+
end
|
16
|
+
|
17
|
+
name, value = hash.shift
|
18
|
+
|
19
|
+
hash[:name] = name
|
20
|
+
hash[:value] = value
|
21
|
+
|
22
|
+
hash = hash.inject({}) do |options, (key, value)|
|
23
|
+
options[(key.downcase.to_sym rescue key) || key] = value
|
24
|
+
options
|
25
|
+
end
|
26
|
+
|
27
|
+
hash
|
28
|
+
end
|
29
|
+
|
30
|
+
class Cookie
|
31
|
+
|
32
|
+
attr_reader :name
|
33
|
+
attr_reader :value
|
34
|
+
attr_reader :path
|
35
|
+
attr_reader :domain
|
36
|
+
attr_reader :expires
|
37
|
+
attr_reader :http_only
|
38
|
+
attr_reader :secure
|
39
|
+
attr_reader :session
|
40
|
+
|
41
|
+
alias :http_only? :http_only
|
42
|
+
alias :secure? :secure
|
43
|
+
alias :session? :session
|
44
|
+
|
45
|
+
def initialize(args)
|
46
|
+
@name = args[:name]
|
47
|
+
@value = args[:value]
|
48
|
+
@path = args[:path]
|
49
|
+
@domain = args[:domain]
|
50
|
+
@expires = args[:expires] ? parse_expiry(args[:expires]) : nil
|
51
|
+
@http_only = args.has_key?(:httponly) ? true : false
|
52
|
+
@secure = args.has_key?(:secure) ? true : false
|
53
|
+
@session = expires.nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
def expired?
|
57
|
+
return false unless expires
|
58
|
+
Time.now > expires
|
59
|
+
end
|
60
|
+
|
61
|
+
protected
|
62
|
+
|
63
|
+
def parse_expiry(str)
|
64
|
+
return Time.parse(str)
|
65
|
+
rescue
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe 'Parsing Set-Cookie' do
|
4
|
+
|
5
|
+
describe 'basic parsing' do
|
6
|
+
|
7
|
+
def parse(set_cookie)
|
8
|
+
CookieMonster.parse(set_cookie)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "extracts cookie name" do
|
12
|
+
cookie = parse('foo=bar')
|
13
|
+
cookie.name.must_equal 'foo'
|
14
|
+
|
15
|
+
cookie = parse('baz=quux')
|
16
|
+
cookie.name.must_equal 'baz'
|
17
|
+
end
|
18
|
+
|
19
|
+
it "extracts cookie value" do
|
20
|
+
cookie = parse('foo=bar')
|
21
|
+
cookie.value.must_equal 'bar'
|
22
|
+
|
23
|
+
cookie = parse('baz=quux')
|
24
|
+
cookie.value.must_equal 'quux'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "extracts path" do
|
28
|
+
cookie = parse('foo=bar; path=/')
|
29
|
+
cookie.path.must_equal '/'
|
30
|
+
|
31
|
+
cookie = parse('foo=bar; Path=/foo')
|
32
|
+
cookie.path.must_equal '/foo'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "extracts expiry time" do
|
36
|
+
cookie = parse('foo=bar; expires=Thu, 19-Dec-2013 17:24:17 GMT')
|
37
|
+
cookie.expires.must_equal Time.utc(2013, 12, 19, 17, 24, 17)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be expired when expires in past" do
|
41
|
+
yesterday = (Date.today - 1).httpdate
|
42
|
+
cookie = parse("foo=bar; expires=#{yesterday}")
|
43
|
+
assert cookie.expired?
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should not be expired when expires in future" do
|
47
|
+
tomorrow = (Date.today + 1).httpdate
|
48
|
+
cookie = parse("foo=bar; expires=#{tomorrow}")
|
49
|
+
assert ! cookie.expired?
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should not be expired when expires not set" do
|
53
|
+
cookie = parse('foo=bar')
|
54
|
+
assert ! cookie.expired?
|
55
|
+
end
|
56
|
+
|
57
|
+
it "sets httponly" do
|
58
|
+
cookie = parse('foo=bar; httponly')
|
59
|
+
assert cookie.http_only?
|
60
|
+
end
|
61
|
+
|
62
|
+
it "sets secure" do
|
63
|
+
cookie = parse('foo=bar; secure')
|
64
|
+
assert cookie.secure?
|
65
|
+
end
|
66
|
+
|
67
|
+
it "sets domain" do
|
68
|
+
cookie = parse('foo=bar; domain=foo.com')
|
69
|
+
cookie.domain.must_equal 'foo.com'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "creates a session cookie when no expires is set" do
|
73
|
+
cookie = parse('foo=bar')
|
74
|
+
assert cookie.session?
|
75
|
+
end
|
76
|
+
|
77
|
+
it "creates a session cookie when expires parsing fails" do
|
78
|
+
cookie = parse('foo=bar; expires=Not a date format GMT')
|
79
|
+
assert cookie.session?
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'multiple cookies' do
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cookiemonster
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matt Southerden
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: &2152310760 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2152310760
|
25
|
+
description: A library for working with Set-Cookie data in Ruby
|
26
|
+
email:
|
27
|
+
- matt@localbubble.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- cookiemonster.gemspec
|
37
|
+
- lib/cookiemonster.rb
|
38
|
+
- lib/cookiemonster/version.rb
|
39
|
+
- spec/lib/raw_parse_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
homepage: http://github.com/mattsoutherden/cookiemonster
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project: cookiemonster
|
61
|
+
rubygems_version: 1.8.10
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Let CookieMonster munch your cookies
|
65
|
+
test_files:
|
66
|
+
- spec/lib/raw_parse_spec.rb
|
67
|
+
- spec/spec_helper.rb
|