mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-03-25 08:55:53 +00:00
Add tests for MozillaStore#upgrade_database and fix its bugs.
This commit is contained in:
parent
25dd32219f
commit
e9230a00c1
2 changed files with 54 additions and 8 deletions
|
|
@ -141,9 +141,9 @@ class HTTP::CookieJar
|
|||
st_update = @db.prepare("UPDATE moz_cookies SET baseDomain = :baseDomain WHERE id = :id")
|
||||
|
||||
@db.execute("SELECT id, host FROM moz_cookies") { |row|
|
||||
domain_name = DomainName.new(row[:host])
|
||||
domain_name = DomainName.new(row['host'][/\A\.?(.*)/, 1])
|
||||
domain = domain_name.domain || domain_name.hostname
|
||||
st_update.execute(:baseDomain => domain, :id => row[:id])
|
||||
st_update.execute(:baseDomain => domain, :id => row['id'])
|
||||
}
|
||||
|
||||
@db.execute("CREATE INDEX moz_basedomain ON moz_cookies (baseDomain)")
|
||||
|
|
@ -151,12 +151,12 @@ class HTTP::CookieJar
|
|||
when 3
|
||||
st_delete = @db.prepare("DELETE FROM moz_cookies WHERE id = :id")
|
||||
|
||||
prev_row = nil
|
||||
prev_row = nil
|
||||
@db.execute(<<-'SQL') { |row|
|
||||
SELECT id, name, host, path FROM moz_cookies
|
||||
ORDER BY name ASC, host ASC, path ASC, expiry ASC
|
||||
SQL
|
||||
if %w[name host path].all? { |col| row[col] == prev_row[col] }
|
||||
if %w[name host path].all? { |col| prev_row and row[col] == prev_row[col] }
|
||||
st_delete.execute(prev_row['id'])
|
||||
end
|
||||
prev_row = row
|
||||
|
|
@ -289,8 +289,8 @@ class HTTP::CookieJar
|
|||
attrs[:domain] = row['host']
|
||||
attrs[:path] = row['path']
|
||||
attrs[:expires_at] = Time.at(row['expiry'])
|
||||
attrs[:accessed_at] = Time.at(row['lastAccessed'])
|
||||
attrs[:created_at] = Time.at(row['creationTime'])
|
||||
attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0)
|
||||
attrs[:created_at] = Time.at(row['creationTime'] || 0)
|
||||
attrs[:secure] = secure
|
||||
attrs[:httponly] = row['isHttpOnly'] != 0
|
||||
})
|
||||
|
|
@ -325,8 +325,8 @@ class HTTP::CookieJar
|
|||
attrs[:domain] = row['host']
|
||||
attrs[:path] = row['path']
|
||||
attrs[:expires_at] = Time.at(row['expiry'])
|
||||
attrs[:accessed_at] = Time.at(row['lastAccessed'])
|
||||
attrs[:created_at] = Time.at(row['creationTime'])
|
||||
attrs[:accessed_at] = Time.at(row['lastAccessed'] || 0)
|
||||
attrs[:created_at] = Time.at(row['creationTime'] || 0)
|
||||
attrs[:secure] = row['isSecure'] != 0
|
||||
attrs[:httponly] = row['isHttpOnly'] != 0
|
||||
})
|
||||
|
|
|
|||
|
|
@ -781,6 +781,52 @@ module TestHTTPCookieJar
|
|||
{ :store => :mozilla, :filename => ":memory:" },
|
||||
{ :store => :mozilla, :filename => ":memory:" })
|
||||
end
|
||||
|
||||
def test_upgrade_mozillastore
|
||||
Dir.mktmpdir { |dir|
|
||||
filename = File.join(dir, 'cookies.sqlite')
|
||||
|
||||
sqlite = SQLite3::Database.new(filename)
|
||||
sqlite.execute(<<-'SQL')
|
||||
CREATE TABLE moz_cookies (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT,
|
||||
value TEXT,
|
||||
host TEXT,
|
||||
path TEXT,
|
||||
expiry INTEGER,
|
||||
isSecure INTEGER,
|
||||
isHttpOnly INTEGER)
|
||||
SQL
|
||||
sqlite.execute(<<-'SQL')
|
||||
PRAGMA user_version = 1
|
||||
SQL
|
||||
|
||||
begin
|
||||
st_insert = sqlite.prepare(<<-'SQL')
|
||||
INSERT INTO moz_cookies (
|
||||
id, name, value, host, path, expiry, isSecure, isHttpOnly
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
SQL
|
||||
|
||||
st_insert.execute(1, 'name1', 'value1', '.example.co.jp', '/', 2312085765, 0, 0)
|
||||
st_insert.execute(2, 'name1', 'value2', '.example.co.jp', '/', 2312085765, 0, 0)
|
||||
st_insert.execute(3, 'name1', 'value3', 'www.example.co.jp', '/', 2312085765, 0, 0)
|
||||
ensure
|
||||
st_insert.close if st_insert
|
||||
end
|
||||
|
||||
sqlite.close
|
||||
jar = HTTP::CookieJar.new(:store => :mozilla, :filename => filename)
|
||||
|
||||
assert_equal 2, jar.to_a.size
|
||||
assert_equal 2, jar.cookies('http://www.example.co.jp/').size
|
||||
|
||||
cookie, *rest = jar.cookies('http://host.example.co.jp/')
|
||||
assert_send [rest, :empty?]
|
||||
assert_equal 'value2', cookie.value
|
||||
}
|
||||
end
|
||||
end if begin
|
||||
require 'sqlite3'
|
||||
true
|
||||
|
|
|
|||
Loading…
Reference in a new issue