CodeSampleX

サンプル

req 0.7.2: Stub HTTP responses for Elixir code that uses Req, with no network, no fake server process and no mocking library

検証済みサンプル — hex req 0.7.2: Stub HTTP responses for Elixir code that uses Req, with no network, no fake server process and no mocking library. elixir 1 · linux…

sha256:1a5be8dc134928a8238887f9504e70f60f67fb5fa956fd3fc577d86784ecfa1d

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 elixir 1 linux x64 elixir 1 elixir mix

検証実行環境

環境 コントラクト ステージ 実行日
elixir 1 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:PASS · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · hex@1
2026-08-14

ケース

HOW
ゴール
Stub HTTP responses for Elixir code that uses Req, with no network, no fake server process and no mocking library
パッケージ
シンボル
  • Req.Test.stub
  • Req.Test.expect
  • Req.Test.verify!
  • Req.Test.json
  • Req.Test.transport_error
  • Req.Plug
  • Req.Steps.decode_body
  • Req.Steps.retry
  • Req.Steps.handle_http_errors
  • Req.request!
  • Req.TransportError
  • Plug.Conn.send_resp
環境
elixir 1
作成日
2026-08-14T20:15:02Z

コントラクト

  1. assert the response content-type is what decodes the body, so a stub built with Req.Test.json hands the caller a map and no .json() accessor is called anywhere, and that headers are a map of lists where a missing header reads as nil
  2. assert the same JSON bytes sent with Plug.Conn.send_resp and no content-type stay a binary, so a clean HTTP 200 stops matching the caller's %{"celsius" => _} clause
  3. assert that with no content-type the request URL path decides instead: /data.json is decoded while /data and /data.txt are not, because decode_body falls back to application/octet-stream and resolves that through MIME.from_path
  4. assert an HTTP 500 is {:ok, response} and raises from neither Req.request/1 nor Req.request!/1, that the error body is decoded like any other body, and that http_errors: :raise reports the body still undecoded because handle_http_errors runs ahead of decode_body
  5. assert retry is on by default: one 500 on GET calls the stub four times (the initial request plus max_retries 3), the same 500 on POST calls it once because :safe_transient covers GET and HEAD only, and retry: :transient opts POST in
  6. assert Req.Test.transport_error produces {:error, %Req.TransportError{}} rather than a response, which a queue of canned responses cannot express, and that Req.get! raises it
  7. assert expectations are consumed in order ahead of a stub whatever the registration order, and that once drained Req.Test falls through to the stub instead of failing
  8. assert an exhausted expectation with no stub raises "no mock or stub for X" while a name never registered raises the different "cannot find mock/stub X in process #PID<...>"
  9. assert an unmet expectation fails nothing until Req.Test.verify! is called, and that verify! then names how many more uses were expected
  10. assert JSON decoding is switched off by decode_body: false, decoders: false or raw: true, that :decoders replaces the default [:json, :json_api] rather than extending it, and that a codec under :decoders is the 0.7.0 replacement for the deprecated :decode_json
  11. assert Req.Test implements the Plug behaviour and the stub name is literally that plug's options, so a bare function plug stubs HTTP with no registry at all

ファイル

  • csx.json
  • lib/csx_req/weather.ex
  • mix.exs
  • mix.lock
  • test/contract_test.exs
  • test/test_helper.exs

ソースアーティファクトをダウンロード (tar.gz)

オリジンシーダー

anonymous