From 10944de4cf81fb23bf0ea2abcf7e8bcfe59db5b3 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Fri, 5 Jan 2024 23:08:21 +0100 Subject: [PATCH] Remove mime-types dependency in specs (#2406) * Remove mime-types dependency in specs * Add CHANGELOG.md entry --- CHANGELOG.md | 1 + Gemfile | 1 - gemfiles/multi_json.gemfile | 1 - gemfiles/multi_xml.gemfile | 1 - gemfiles/rack_1_0.gemfile | 1 - gemfiles/rack_2_0.gemfile | 1 - gemfiles/rack_3_0.gemfile | 1 - gemfiles/rack_edge.gemfile | 1 - gemfiles/rails_6_0.gemfile | 1 - gemfiles/rails_6_1.gemfile | 1 - gemfiles/rails_7_0.gemfile | 1 - gemfiles/rails_7_1.gemfile | 1 - gemfiles/rails_edge.gemfile | 1 - spec/grape/api_spec.rb | 45 ++++++++++++++++++++++--------------- 14 files changed, 28 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43ff91251f..d6ba50c99a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * [#2397](https://github.com/ruby-grape/grape/pull/2397): Add support for ruby 3.3 - [@ericproulx](https://github.com/ericproulx). * [#2399](https://github.com/ruby-grape/grape/pull/2399): Update `rubocop` to 1.59.0, `rubocop-performance` to 1.20.1 and `rubocop-rspec` to 2.25.0 - [@ericproulx](https://github.com/ericproulx). * [#2402](https://github.com/ruby-grape/grape/pull/2402): Grape::Deprecations will be raised when running specs - [@ericproulx](https://github.com/ericproulx). +* [#2406](https://github.com/ruby-grape/grape/pull/2406): Remove mime-types dependency in specs - [@ericproulx](https://github.com/ericproulx). * Your contribution here. #### Fixes diff --git a/Gemfile b/Gemfile index f845da35c6..e7731ef6f3 100644 --- a/Gemfile +++ b/Gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/multi_json.gemfile b/gemfiles/multi_json.gemfile index b7030feaf0..5ed288a8de 100644 --- a/gemfiles/multi_json.gemfile +++ b/gemfiles/multi_json.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/multi_xml.gemfile b/gemfiles/multi_xml.gemfile index b4690fe967..dfb2a8730f 100644 --- a/gemfiles/multi_xml.gemfile +++ b/gemfiles/multi_xml.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rack_1_0.gemfile b/gemfiles/rack_1_0.gemfile index 0461250b46..41730950f3 100644 --- a/gemfiles/rack_1_0.gemfile +++ b/gemfiles/rack_1_0.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rack_2_0.gemfile b/gemfiles/rack_2_0.gemfile index abda412007..8b9cced0ed 100644 --- a/gemfiles/rack_2_0.gemfile +++ b/gemfiles/rack_2_0.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rack_3_0.gemfile b/gemfiles/rack_3_0.gemfile index 958b2dd9db..59fa6a9b93 100644 --- a/gemfiles/rack_3_0.gemfile +++ b/gemfiles/rack_3_0.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rack_edge.gemfile b/gemfiles/rack_edge.gemfile index f9bd6c4a82..c61fb1914c 100644 --- a/gemfiles/rack_edge.gemfile +++ b/gemfiles/rack_edge.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rails_6_0.gemfile b/gemfiles/rails_6_0.gemfile index 357aa8a342..eec0b60743 100644 --- a/gemfiles/rails_6_0.gemfile +++ b/gemfiles/rails_6_0.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rails_6_1.gemfile b/gemfiles/rails_6_1.gemfile index 6cbd5d6fcc..5daa55be9d 100644 --- a/gemfiles/rails_6_1.gemfile +++ b/gemfiles/rails_6_1.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rails_7_0.gemfile b/gemfiles/rails_7_0.gemfile index 5d2beedabf..9e16ddb556 100644 --- a/gemfiles/rails_7_0.gemfile +++ b/gemfiles/rails_7_0.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rails_7_1.gemfile b/gemfiles/rails_7_1.gemfile index 79131db639..9401489313 100644 --- a/gemfiles/rails_7_1.gemfile +++ b/gemfiles/rails_7_1.gemfile @@ -27,7 +27,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/gemfiles/rails_edge.gemfile b/gemfiles/rails_edge.gemfile index cf0906d208..cac5c4970b 100644 --- a/gemfiles/rails_edge.gemfile +++ b/gemfiles/rails_edge.gemfile @@ -26,7 +26,6 @@ end group :test do gem 'grape-entity', '~> 0.6', require: false - gem 'mime-types' gem 'rack-jsonp', require: 'rack/jsonp' gem 'rack-test', '< 2.1' gem 'rspec', '< 4' diff --git a/spec/grape/api_spec.rb b/spec/grape/api_spec.rb index 243c6cb9ed..dc31094afc 100644 --- a/spec/grape/api_spec.rb +++ b/spec/grape/api_spec.rb @@ -1324,36 +1324,45 @@ class DummyFormatClass context 'env["api.format"]' do before do + ct = content_type subject.post 'attachment' do filename = params[:file][:filename] - content_type MIME::Types.type_for(filename)[0].to_s + content_type ct env['api.format'] = :binary # there's no formatter for :binary, data will be returned "as is" header 'Content-Disposition', "attachment; filename*=UTF-8''#{CGI.escape(filename)}" params[:file][:tempfile].read end end - ['/attachment.png', 'attachment'].each do |url| - it "uploads and downloads a PNG file via #{url}" do - image_filename = 'grape.png' - post url, file: Rack::Test::UploadedFile.new(image_filename, 'image/png', true) - expect(last_response.status).to eq(201) - expect(last_response.headers[Rack::CONTENT_TYPE]).to eq('image/png') - expect(last_response.headers['Content-Disposition']).to eq("attachment; filename*=UTF-8''grape.png") - File.open(image_filename, 'rb') do |io| - expect(last_response.body).to eq io.read + context 'when image/png' do + let(:content_type) { 'image/png' } + + %w[/attachment.png attachment].each do |url| + it "uploads and downloads a PNG file via #{url}" do + image_filename = 'grape.png' + post url, file: Rack::Test::UploadedFile.new(image_filename, content_type, true) + expect(last_response.status).to eq(201) + expect(last_response.headers[Rack::CONTENT_TYPE]).to eq(content_type) + expect(last_response.headers['Content-Disposition']).to eq("attachment; filename*=UTF-8''grape.png") + File.open(image_filename, 'rb') do |io| + expect(last_response.body).to eq io.read + end end end end - it 'uploads and downloads a Ruby file' do - filename = __FILE__ - post '/attachment.rb', file: Rack::Test::UploadedFile.new(filename, 'application/x-ruby', true) - expect(last_response.status).to eq(201) - expect(last_response.headers[Rack::CONTENT_TYPE]).to eq('application/x-ruby') - expect(last_response.headers['Content-Disposition']).to eq("attachment; filename*=UTF-8''api_spec.rb") - File.open(filename, 'rb') do |io| - expect(last_response.body).to eq io.read + context 'when ruby file' do + let(:content_type) { 'application/x-ruby' } + + it 'uploads and downloads a Ruby file' do + filename = __FILE__ + post '/attachment.rb', file: Rack::Test::UploadedFile.new(filename, content_type, true) + expect(last_response.status).to eq(201) + expect(last_response.headers[Rack::CONTENT_TYPE]).to eq(content_type) + expect(last_response.headers['Content-Disposition']).to eq("attachment; filename*=UTF-8''api_spec.rb") + File.open(filename, 'rb') do |io| + expect(last_response.body).to eq io.read + end end end end