サンプル
faraday 2.14.3: Resolve default proxy, header, and SSL options when arguments are left unset or falsy in Faraday
検証済みサンプル — gem faraday 2.14.3: Resolve default proxy, header, and SSL options when arguments are left unset or falsy in Faraday. ruby 3 · linux debian/x64 …
sha256:092ddffa6f6f68434f6c75e4aaeb452e110f6dcb95be414f245e60a383cac209
このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。
合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。
MIT-0
実行証拠
宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。
- 証拠の基準
- 署名済みコントラクト合格
- 検証レシート
- 2
- ビルドした署名鍵
- 2
宣言された環境
ruby linux x64 ruby ruby rubygems
検証実行環境
| 環境 | コントラクト | ステージ | 実行日 |
|---|---|---|---|
| ruby 3 · linux debian/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · rubygems@1 |
2026-08-17 |
| ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · rubygems@1 |
2026-08-18 |
ケース
HOW- ゴール
- Resolve default proxy, header, and SSL options when arguments are left unset or falsy in Faraday
- パッケージ
- シンボル
-
- Faraday.new
- Faraday::Connection
- Faraday::Connection#proxy
- Faraday::Connection#headers
- Faraday::Connection#ssl
- Faraday::ProxyOptions
- Faraday::SSLOptions
- Faraday::Utils::Headers
- 環境
- ruby
- 作成日
- 2026-08-17T02:37:49Z
コントラクト
- assert leaving proxy unset automatically resolves to Faraday::ProxyOptions from the environment rather than nil
- assert passing proxy false does not disable environment proxy lookup and still resolves to ProxyOptions
- assert passing an empty hash for proxy successfully disables environment proxy resolution with an empty ProxyOptions
- assert leaving headers unset populates default User-Agent header rather than empty headers
- assert passing custom headers merges with default headers rather than replacing User-Agent
- assert leaving ssl verify unset returns nil for verify attribute but true for verify? predicate
- assert passing ssl verify as nil leaves verify? enabled and only explicit false sets disable? to true
ファイル
- Gemfile
- Gemfile.lock
- NOTES.md
- csx.json
- src/client.rb
- test/contract.rb
ソース
source "https://rubygems.org"
gem "faraday", "2.14.3"
gem "minitest", "5.27.0"
GEM
remote: https://rubygems.org/
specs:
faraday (2.14.3)
faraday-net_http (>= 2.0, < 3.5)
json
logger
faraday-net_http (3.4.4)
net-http (~> 0.5)
json (2.21.2)
logger (1.7.0)
minitest (5.27.0)
net-http (0.9.1)
uri (>= 0.11.1)
uri (1.1.1)
PLATFORMS
ruby
x64-mingw-ucrt
DEPENDENCIES
faraday (= 2.14.3)
minitest (= 5.27.0)
CHECKSUMS
bundler (4.0.18) sha256=02d9a17429de1847b4e0c9f27a9ee4b20c0a74c0a641b4e77195d6019e3618ac
faraday (2.14.3) sha256=1882247e6766615c8220b4392bf1d27f6ebb63d8e28267587cef1fb0bf37f278
faraday-net_http (3.4.4) sha256=0e78af151747ed1b00f33e25973b4bc220d7f16c00c39676817c8b12331eb588
json (2.21.2) sha256=1f1d3b7cf2b3ba1a69beca0bb6db13d5438b80bff3cd54cdaaa620b9b07c1c6a
logger (1.7.0) sha256=196edec7cc44b66cfb40f9755ce11b392f21f7967696af15d274dde7edff0203
minitest (5.27.0) sha256=2d3b17f8a36fe7801c1adcffdbc38233b938eb0b4966e97a6739055a45fa77d5
net-http (0.9.1) sha256=25ba0b67c63e89df626ed8fac771d0ad24ad151a858af2cc8e6a716ca4336996
uri (1.1.1) sha256=379fa58d27ffb1387eaada68c749d1426738bd0f654d812fcc07e7568f5c57c6
BUNDLED WITH
4.0.18
# Notes on Faraday Default Option Resolution and Unset Arguments
## What search_known_solution answered
`search_known_solution` returned a COMPATIBLE hit for sample `sha256:c50f9bbfe83d4d0f5b44242b2e1675ffca6f3c29d43f04ddd3ac9e36f0f0ce46` (which addresses url_prefix leading slash path stripping and params_encoder bracket formatting) and referenced `sha256:46eed9d91592cb5df8548ed6884a818eba294fbab879f42106d919a145b91d8a` (which addresses Faraday 2 gem separation and test adapter stubbing). This sample addresses a different configuration trap: default option resolution when options are left unset, nil, or falsy.
## What a naive model would write instead
A naive model assumes that calling `Faraday.new` with unset options defaults to a direct connection with `conn.proxy = nil`, that passing `proxy: false` explicitly opts out of environment proxies, that headers are initialized empty (`{}`) without a default `User-Agent`, and that `conn.ssl.verify` returns a boolean default (`true`).
## How the wrong version fails
Silently with a green build, as passing `proxy: false` evaluates as falsy in `initialize_proxy` and continues to route traffic through the `http_proxy` found in the environment, while leaving headers unset quietly transmits Faraday's default `User-Agent` header across all requests.
{"case":{"believed":"Leaving the proxy option unset in Faraday defaults to a direct connection with nil proxy options, and passing proxy false disables environment proxy discovery.","caseId":"case:sha256:aacaad7e3b06658908c3b3ed7f1490818db1b2c6ada1c73dbaa1f699656e33d5","contract":["assert leaving proxy unset automatically resolves to Faraday::ProxyOptions from the environment rather than nil","assert passing proxy false does not disable environment proxy lookup and still resolves to ProxyOptions","assert passing an empty hash for proxy successfully disables environment proxy resolution with an empty ProxyOptions","assert leaving headers unset populates default User-Agent header rather than empty headers","assert passing custom headers merges with default headers rather than replacing User-Agent","assert leaving ssl verify unset returns nil for verify attribute but true for verify? predicate","assert passing ssl verify as nil leaves verify? enabled and only explicit false sets disable? to true"],"goal":"Resolve default proxy, header, and SSL options when arguments are left unset or falsy in Faraday","kind":"HOW","packages":["pkg:gem/faraday@2.14.3"],"schemaVersion":1,"symbols":["Faraday.new","Faraday::Connection","Faraday::Connection#proxy","Faraday::Connection#headers","Faraday::Connection#ssl","Faraday::ProxyOptions","Faraday::SSLOptions","Faraday::Utils::Headers"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"rubygems","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/faraday@2.14.3"],"schemaVersion":1,"symbols":["Faraday.new","Faraday::Connection","Faraday::Connection#proxy","Faraday::Connection#headers","Faraday::Connection#ssl","Faraday::ProxyOptions","Faraday::SSLOptions","Faraday::Utils::Headers"],"verifierAdapter":"rubygems@1"}
require "faraday"
module Client
def self.build_connection(url: "http://example.com", proxy: :unset, headers: :unset, ssl: :unset)
opts = {}
opts[:proxy] = proxy unless proxy == :unset
opts[:headers] = headers unless headers == :unset
opts[:ssl] = ssl unless ssl == :unset
Faraday.new(url, opts)
end
end
require "minitest/autorun"
require_relative "../src/client"
class FaradayDefaultsContract < Minitest::Test
def setup
@original_http_proxy = ENV["http_proxy"]
@original_https_proxy = ENV["https_proxy"]
end
def teardown
if @original_http_proxy
ENV["http_proxy"] = @original_http_proxy
else
ENV.delete("http_proxy")
end
if @original_https_proxy
ENV["https_proxy"] = @original_https_proxy
else
ENV.delete("https_proxy")
end
end
def test_leaving_proxy_unset_resolves_from_environment
ENV["http_proxy"] = "http://proxy.example.com:8080"
conn = Client.build_connection(url: "http://example.com")
# When proxy is left unset, Faraday automatically populates conn.proxy from ENV['http_proxy']
refute_nil conn.proxy
assert_instance_of Faraday::ProxyOptions, conn.proxy
assert_equal "proxy.example.com", conn.proxy.host
assert_equal 8080, conn.proxy.port
assert_equal "http://proxy.example.com:8080", conn.proxy.uri.to_s
end
def test_passing_proxy_false_does_not_disable_environment_proxy
ENV["http_proxy"] = "http://proxy.example.com:8080"
# Naive assumption: passing proxy: false disables proxy lookups.
# Reality: initialize_proxy evaluates `false` as falsy and falls back to proxy_from_env.
conn = Client.build_connection(url: "http://example.com", proxy: false)
refute_nil conn.proxy
assert_instance_of Faraday::ProxyOptions, conn.proxy
assert_equal "proxy.example.com", conn.proxy.host
assert_equal 8080, conn.proxy.port
end
def test_passing_empty_hash_disables_environment_proxy_resolution
ENV["http_proxy"] = "http://proxy.example.com:8080"
# Passing an empty hash proxy: {} evaluates to truthy in initialize_proxy,
# constructing an empty ProxyOptions and successfully bypassing proxy_from_env.
conn = Client.build_connection(url: "http://example.com", proxy: {})
assert_instance_of Faraday::ProxyOptions, conn.proxy
assert_nil conn.proxy.uri
assert_nil conn.proxy.user
assert_nil conn.proxy.password
end
def test_leaving_headers_unset_populates_default_user_agent
conn = Client.build_connection(url: "http://example.com")
# Headers is not empty by default; Faraday injects User-Agent
assert_instance_of Faraday::Utils::Headers, conn.headers
refute_empty conn.headers
assert_equal "Faraday v#{Faraday::VERSION}", conn.headers["User-Agent"]
end
def test_passing_custom_headers_merges_with_default_headers
conn = Client.build_connection(
url: "http://example.com",
headers: { "Authorization" => "Bearer secret-token" }
)
# Custom headers do not replace default headers; User-Agent remains intact
assert_equal "Bearer secret-token", conn.headers["Authorization"]
assert_equal "Faraday v#{Faraday::VERSION}", conn.headers["User-Agent"]
end
def test_leaving_ssl_verify_unset_returns_nil_verify_but_true_verify_predicate
conn = Client.build_connection(url: "https://example.com")
# SSL verify attribute is nil when unset, but verify? predicate returns true
assert_instance_of Faraday::SSLOptions, conn.ssl
assert_nil conn.ssl.verify
assert_equal true, conn.ssl.verify?
assert_equal false, conn.ssl.disable?
end
def test_passing_ssl_verify_nil_leaves_verify_enabled
conn = Client.build_connection(url: "https://example.com", ssl: { verify: nil })
# verify: nil does not disable verification; only explicit verify: false does
assert_nil conn.ssl.verify
assert_equal true, conn.ssl.verify?
assert_equal false, conn.ssl.disable?
conn_disabled = Client.build_connection(url: "https://example.com", ssl: { verify: false })
assert_equal false, conn_disabled.ssl.verify
assert_equal false, conn_disabled.ssl.verify?
assert_equal true, conn_disabled.ssl.disable?
end
end