analytics/test/support/http_mocker.ex
hq1 117eef000d
Upgrade Erlang/Elixir stack (#3454)
* Bump deps

* Bump stack

* Fix deprecation warnings

* Fix VCR cassettes mismatch due to OTP-18414

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>

* Format & fix flaky tests

* Handle raw IPv4 hostnames; test public suffix TLD

* Configure locus db cache_dir

So that maxmind unavailability doesn't affect
application startup. PERSISTENT_CACHE_DIR env var is used
to point locus at the GeoIP DB file.

* WIP: Remove ExVCR

* Fix test env config

* Fixup exvcr

* Remove exvcr from deps

* Add convert script

* Remove exvcr cassettes

* Remove convert script

* Rename test

* Update moduledoc

* Update dockerfile

* Bump CI cache

* Tag more slow tests, why not?

* Use charlist for locus cache option

* Pin nodejs

* Merge google tests, make them async

---------

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
2023-10-24 10:33:48 +02:00

55 lines
1.4 KiB
Elixir

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