<commit_before>defmodule Tesla.Mock do
  @moduledoc """
  Mock adapter for better testing.

  ## Setup

  ```
  # config/test.exs
  config :tesla, adapter: Tesla.Mock

  # in case MyClient defines specific adapter with `adapter SpecificAdapter`
  config :tesla, MyClient, adapter: Tesla.Mock
  ```

  ## Examples

  ```
  defmodule MyAppTest do
    use ExUnit.Case

    setup do
      Tesla.Mock.mock(fn
        %{method: :get} ->
          %Tesla.Env{status: 200, body: "hello"}
      end)

      :ok
    end

    test "list things" do
      assert {:ok, env} = MyApp.get("...")
      assert env.status == 200
      assert env.body == "hello"
    end
  end
  ```

  ## Setting up mocks

  ```
  # Match on method & url and return whole Tesla.Env
  Tesla.Mock.mock(fn
    %{method: :get, url: "http://example.com/list"} ->
      %Tesla.Env{status: 200, body: "hello"}
  end)

  # You can use any logic required
  Tesla.Mock.mock(fn env ->
    case env.url do
      "http://example.com/list" ->
        %Tesla.Env{status: 200, body: "ok!"}

      _ ->
        %Tesla.Env{status: 404, body: "NotFound"}
    end
  end)


  # mock will also accept short version of response
  # in the form of {status, headers, body}
  Tesla.Mock.mock(fn
    %{method: :post} -> {201, %{}, %{id: 42}}
  end)
  ```

  ## Global mocks

  By default, mocks are bound to the current process,
  i.e. the process running a single test case.
  This design allows proper isolation between test cases
  and make testing in parallel (`async: true`) possible.

  While this style is recommended, there is one drawback:
  if Tesla client is called from different process
  it will not use the setup mock.

  To solve this issue it is possible to setup a global mock
  using `mock_global/1` function.

  ```
  defmodule MyTest do
    use ExUnit.Case, async: false # must be false!
<commit_msg>Document that mock can be configured to return error tuples.<commit_after>defmodule Tesla.Mock do
  @moduledoc """
  Mock adapter for better testing.

  ## Setup

  ```
  # config/test.exs
  config :tesla, adapter: Tesla.Mock

  # in case MyClient defines specific adapter with `adapter SpecificAdapter`
  config :tesla, MyClient, adapter: Tesla.Mock
  ```

  ## Examples

  ```
  defmodule MyAppTest do
    use ExUnit.Case

    setup do
      Tesla.Mock.mock(fn
        %{method: :get} ->
          %Tesla.Env{status: 200, body: "hello"}
      end)

      :ok
    end

    test "list things" do
      assert {:ok, env} = MyApp.get("...")
      assert env.status == 200
      assert env.body == "hello"
    end
  end
  ```

  ## Setting up mocks

  ```
  # Match on method & url and return whole Tesla.Env
  Tesla.Mock.mock(fn
    %{method: :get, url: "http://example.com/list"} ->
      %Tesla.Env{status: 200, body: "hello"}
  end)

  # You can use any logic required
  Tesla.Mock.mock(fn env ->
    case env.url do
      "http://example.com/list" ->
        %Tesla.Env{status: 200, body: "ok!"}

      _ ->
        %Tesla.Env{status: 404, body: "NotFound"}
    end
  end)


  # mock will also accept short version of response
  # in the form of {status, headers, body}
  Tesla.Mock.mock(fn
    %{method: :post} -> {201, %{}, %{id: 42}}
  end)

  # mock will also accept error tuples in the form
  # of {:error, reason}
  Tesla.Mock.mock(fn
    %{method: :post} -> {:error, :timeout}
  end)
  ```

  ## Global mocks

  By default, mocks are bound to the current process,
  i.e. the process running a single test case.
  This design allows proper isolation between test cases
  and make testing in parallel (`async: true`) possible.

  While this style is recommended, there is one drawback:
  if Tesla client is called from different process
  it will not use the setup mock.

  To solve this issue it is possible to setup a global mock
  using `mock_global/1` function.

  ```
  defmodule MyTest do
    use ExUnit.Case, async: false # must be false!

{'fraction_non_alphanumeric': 0.11784253578732107, 'fraction_numerical': 0.010736196319018405, 'mean_word_length': 2.6164510166358594, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5958098', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1247, 'n_words': 451}