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
87 changes: 87 additions & 0 deletions google-cloud-storage/acceptance/storage/headers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "../storage_helper"

describe "Accept-Encoding" do

class HeaderRecorder
def self.captured_headers
@captured_headers ||= []
end

def self.clear_headers!
@captured_headers = []
end

def initialize(app)
@app = app
end

def call(env)
self.class.captured_headers << env.request_headers
@app.call(env)
end
end

before do
@fresh_storage = Google::Cloud::Storage.new

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove fresh_storage, add some name like storage like that

@client = @fresh_storage.service.service.client

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why these many service.service.client

@client.builder.insert_before(0, HeaderRecorder)

# Start every test with a clean slate
HeaderRecorder.clear_headers!
end

after do
@client.builder.delete(HeaderRecorder) rescue nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

According to the community Ruby Style Guide, using the inline rescue modifier is discouraged because it can silently swallow unexpected errors (such as NoMethodError on other parts of the chain) and make debugging difficult. Since the only potential reason for an exception here is if @client or @client.builder is nil, we can use the safe navigation operator (&.) instead.

    @client&.builder&.delete(HeaderRecorder)
References
  1. Avoid using rescue in its modifier form. Use safe navigation instead to handle potential nil values. (link)

end

it "does not include Accept-Encoding gzip header on metadata calls" do
@fresh_storage.buckets(max: 1)

refute_empty HeaderRecorder.captured_headers
HeaderRecorder.captured_headers.each do |headers|
assert_nil headers["Accept-Encoding"]
end
end

it "includes Accept-Encoding gzip header on media calls (upload/download)" do
bucket_name = "gcloud-test-ae-#{Time.now.to_i}-#{SecureRandom.hex(4)}"
bucket = @fresh_storage.create_bucket(bucket_name)

begin
# Clear the headers generated by the bucket creation above
HeaderRecorder.clear_headers!

# Test Upload
file = bucket.create_file(StringIO.new("hello world"), "test.txt")
upload_headers = HeaderRecorder.captured_headers.map { |h| h["Accept-Encoding"] }
assert_includes upload_headers, "gzip"

# Clear headers before the next action
HeaderRecorder.clear_headers!

# Test Download
file.download
download_headers = HeaderRecorder.captured_headers.map { |h| h["Accept-Encoding"] }

assert_includes download_headers, "gzip"
ensure
# Cleanup
safe_gcs_execute { file&.delete }
safe_gcs_execute { bucket&.delete }
end
end
end
1 change: 0 additions & 1 deletion google-cloud-storage/lib/google/cloud/storage/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def initialize project, credentials, retries: nil,
@service.request_options.header ||= {}
@service.request_options.header["x-goog-api-client"] =
"gl-ruby/#{RUBY_VERSION} gccl/#{Google::Cloud::Storage::VERSION}"
@service.request_options.header["Accept-Encoding"] = "gzip"
@service.request_options.quota_project = quota_project if quota_project
@service.request_options.max_elapsed_time = max_elapsed_time if max_elapsed_time
@service.request_options.base_interval = base_interval if base_interval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def creds.is_a? target
_(project.universe_domain).must_equal "googleapis.com"
end

it "does not set Accept-Encoding gzip header by default" do
service = Google::Cloud::Storage::Service.new "my-project", default_credentials
_(service.service.request_options.header["Accept-Encoding"]).must_be :nil?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In Minitest, it is more idiomatic and readable to use the built-in must_be_nil expectation instead of must_be :nil?.

    _(service.service.request_options.header["Accept-Encoding"]).must_be_nil
References
  1. Use idiomatic Minitest expectations like must_be_nil instead of generic predicate checks. (link)

end

it "supports setting a universe domain argument" do
service = Google::Cloud::Storage::Service.new "my-project", default_credentials, universe_domain: "mydomain1.com"
_(service.universe_domain).must_equal "mydomain1.com"
Expand Down