Skip to content
Open
Show file tree
Hide file tree
Changes from all 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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [#3008](https://github.com/ruby-grape/grape/pull/3008): Add the grape-on-rack app as a live-server memory profiling harness - [@ericproulx](https://github.com/ericproulx).
* [#3025](https://github.com/ruby-grape/grape/pull/3025): Add `:md` and `:markdown` formatters that render an object's `to_markdown`, for APIs that declare a `text/markdown` content type - [@ericproulx](https://github.com/ericproulx).
* [#3029](https://github.com/ruby-grape/grape/pull/3029): Add a built-in `:jsonl` (JSON Lines) format that streams an Enumerator body - [@ericproulx](https://github.com/ericproulx).
* [#3041](https://github.com/ruby-grape/grape/pull/3041): Add `Rack::TempfileReaper` to every endpoint's middleware stack so multipart upload tempfiles are unlinked after the response - [@anyingiit](https://github.com/anyingiit).
* Your contribution here.

#### Fixes
Expand Down
1 change: 1 addition & 0 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ def build_stack
content_types = setting.content_types
format = setting.format

stack.use Rack::TempfileReaper
stack.use Rack::Head
stack.use Grape::Middleware::Error, **error_middleware_options(format, content_types)

Expand Down
40 changes: 40 additions & 0 deletions spec/grape/integration/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,46 @@
end
end

# https://github.com/ruby-grape/grape/issues/2487
describe 'multipart tempfiles' do
let(:uploaded_paths) { [] }

let(:app) do
paths = uploaded_paths
Class.new(Grape::API) do
params do
requires :file, type: File
end

post 'upload' do
paths << params[:file][:tempfile].path
'ok'
end

post 'failed_upload' do
paths << params[:file][:tempfile].path
error!('failed', 422)
end
end
end

let(:upload) { Rack::Test::UploadedFile.new(__FILE__, 'text/plain') }

it 'unlinks the tempfile once the response is closed' do
post '/upload', file: upload
expect(last_response.body).to eq('ok')
expect(uploaded_paths.size).to eq(1)
expect(File.exist?(uploaded_paths.first)).to be(false)
end

it 'unlinks the tempfile when the endpoint returns an error' do
post '/failed_upload', file: upload
expect(last_response.status).to eq(422)
expect(uploaded_paths.size).to eq(1)
expect(File.exist?(uploaded_paths.first)).to be(false)
end
end

context 'when the app is mounted' do
let(:ping_mount) do
Class.new(Grape::API) do
Expand Down
Loading