Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove cookie-jar dependency for ruby 3.3 #2395

Merged
merged 8 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/multi_json.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/multi_xml.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rack_1_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rack_2_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rack_3_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rack_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails_6_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails_7_0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails_7_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ group :development do
end

group :test do
gem 'cookiejar'
gem 'grape-entity', '~> 0.6', require: false
gem 'mime-types'
gem 'rack-jsonp', require: 'rack/jsonp'
Expand Down
3 changes: 2 additions & 1 deletion lib/grape/cookies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def each(&block)
@cookies.each(&block)
end

# 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(value: 'deleted', 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
1 change: 0 additions & 1 deletion spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

describe Grape::API do
subject do
puts described_class
Class.new(described_class)
end

Expand Down
55 changes: 23 additions & 32 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,37 +175,42 @@ def app

get('/get/cookies')

expect(Array(last_response.headers['Set-Cookie']).flat_map { |h| h.split("\n") }.sort).to eql [
expect(last_response_cookies).to contain_exactly(
'cookie3=symbol',
'cookie4=secret+code+here',
'my-awesome-cookie1=is+cool',
'my-awesome-cookie2=is+cool+too; domain=my.example.com; path=/; secure'
]
)
end

it 'sets browser cookies and does not set response cookies' do
set_cookie %w[username=mrplum sandbox=true]
subject.get('/username') do
cookies[:username]
end
get('/username', {}, 'HTTP_COOKIE' => 'username=mrplum; sandbox=true')

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

it 'sets and update browser cookies' do
set_cookie %w[username=user sandbox=false]
subject.get('/username') do
cookies[:sandbox] = true if cookies[:sandbox] == 'false'
cookies[:username] += '_test'
end
get('/username', {}, 'HTTP_COOKIE' => 'username=user; sandbox=false')

get '/username'
expect(last_response.body).to eq('user_test')
cookies = Array(last_response.headers['Set-Cookie']).flat_map { |h| h.split("\n") }
expect(cookies[0]).to match(/username=user_test/)
expect(cookies[1]).to match(/sandbox=true/)
expect(last_response_cookies).to contain_exactly(
'sandbox=true',
'username=user_test'
)
end

it 'deletes cookie' do
set_cookie %w[delete_this_cookie=1 and_this=2]
subject.get('/test') do
sum = 0
cookies.each do |name, val|
Expand All @@ -214,22 +219,16 @@ def app
end
sum
end
get '/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2'
get '/test'
expect(last_response.body).to eq('3')
cookies = Array(last_response.headers['Set-Cookie']).flat_map { |h| h.split("\n") }.to_h do |set_cookie|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
[cookie.name, cookie]
end
expect(cookies.size).to eq(2)
%w[and_this delete_this_cookie].each do |cookie_name|
cookie = cookies[cookie_name]
expect(cookie).not_to be_nil
expect(cookie.value).to eq('deleted')
expect(cookie.expired?).to be true
end
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

it 'deletes cookies with path' do
set_cookie %w[delete_this_cookie=1 and_this=2]
subject.get('/test') do
sum = 0
cookies.each do |name, val|
Expand All @@ -238,20 +237,12 @@ def app
end
sum
end
get('/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2')
get '/test'
expect(last_response.body).to eq('3')
cookies = Array(last_response.headers['Set-Cookie']).flat_map { |h| h.split("\n") }.to_h do |set_cookie|
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
[cookie.name, cookie]
end
expect(cookies.size).to eq(2)
%w[and_this delete_this_cookie].each do |cookie_name|
cookie = cookies[cookie_name]
expect(cookie).not_to be_nil
expect(cookie.value).to eq('deleted')
expect(cookie.path).to eq('/test')
expect(cookie.expired?).to be true
end
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_expires_value ||= 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
ericproulx marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
Loading