Skip to content

Commit

Permalink
put_plug: Do not leak Plug.Test messages, closes #316
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Mar 5, 2024
1 parent 3023ac9 commit 7c04f79
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1203,11 +1203,25 @@ defmodule Req.Steps do
conn = Plug.Test.conn(request.method, request.url, req_body)

conn = put_in(conn.req_headers, req_headers)
conn = call_plug(conn, plug)

# consume messages sent by Plug.Test adapter
{_, %{ref: ref}} = conn.adapter

receive do
{^ref, {_status, _headers, _body}} -> :ok
after
0 -> :ok
end

receive do
{:plug_conn, :sent} -> :ok
after
0 -> :ok
end

case request.into do
nil ->
conn = call_plug(conn, plug)

response =
Req.Response.new(
status: conn.status,
Expand All @@ -1218,8 +1232,6 @@ defmodule Req.Steps do
{request, response}

fun when is_function(fun, 2) ->
conn = call_plug(conn, plug)

response =
Req.Response.new(
status: conn.status,
Expand All @@ -1243,7 +1255,6 @@ defmodule Req.Steps do

collectable ->
{acc, collector} = Collectable.into(collectable)
conn = call_plug(conn, plug)

response =
Req.Response.new(
Expand Down
20 changes: 20 additions & 0 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,7 @@ defmodule Req.StepsTest do
end

assert Req.request!(plug: plug, json: %{a: 1}).body == "ok"
refute_receive _
end

test "request stream" do
Expand All @@ -1649,6 +1650,7 @@ defmodule Req.StepsTest do
)

assert Req.request!(req).body == "foofoo"
refute_receive _
end

test "into: fun" do
Expand All @@ -1669,6 +1671,7 @@ defmodule Req.StepsTest do
resp = Req.request!(req)
assert resp.status == 200
assert resp.body == "foobar"
refute_receive _
end

test "into: fun with halt" do
Expand All @@ -1690,6 +1693,7 @@ defmodule Req.StepsTest do
assert output =~ ~r/returning {:halt, acc} is not yet supported by Plug adapter/
assert resp.status == 200
assert resp.body == "foobar"
refute_receive _
end

test "into: collectable" do
Expand All @@ -1707,6 +1711,7 @@ defmodule Req.StepsTest do
resp = Req.request!(req)
assert resp.status == 200
assert resp.body == ["foobar"]
refute_receive _
end

test "into: collectable with send_resp" do
Expand All @@ -1721,6 +1726,7 @@ defmodule Req.StepsTest do
resp = Req.request!(req)
assert resp.status == 200
assert resp.body == ["foo"]
refute_receive _
end

test "into: collectable with send_file" do
Expand All @@ -1735,6 +1741,20 @@ defmodule Req.StepsTest do
resp = Req.request!(req)
assert resp.status == 200
assert ["defmodule Req.MixProject do" <> _] = resp.body
refute_receive _
end

# TODO
@tag :skip
test "inform", c do
Bypass.expect(c.bypass, fn conn ->
conn
|> Plug.Conn.inform(100)
|> Plug.Conn.send_resp(200, "ok")
end)

assert Req.put!(c.url, body: "foo", headers: [expect: "100-continue"]).status == 200
refute_receive _
end
end

Expand Down

0 comments on commit 7c04f79

Please sign in to comment.