class Rack::Test::Cookie
:nodoc:
API and behavior of this class can change at any time.
Represents individual cookies in the cookie jar. This is considered private
def <=>(other)
def <=>(other) [name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse] end
def default_uri
def default_uri URI.parse('//' + @default_host + '/') end
def domain
def domain @options['domain'] end
def empty?
def empty? @value.nil? || @value.empty? end
def expired?
def expired? expires && expires < Time.now end
def expires
def expires Time.parse(@options['expires']) if @options['expires'] end
def http_only?
Whether the cookie has the httponly flag, indicating it is not available via
def http_only? @options.key?('HttpOnly') || @options.key?('httponly') end
def initialize(raw, uri = nil, default_host = DEFAULT_HOST)
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
def matches?(uri)
def matches?(uri) !expired? && valid?(uri) && uri.path.start_with?(path) end
def path
def path ([*@options['path']].first.split(',').first || '/').strip end
def replaces?(other)
def replaces?(other) [name.downcase, domain, path] == [other.name.downcase, other.domain, other.path] end
def secure?
Whether the cookie has the secure flag, indicating it can only be sent over
def secure? @options.key?('secure') end
def to_h
def to_h @options.merge( 'value' => @value, 'HttpOnly' => http_only?, 'secure' => secure? ) end
def valid?(uri)
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