Skip to content

Commit

Permalink
sinatra-contrib tests now pass
Browse files Browse the repository at this point in the history
Multiple response header values are encoded using an Array instead of
newlines: https://github.com/rack/rack/blob/v3.0.3/UPGRADE-GUIDE.md#multiple-response-header-values-are-encoded-using-an-array

Rack 3 does not remove cookies from the internal storage (because it
doesn't make much sense), see rack/rack#1844, rack/rack#1840
  • Loading branch information
dentarg committed Jan 2, 2024
1 parent 5019214 commit 5db7822
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
10 changes: 5 additions & 5 deletions sinatra-contrib/lib/sinatra/cookies.rb
Expand Up @@ -60,7 +60,7 @@ class Jar
attr_reader :options

def initialize(app)
@response_string = nil
@response_array = nil
@response_hash = {}
@response = app.response
@request = app.request
Expand Down Expand Up @@ -309,12 +309,12 @@ def response_cookies
end

def parse_response
string = @response['Set-Cookie']
return if @response_string == string
cookies_from_response = Array(@response['Set-Cookie'])
return if @response_array == cookies_from_response

hash = {}

string.each_line do |line|
cookies_from_response.each do |line|
key, value = line.split(';', 2).first.to_s.split('=', 2)
next if key.nil?

Expand All @@ -328,7 +328,7 @@ def parse_response
end

@response_hash.replace hash
@response_string = string
@response_array = cookies_from_response
end

def request_cookies
Expand Down
25 changes: 14 additions & 11 deletions sinatra-contrib/spec/cookies_spec.rb
Expand Up @@ -153,12 +153,12 @@ def cookies(*set_cookies)
expect(jar['foo']).to be_nil
end

it 'removes response cookies from cookies hash' do
it 'does not remove response cookies from cookies hash' do
expect(cookie_route do
cookies['foo'] = 'bar'
cookies.clear
cookies['foo']
end).to be_nil
end).to eq('bar')
end

it 'expires existing cookies' do
Expand Down Expand Up @@ -189,12 +189,12 @@ def cookies(*set_cookies)
expect(jar['foo']).to be_nil
end

it 'removes response cookies from cookies hash' do
it 'does not remove response cookies from cookies hash' do
expect(cookie_route do
cookies['foo'] = 'bar'
cookies.delete 'foo'
cookies['foo']
end).to be_nil
end).to eq('bar')
end

it 'expires existing cookies' do
Expand Down Expand Up @@ -246,13 +246,16 @@ def cookies(*set_cookies)
end

describe :delete_if do
it 'deletes cookies that match the block' do
it 'expires cookies that match the block' do
expect(cookie_route('foo=bar') do
cookies['bar'] = 'baz'
cookies['baz'] = 'foo'
cookies.delete_if { |*a| a.include? 'bar' }
cookies.values_at 'foo', 'bar', 'baz'
end).to eq([nil, nil, 'foo'])
response['Set-Cookie']
end).to eq(["bar=baz; domain=example.org; path=/; httponly",
"baz=foo; domain=example.org; path=/; httponly",
"foo=; domain=example.org; path=/; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly",
"bar=; domain=example.org; path=/; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT; httponly"])
end
end

Expand Down Expand Up @@ -454,12 +457,12 @@ def cookies(*set_cookies)
end).to be false
end

it 'becomes true if response cookies are removed' do
it 'deos not become true if response cookies are removed' do
expect(cookie_route do
cookies['foo'] = 'bar'
cookies.delete :foo
cookies.empty?
end).to be true
end).to be false
end

it 'becomes true if request cookies are removed' do
Expand All @@ -469,12 +472,12 @@ def cookies(*set_cookies)
end).to be_truthy
end

it 'becomes true after clear' do
it 'does not become true after clear' do
expect(cookie_route('foo=bar', 'bar=baz') do
cookies['foo'] = 'bar'
cookies.clear
cookies.empty?
end).to be_truthy
end).to be false
end
end

Expand Down
2 changes: 1 addition & 1 deletion sinatra-contrib/spec/link_header_spec.rb
Expand Up @@ -30,7 +30,7 @@
describe :link do
it "sets link headers" do
get '/'
expect(headers['Link'].lines).to include('<booyah>; rel="something"')
expect(headers['Link']).to include('<booyah>; rel="something"')
end

it "returns link html tags" do
Expand Down

0 comments on commit 5db7822

Please sign in to comment.