Class: Rack::Test::Cookie

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

Overview

Represents individual cookies in the cookie jar. This is considered private API and behavior of this class can change at any time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Cookie.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/test/cookie_jar.rb', line 23

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
  @raw, options = raw.split(/[;,] */n, 2)

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

  if domain = @options['domain']
    @exact_domain_match = false
    domain[0] = '' if domain[0] == '.'
  else
    # If the domain attribute is not present in the cookie,
    # the domain must match exactly.
    @exact_domain_match = true
    @options['domain'] = (uri.host || default_host)
  end

  # Set the path for the cookie to the directory containing
  # the request if it isn't set.
  @options['path'] ||= uri.path.sub(/\/[^\/]*\Z/, '')
end

Instance Attribute Details

#nameObject (readonly)

The name of the cookie, will be a string



14
15
16
# File 'lib/rack/test/cookie_jar.rb', line 14

def name
  @name
end

#rawObject (readonly)

The raw string for the cookie, without options. Will generally be in name=value format is name and value are provided.



21
22
23
# File 'lib/rack/test/cookie_jar.rb', line 21

def raw
  @raw
end

#valueObject (readonly)

The value of the cookie, will be a string or nil if there is no value.



17
18
19
# File 'lib/rack/test/cookie_jar.rb', line 17

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object

Order cookies by name, path, and domain.



107
108
109
# File 'lib/rack/test/cookie_jar.rb', line 107

def <=>(other)
  [name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse]
end

#domainObject

The explicit or implicit domain for the cookie.



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

def domain
  @options['domain']
end

#empty?Boolean

Whether the cookie has a value.

Returns:

  • (Boolean)


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

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

#expired?Boolean

Whether the cookie is currently expired.

Returns:

  • (Boolean)


86
87
88
# File 'lib/rack/test/cookie_jar.rb', line 86

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

#expiresObject

A Time value for when the cookie expires, if the expires option is set.



81
82
83
# File 'lib/rack/test/cookie_jar.rb', line 81

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

#http_only?Boolean

Whether the cookie has the httponly flag, indicating it is not available via a javascript API.

Returns:

  • (Boolean)


71
72
73
# File 'lib/rack/test/cookie_jar.rb', line 71

def http_only?
  @options.key?('HttpOnly') || @options.key?('httponly')
end

#matches?(uri) ⇒ Boolean

Cookies that do not match the URI will not be sent in requests to the URI.

Returns:

  • (Boolean)


102
103
104
# File 'lib/rack/test/cookie_jar.rb', line 102

def matches?(uri)
  !expired? && valid?(uri) && uri.path.start_with?(path)
end

#pathObject

The explicit or implicit path for the cookie.



76
77
78
# File 'lib/rack/test/cookie_jar.rb', line 76

def path
  ([*@options['path']].first.split(',').first || '/').strip
end

#replaces?(other) ⇒ Boolean

Wether the given cookie can replace the current cookie in the cookie jar.

Returns:

  • (Boolean)


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

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

#secure?Boolean

Whether the cookie has the secure flag, indicating it can only be sent over an encrypted connection.

Returns:

  • (Boolean)


65
66
67
# File 'lib/rack/test/cookie_jar.rb', line 65

def secure?
  @options.key?('secure')
end

#to_hObject Also known as: to_hash

A hash of cookie options, including the cookie value, but excluding the cookie name.



112
113
114
115
116
117
118
# File 'lib/rack/test/cookie_jar.rb', line 112

def to_h
  @options.merge(
    'value'    => @value,
    'HttpOnly' => http_only?,
    'secure'   => secure?
  )
end

#valid?(uri) ⇒ Boolean

Whether the cookie is valid for the given URI.

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
# File 'lib/rack/test/cookie_jar.rb', line 91

def valid?(uri)
  uri ||= default_uri

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

  real_domain = domain =~ /^\./ ? domain[1..-1] : domain
  !!((!secure? || (secure? && uri.scheme == 'https')) &&
    uri.host =~ Regexp.new("#{'^' if @exact_domain_match}#{Regexp.escape(real_domain)}$", Regexp::IGNORECASE))
end