Class: Rack::Test::Cookie

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/test/cookie_jar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, uri = nil, default_host = DEFAULT_HOST) ⇒ Cookie

:api: private



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rack/test/cookie_jar.rb', line 12

def initialize(raw, uri = nil, default_host = DEFAULT_HOST)
  @default_host = default_host
  uri ||= default_uri

  # separate the name / value pair from the cookie options
  @name_value_raw, options = raw.split(/[;,] */n, 2)

  @name, @value = parse_query(@name_value_raw, ';').to_a.first
  @options = parse_query(options, ';')

  @options["domain"]  ||= (uri.host || default_host)
  @options["path"]    ||= uri.path.sub(/\/[^\/]*\Z/, "")
end

Instance Attribute Details

#nameObject (readonly)

:api: private



9
10
11
# File 'lib/rack/test/cookie_jar.rb', line 9

def name
  @name
end

#valueObject (readonly)

:api: private



9
10
11
# File 'lib/rack/test/cookie_jar.rb', line 9

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object

:api: private



83
84
85
86
# File 'lib/rack/test/cookie_jar.rb', line 83

def <=>(other)
  # Orders the cookies from least specific to most
  [name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse]
end

#domainObject

:api: private



41
42
43
# File 'lib/rack/test/cookie_jar.rb', line 41

def domain
  @options["domain"]
end

#empty?Boolean

:api: private

Returns:

  • (Boolean)


36
37
38
# File 'lib/rack/test/cookie_jar.rb', line 36

def empty?
  @value.nil? || @value.empty?
end

#expired?Boolean

:api: private

Returns:

  • (Boolean)


60
61
62
# File 'lib/rack/test/cookie_jar.rb', line 60

def expired?
  expires && expires < Time.now
end

#expiresObject

:api: private



55
56
57
# File 'lib/rack/test/cookie_jar.rb', line 55

def expires
  Time.parse(@options["expires"]) if @options["expires"]
end

#matches?(uri) ⇒ Boolean

:api: private

Returns:

  • (Boolean)


78
79
80
# File 'lib/rack/test/cookie_jar.rb', line 78

def matches?(uri)
  ! expired? && valid?(uri)
end

#pathObject

:api: private



50
51
52
# File 'lib/rack/test/cookie_jar.rb', line 50

def path
  @options["path"].strip || "/"
end

#rawObject

:api: private



31
32
33
# File 'lib/rack/test/cookie_jar.rb', line 31

def raw
  @name_value_raw
end

#replaces?(other) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rack/test/cookie_jar.rb', line 26

def replaces?(other)
  [name.downcase, domain, path] == [other.name.downcase, other.domain, other.path]
end

#secure?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/rack/test/cookie_jar.rb', line 45

def secure?
  @options.has_key?("secure")
end

#valid?(uri) ⇒ Boolean

:api: private

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rack/test/cookie_jar.rb', line 65

def valid?(uri)
  uri ||= default_uri

  if uri.host.nil?
    uri.host = @default_host
  end

  (!secure? || (secure? && uri.scheme == "https")) &&
  uri.host =~ Regexp.new("#{Regexp.escape(domain)}$", Regexp::IGNORECASE) &&
  uri.path =~ Regexp.new("^#{Regexp.escape(path)}")
end