Skip to content

Commit

Permalink
redirect: Cancel async request before redirecting
Browse files Browse the repository at this point in the history
Closes #372
  • Loading branch information
wojtekmach committed Jun 28, 2024
1 parent f327e2e commit 3cdaafc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/req/finch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ defmodule Req.Finch do

defp finch_cancel(ref) do
Finch.cancel_async_request(ref)
clean_responses(ref)
:ok
end

defp clean_responses(ref) do
receive do
{^ref, _} -> clean_responses(ref)
after
0 -> :ok
end
end

defp finch_name(request) do
Expand Down
4 changes: 4 additions & 0 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,10 @@ defmodule Req.Steps do
redirect_count = Req.Request.get_private(request, :req_redirect_count, 0)

if redirect_count < max_redirects do
with %Req.Response.Async{} <- response.body do
Req.cancel_async_response(response)
end

request =
request
|> build_redirect_request(response, location)
Expand Down
23 changes: 23 additions & 0 deletions test/req/finch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,29 @@ defmodule Req.FinchTest do
assert resp.status == 200
assert :ok = Req.cancel_async_response(resp)
end

@tag :capture_log
test "into: :self with redirect" do
%{url: url} =
TestHelper.start_http_server(fn conn ->
Plug.Conn.send_resp(conn, 200, "ok")
end)

%{url: url} =
TestHelper.start_http_server(fn conn ->
conn
|> Plug.Conn.put_resp_header("location", to_string(url))
|> Plug.Conn.send_resp(307, "redirecting to #{url}")
end)

req =
Req.new(
url: url,
into: :self
)

assert Req.get!(req).body |> Enum.to_list() == ["ok"]
end
end

describe "pool_options" do
Expand Down

0 comments on commit 3cdaafc

Please sign in to comment.