File size: 1,484 Bytes
6778ee0 | 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 | defmodule Plausible.Test.Support.HTTPMocker do
@moduledoc """
Currently only supports post request, it's a drop-in replacement
for our exvcr usage that wasn't ever needed (e.g. we had no way to
re-record the cassettes anyway).
"""
defmacro __using__(_) do
quote do
import Mox
def mock_http_with(http_mock_fixture) do
mocks =
"fixture/http_mocks/#{http_mock_fixture}"
|> File.read!()
|> Jason.decode!()
|> Enum.into(%{}, &{{&1["url"], &1["request_body"]}, &1})
stub(
Plausible.HTTPClient.Mock,
:post,
fn url, _, params, _ -> http_mocker_stub(mocks, url, params) end
)
stub(
Plausible.HTTPClient.Mock,
:post,
fn url, _, params -> http_mocker_stub(mocks, url, params) end
)
end
defp http_mocker_stub(mocks, url, params) do
params =
case Jason.encode(params) do
{:ok, p} -> Jason.decode!(p)
{:error, _} -> params
end
mock = Map.fetch!(mocks, {url, params})
response = %Finch.Response{
status: mock["status"],
headers: [{"content-type", "application/json"}],
body: mock["response_body"]
}
if mock["status"] >= 200 and mock["status"] < 300 do
{:ok, response}
else
{:error, Plausible.HTTPClient.Non200Error.new(response)}
end
end
end
end
end
|