{#- Once-per-suite mock-request-count helper (alef issue #443).
Context variables (see src/e2e/codegen/mock_assertions/helper.rs for the full contract):
- helper_name: function name
- total_path: mock server's total-count endpoint path
- one_path: mock server's single-key endpoint path
Emits ONE private function (`defp`) using Erlang's built-in `:httpc` (the OTP `inets`
application), never `Req`/`Finch`: this backend's mix.exs only declares those when the suite
has HTTP or mock-server fixtures (`elixir/project.rs::render_mix_exs`), but a `mock.*`
assertion can appear on a fixture that trips neither flag, so the helper cannot assume either
is a declared dependency. `:inets`/`:httpc` ship with every Erlang/OTP install, so this adds no
new mix dependency at all. `Application.ensure_all_started/1` is idempotent -- safe to call on
every invocation even once already started. Callers build `path_and_query` from
"{{ total_path }}?prefix=<p>" (mock.requests.total) or "{{ one_path }}?key=<k>"
(mock.requests["<k>"]) -- see mock_assertions::model::MockQuery. Appended directly to the
enclosing `ExUnit.Case` module body, so every `test` block in the file can call it directly.
#}
defp {{ helper_name }}(base_url, path_and_query) do
{:ok, _} = Application.ensure_all_started(:inets)
url = String.to_charlist(base_url <> path_and_query)
case :httpc.request(:get, {url, []}, [], []) do
{:ok, result} ->
{status_line, _headers, body} = result
{_protocol, status, _reason} = status_line
if status == 200 do
body
|> List.to_string()
|> String.trim()
|> String.to_integer()
else
raise "{{ helper_name }}: unexpected HTTP status #{status}"
end
{:error, reason} ->
raise "{{ helper_name }}: request failed: #{inspect(reason)}"
end
end