From 3ecd1458169c83ab96a44aaeb59abeb15f6b33b0 Mon Sep 17 00:00:00 2001 From: Camilo Hollanda <775409+prem-prakash@users.noreply.github.com> Date: Thu, 5 Sep 2024 15:42:01 -0300 Subject: [PATCH] add custom etag option to rack_response plugin --- lib/shrine/plugins/rack_response.rb | 4 ++-- test/plugin/rack_response_test.rb | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/shrine/plugins/rack_response.rb b/lib/shrine/plugins/rack_response.rb index b0f54c7b..2f8585ff 100644 --- a/lib/shrine/plugins/rack_response.rb +++ b/lib/shrine/plugins/rack_response.rb @@ -49,14 +49,14 @@ def rack_status(range: nil, **) # "Content-Disposition" headers, whose values are extracted from # metadata. Also returns the correct "Content-Range" header on ranged # requests. - def rack_headers(filename: nil, type: nil, disposition: "inline", range: false) + def rack_headers(filename: nil, type: nil, disposition: "inline", range: false, custom_etag: nil) { "Content-Length" => content_length(range), "Content-Type" => content_type(type), "Content-Disposition" => content_disposition(disposition, filename), "Content-Range" => content_range(range), "Accept-Ranges" => accept_ranges(range), - "ETag" => etag, + "ETag" => custom_etag || etag, }.compact end diff --git a/test/plugin/rack_response_test.rb b/test/plugin/rack_response_test.rb index b3a5386c..5e060991 100644 --- a/test/plugin/rack_response_test.rb +++ b/test/plugin/rack_response_test.rb @@ -232,6 +232,13 @@ assert_equal etags, etags.uniq end + it "allows custom ETAG value" do + uploaded_file = @uploader.upload(fakeio) + response = uploaded_file.to_rack_response(custom_etag: "VeryCustomETAG") + assert_instance_of String, response[1]["ETag"] + assert_match "VeryCustomETAG", response[1]["ETag"] + end + it "implements #to_path on the body for filesystem storage" do uploaded_file = @uploader.upload(fakeio) response = uploaded_file.to_rack_response