mirror of
https://github.com/plausible/analytics.git
synced 2024-12-24 18:12:44 +03:00
117eef000d
* 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>
55 lines
1.4 KiB
Elixir
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
|