在尝试让 CGI::Cookie.parse 做正确的事情失败后,我最终滚动了自己的解析器。这很简单:
def parse_set_cookie_header(header)
kv_pairs = header.split(/\s*;\s*/).map do |attr|
k, v = attr.split '='
[ k, v || nil ]
end
Hash[ kv_pairs ]
end
这是它产生的结果的示例:
饼干制作:
IN: "signup=VALUE_HERE; path=/subscriptions; secure; HttpOnly"
OUT: {"signup"=>"VALUE_HERE", "path"=>"/subscriptions", "secure"=>nil, "HttpOnly"=>nil}
Cookie 删除:
IN: "signup=; path=/subscriptions; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000; secure; HttpOnly"
OUT: {"signup"=>nil, "path"=>"/subscriptions", "max-age"=>"0", "expires"=>"Thu, 01 Jan 1970 00:00:00 -0000", "secure"=>nil, "HttpOnly"=>nil}
这是一个与之配套的示例规范:
describe 'the Set-Cookie header' do
let(:value) { 'hello world' }
let(:signup_cookie) do
parse_set_cookie_header response.header['Set-Cookie']
end
before do
get :index, :spec => 'set_signup_cookie'
end
it 'has a payload set for :signup' do
expect(signup_cookie['signup']).to be_present
end
it 'has the right path' do
expect(signup_cookie['path']).to eq '/subscriptions'
end
it 'has the secure flag set' do
expect(signup_cookie).to have_key 'secure'
end
it 'has the HttpOnly flag set' do
expect(signup_cookie).to have_key 'HttpOnly'
end
it 'is a session cookie (i.e. it has no :expires)' do
expect(signup_cookie).not_to have_key 'expires'
end
it 'has no max-age' do
expect(signup_cookie).not_to have_key 'max-age'
end
end