Skip to content

Commit

Permalink
Add cookie_helper to manage different expires value.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericproulx committed Dec 29, 2023
1 parent b349c12 commit 1dc9799
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/grape/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def each(&block)
# see https://github.com/rack/rack/blob/main/lib/rack/utils.rb#L338-L340
# rubocop:disable Layout/SpaceBeforeBrackets
def delete(name, **opts)
options = opts.merge(max_age: 0, value: '', expires: Time.at(0))
options = opts.merge(max_age: '0', value: '', expires: Time.at(0))
self.[]=(name, options)
end
# rubocop:enable Layout/SpaceBeforeBrackets
Expand Down
25 changes: 13 additions & 12 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ def app
end

get('/get/cookies')
expect(last_response.headers[Rack::SET_COOKIE]).to contain_exactly(
'my-awesome-cookie1=is+cool',
'my-awesome-cookie2=is+cool+too; domain=my.example.com; path=/; secure',

expect(last_response_cookies).to contain_exactly(
'cookie3=symbol',
'cookie4=secret+code+here'
'cookie4=secret+code+here',
'my-awesome-cookie1=is+cool',
'my-awesome-cookie2=is+cool+too; domain=my.example.com; path=/; secure'
)
end

Expand All @@ -190,7 +191,7 @@ def app

get '/username'
expect(last_response.body).to eq('mrplum')
expect(last_response.cookies).to be_empty
expect(last_response.headers['Set-Cookie']).to be_nil
end

it 'sets and update browser cookies' do
Expand All @@ -202,7 +203,7 @@ def app

get '/username'
expect(last_response.body).to eq('user_test')
expect(last_response.headers[Rack::SET_COOKIE]).to contain_exactly(
expect(last_response_cookies).to contain_exactly(
'sandbox=true',
'username=user_test'
)
Expand All @@ -220,9 +221,9 @@ def app
end
get '/test'
expect(last_response.body).to eq('3')
expect(last_response.headers[Rack::SET_COOKIE]).to contain_exactly(
'and_this=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT',
'delete_this_cookie=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT'
expect(last_response_cookies).to contain_exactly(
"and_this=; max-age=0; expires=#{cookie_expires_value}",
"delete_this_cookie=; max-age=0; expires=#{cookie_expires_value}"
)
end

Expand All @@ -238,9 +239,9 @@ def app
end
get '/test'
expect(last_response.body).to eq('3')
expect(last_response.headers[Rack::SET_COOKIE]).to contain_exactly(
'and_this=; path=/test; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT',
'delete_this_cookie=; path=/test; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT'
expect(last_response_cookies).to contain_exactly(
"and_this=; path=/test; max-age=0; expires=#{cookie_expires_value}",
"delete_this_cookie=; path=/test; max-age=0; expires=#{cookie_expires_value}"
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/rack/v3/headers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe Grape::Http::Headers do
describe Grape::Http::Headers, if: Gem::Version.new(Rack.release) >= Gem::Version.new('3') do
it { expect(described_class::ALLOW).to eq('allow') }
it { expect(described_class::LOCATION).to eq('location') }
it { expect(described_class::TRANSFER_ENCODING).to eq('transfer-encoding') }
Expand Down
15 changes: 15 additions & 0 deletions spec/support/cookie_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Spec
module Support
module Helpers
def cookie_expires_value
@cookie_expired ||= Gem::Version.new(Rack.release) <= Gem::Version.new('2') ? Time.at(0).gmtime.rfc2822 : Time.at(0).httpdate
end

def last_response_cookies
Array(last_response.headers['Set-Cookie']).flat_map { |h| h.split("\n") }.sort
end
end
end
end

0 comments on commit 1dc9799

Please sign in to comment.