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
77 changes: 77 additions & 0 deletions google-cloud-storage/acceptance/storage/headers_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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"
require 'pry'

describe "Accept-Encoding" do
# Inject an array into the middleware instead of using class-level state
class HeaderRecorder < Faraday::Middleware
def initialize(app, captured_headers)
super(app)
@captured_headers = captured_headers
end

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

let(:storage) { Google::Cloud::Storage.new }
let(:captured_headers) { [] }
let(:client) { storage.service.service.client }

before do
# Pass our local captured_headers array to the middleware
client.builder.insert_before(0, HeaderRecorder, captured_headers)
end

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

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

refute_empty captured_headers

# Assert none of the requests included the header
assert captured_headers.none? { |headers| headers.key?("Accept-Encoding") }
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 = storage.create_bucket(bucket_name)

begin
# Reset our local array instead of calling a class method
captured_headers.clear

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

captured_headers.clear

# Test Download
file.download
assert_includes captured_headers.map { |h| h["Accept-Encoding"] }, "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