http_request_mock.ex 2.27 KB
Newer Older
Maksim's avatar
Maksim committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
defmodule HttpRequestMock do
  def request(
        %Tesla.Env{
          url: url,
          method: method,
          headers: headers,
          query: query,
          body: body
        } = _env
      ) do
    with {:ok, res} <- apply(__MODULE__, method, [url, query, body, headers]) do
      res
    else
      {_, r} = error ->
        IO.warn(r)
        error
    end
  end

  # GET Requests
  #
  def get(url, query \\ [], body \\ [], headers \\ [])

  def get("https://social.heldscal.la/api/statuses/user_timeline/23211.atom", _, _, _) do
    {:ok, %Tesla.Env{
        status: 200,
        body: File.read!(
          "test/fixtures/httpoison_mock/https___social.heldscal.la_api_statuses_user_timeline_23211.atom.xml"
        )}}
  end

  def get("https://social.heldscal.la/.well-known/webfinger?resource=https://social.heldscal.la/user/23211", _, _, _) do
    {:ok, %Tesla.Env{
        status: 200,
        body: File.read!("test/fixtures/httpoison_mock/https___social.heldscal.la_user_23211.xml")}}
  end

  def get("http://social.heldscal.la/.well-known/host-meta", _, _, _) do
    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")}}
  end

  def get("https://social.heldscal.la/.well-known/host-meta", _, _, _) do
    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/httpoison_mock/social.heldscal.la_host_meta")}}
  end

  def get("https://mastodon.social/users/lambadalambda.atom", _, _, _) do
    {:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/lambadalambda.atom")}}
  end

  def get("https://social.heldscal.la/user/23211", _, _, [Accept: "application/activity+json"]) do
    {:ok,
     Tesla.Mock.json(%{"id" => "https://social.heldscal.la/user/23211"}, status: 200)
    }
  end

  def get(url, query, body, headers) do
    {:error,
     "Not implemented the mock response for get #{inspect(url)}, #{query}, #{inspect(body)}, #{
     inspect(headers)
     }"}
  end


  # POST Requests
  #

  def post(url, query \\ [], body \\ [], headers \\ [])

  def post("http://example.org/needs_refresh", _, _, _) do
    {:ok,
     %Tesla.Env{
       status: 200,
       body: ""
     }}
  end

  def post(url, _query, _body, _headers) do
    {:error, "Not implemented the mock response for post #{inspect(url)}"}
  end
end