CodeSampleX

Exemplo

faraday 2.14.3: Prove that explicit Content-Type overrides can disable Faraday's request JSON encoder.

Amostra verificada para gem faraday 2.14.3: Prove that explicit Content-Type overrides can disable Faraday's request JSON encoder. O contrato rodou em ruby 3…

sha256:c583afd65ae4fef23d2ba39bc24833e246ff77df5dfb62572a921224ed43a5e1

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
2
Chaves de assinatura que o compilaram
2
Ambiente declarado ruby linux x64 ruby ruby bundler

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
Prove that explicit Content-Type overrides can disable Faraday's request JSON encoder.
Pacotes
Símbolos
  • Faraday::Connection
  • Faraday::Request::Json
Ambiente
ruby
Criado
2026-08-17T02:38:02Z

Contrato

  1. assert that with request :json and no Content-Type override, a POST with {name: "sam"} serializes env.body to a JSON string
  2. assert that with request :json and explicit Content-Type: application/json, a POST with {name: "sam"} still serializes env.body to a JSON string
  3. assert that with request :json and explicit Content-Type: text/plain, a POST with {name: "sam"} leaves env.body as a Hash instead of serializing it to JSON

Arquivos

  • Gemfile
  • Gemfile.lock
  • NOTES.md
  • csx.json
  • test/contract.rb

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

Gemfile
source "https://rubygems.org"

gem "faraday", "2.14.3"
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)
    net-http (0.9.1)
      uri (>= 0.11.1)
    uri (1.1.1)

PLATFORMS
  ruby
  x64-mingw-ucrt

DEPENDENCIES
  faraday (= 2.14.3)

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
  net-http (0.9.1) sha256=25ba0b67c63e89df626ed8fac771d0ad24ad151a858af2cc8e6a716ca4336996
  uri (1.1.1) sha256=379fa58d27ffb1387eaada68c749d1426738bd0f654d812fcc07e7568f5c57c6

BUNDLED WITH
  4.0.18
NOTES.md
search_known_solution returned Faraday samples `case:sha256:91cf9aa05437ce1f92982b2fca2f9eccc56bb8dd1d5621086df6efcc682ef54f` and `case:sha256:1c8ebe08d179b2355556694606189b6b5cf0615403b1194d2b57e925d8c0d3c3` (plus 2.x adaptation cases), but none for the explicit Content-Type override gate.
Wrong expectation fails silently: the request succeeds while returning a Ruby Hash body under `text/plain`, so downstream code receives un-serialized payload with no exception.
csx.json
{"case":{"believed":"If request middleware includes :json, Faraday always JSON-serializes Hash bodies, even when a different Content-Type header is supplied.","caseId":"case:sha256:96c445836a569c49908f27d8d469e25a8fe70b492793ad5f21337976e413608d","contract":["assert that with request :json and no Content-Type override, a POST with {name: \"sam\"} serializes env.body to a JSON string","assert that with request :json and explicit Content-Type: application/json, a POST with {name: \"sam\"} still serializes env.body to a JSON string","assert that with request :json and explicit Content-Type: text/plain, a POST with {name: \"sam\"} leaves env.body as a Hash instead of serializing it to JSON"],"goal":"Prove that explicit Content-Type overrides can disable Faraday's request JSON encoder.","kind":"HOW","packages":["pkg:gem/faraday@2.14.3"],"schemaVersion":1,"symbols":["Faraday::Connection","Faraday::Request::Json"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"bundler","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/faraday@2.14.3"],"schemaVersion":1,"symbols":["Faraday::Connection","Faraday::Request::Json"],"verifierAdapter":"rubygems@1"}
test/contract.rb
require "faraday"
require "faraday/adapter/test"

def capture_request(content_type: nil)
  captured = {}

  connection = Faraday.new(url: "https://example.test") do |builder|
    builder.request :json
    builder.adapter :test do |stubs|
      stubs.post("/payload") do |env|
        captured = {
          body: env.body,
          content_type: env.request_headers["Content-Type"],
        }
        [200, {}, "ok"]
      end
    end
  end

  if content_type
    connection.post("/payload", { name: "sam" }, { "Content-Type" => content_type })
  else
    connection.post("/payload", { name: "sam" })
  end

  captured
end

def assert(condition, message)
  raise "Assertion failed: #{message}" unless condition
end

default_case = capture_request
json_case = capture_request(content_type: "application/json")
text_case = capture_request(content_type: "text/plain")

assert(default_case[:body].is_a?(String) && default_case[:body] == "{\"name\":\"sam\"}", "default case did not emit JSON")
assert(default_case[:content_type] == "application/json", "default case did not set Content-Type to application/json")

assert(json_case[:body].is_a?(String) && json_case[:body] == "{\"name\":\"sam\"}", "explicit application/json case did not emit JSON")
assert(json_case[:content_type] == "application/json", "explicit application/json case changed Content-Type")

assert(text_case[:body].is_a?(Hash), "text/plain case unexpectedly serialized body")
assert(text_case[:content_type] == "text/plain", "explicit text/plain case did not preserve header")

puts "OK"

Seeder de origem

csx-seed