fix(Proxy): don't crash for responses with no body.

This commit is contained in:
James Harton 2024-08-27 15:48:23 +12:00
parent de29cb2b51
commit a191077cc4
Signed by: james
GPG key ID: 90E82DAA13F624F4
2 changed files with 14 additions and 4 deletions

View file

@ -244,10 +244,20 @@ defmodule Wayfarer.Server.Proxy do
end
end
defp handle_responses(conn, [{:done, req} | _], mint, req) do
{:ok, Conn.halt(conn), mint}
# If the connection is done without sending any body content, then we need to
# send the respond and halt the conn.
defp handle_responses(conn, [{:done, req} | _], mint, req) when conn.state == :unset do
conn =
conn
|> Conn.send_resp(conn.status, "")
|> Conn.halt()
{:ok, conn, mint}
end
defp handle_responses(conn, [{:done, req} | _], mint, req) when conn.state == :chunked,
do: {:ok, Conn.halt(conn), mint}
defp handle_responses(conn, [{:error, req, reason} | _], _mint, req), do: {:error, conn, reason}
defp send_request(conn, mint) do

View file

@ -55,7 +55,7 @@ defmodule Wayfarer.Server.ProxyTest do
{:ok, mint, req}
end)
|> stub(:stream, fn mint, :ignore -> {:ok, mint, responses} end)
|> stub(:stream, fn mint, _ -> {:ok, mint, [{:done, req}]} end)
|> stub(:stream, fn mint, _ -> {:ok, mint, [{:status, req, 200}, {:done, req}]} end)
end
describe "request/2" do
@ -79,7 +79,7 @@ defmodule Wayfarer.Server.ProxyTest do
{:ok, mint, req}
end)
|> expect(:stream, fn mint, :ignore ->
{:ok, mint, [{:done, req}]}
{:ok, mint, [{:status, req, 200}, {:done, req}]}
end)
assert conn = Proxy.request(conn, target)