Class: Rack::Test::Cookie

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

:api: private


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/test/cookie_jar.rb', line 13

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, ';')

  if @options['domain']
    @exact_domain_match = false
  else
    @exact_domain_match = true
    @options['domain'] = (uri.host || default_host)
  end

  @options['path']    ||= uri.path.sub(/\/[^\/]*\Z/, '')
end

Instance Attribute Details

#nameObject (readonly)

:api: private


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

def name
  @name
end

#valueObject (readonly)

:api: private


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

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object

:api: private


93
94
95
96
# File 'lib/rack/test/cookie_jar.rb', line 93

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


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

def domain
  @options['domain']
end

#empty?Boolean

:api: private

Returns:

  • (Boolean)

43
44
45
# File 'lib/rack/test/cookie_jar.rb', line 43

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

#expired?Boolean

:api: private

Returns:

  • (Boolean)

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

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

#expiresObject

:api: private


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

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

#http_only?Boolean

Returns:

  • (Boolean)

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

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

#matches?(uri) ⇒ Boolean

:api: private

Returns:

  • (Boolean)

88
89
90
# File 'lib/rack/test/cookie_jar.rb', line 88

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

#pathObject

:api: private


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

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

#rawObject

:api: private


38
39
40
# File 'lib/rack/test/cookie_jar.rb', line 38

def raw
  @name_value_raw
end

#replaces?(other) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
# File 'lib/rack/test/cookie_jar.rb', line 33

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

#secure?Boolean

Returns:

  • (Boolean)

52
53
54
# File 'lib/rack/test/cookie_jar.rb', line 52

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

#to_hObject Also known as: to_hash


98
99
100
101
102
103
104
# File 'lib/rack/test/cookie_jar.rb', line 98

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

#valid?(uri) ⇒ Boolean

:api: private

Returns:

  • (Boolean)

76
77
78
79
80
81
82
83
84
85
# File 'lib/rack/test/cookie_jar.rb', line 76

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) &&
    uri.path =~ Regexp.new("^#{Regexp.escape(path)}"))
end