CodeSampleX

샘플

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

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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

컨트랙트

  1. assert leaving proxy unset automatically resolves to Faraday::ProxyOptions from the environment rather than nil
  2. assert passing proxy false does not disable environment proxy lookup and still resolves to ProxyOptions
  3. assert passing an empty hash for proxy successfully disables environment proxy resolution with an empty ProxyOptions
  4. assert leaving headers unset populates default User-Agent header rather than empty headers
  5. assert passing custom headers merges with default headers rather than replacing User-Agent
  6. assert leaving ssl verify unset returns nil for verify attribute but true for verify? predicate
  7. 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

소스 아티팩트 내려받기 (tar.gz)

소스

Gemfile
source "https://rubygems.org"

gem "faraday", "2.14.3"
gem "minitest", "5.27.0"
Gemfile.lock
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.md
# 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.
csx.json
{"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"}
src/client.rb
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
test/contract.rb
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

오리진 시더

csx-seed