From 18d9379fec3ab13f668efde9310d5e928dd08b02 Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Mon, 19 Feb 2024 11:27:30 +0100 Subject: [PATCH] `Plug.Test.json/2`: Don't crash compilation when Plug is not available Closes #306 --- lib/req/test.ex | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/lib/req/test.ex b/lib/req/test.ex index 743cfb6c..43dc8a5a 100644 --- a/lib/req/test.ex +++ b/lib/req/test.ex @@ -73,6 +73,8 @@ defmodule Req.Test do end """ + require Logger + @ownership Req.Ownership @doc """ @@ -90,22 +92,38 @@ defmodule Req.Test do iex> resp.body %{"celsius" => 25.0} """ - def json(%Plug.Conn{} = conn, data) do - send_resp(conn, conn.status || 200, "application/json", Jason.encode_to_iodata!(data)) - end + def json(conn, data) - defp send_resp(conn, default_status, default_content_type, body) do - conn - |> ensure_resp_content_type(default_content_type) - |> Plug.Conn.send_resp(conn.status || default_status, body) - end + if Code.ensure_loaded?(Plug.Test) do + def json(%Plug.Conn{} = conn, data) do + send_resp(conn, conn.status || 200, "application/json", Jason.encode_to_iodata!(data)) + end - defp ensure_resp_content_type(%Plug.Conn{resp_headers: resp_headers} = conn, content_type) do - if List.keyfind(resp_headers, "content-type", 0) do + defp send_resp(conn, default_status, default_content_type, body) do conn - else - content_type = content_type <> "; charset=utf-8" - %Plug.Conn{conn | resp_headers: [{"content-type", content_type} | resp_headers]} + |> ensure_resp_content_type(default_content_type) + |> Plug.Conn.send_resp(conn.status || default_status, body) + end + + defp ensure_resp_content_type(%Plug.Conn{resp_headers: resp_headers} = conn, content_type) do + if List.keyfind(resp_headers, "content-type", 0) do + conn + else + content_type = content_type <> "; charset=utf-8" + %Plug.Conn{conn | resp_headers: [{"content-type", content_type} | resp_headers]} + end + end + else + def json(_conn, _data) do + Logger.error(""" + Could not find plug dependency. + + Please add :plug to your dependencies: + + {:plug, "~> 1.0"} + """) + + raise "missing plug dependency" end end