Skip to content

Commit

Permalink
run_plug: Make public
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Mar 16, 2024
1 parent 6644cf4 commit 18b3429
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

* [`run_finch`]: Set `inet6: true` if URL looks like IPv6 address.

* [`put_plug`]: Add support for simulating network issues using [`Req.Test.transport_error/2`].
* [`put_plug`]: Move most documentation to [`run_plug`].

* [`run_plug`]: Make public.

* [`run_plug`]: Add support for simulating network issues using [`Req.Test.transport_error/2`].

* [`put_aws_sigv4`]: Drop `:aws_signature` dependency.

Expand Down Expand Up @@ -863,6 +867,7 @@ See "Adapter" section in `Req.Request` module documentation for more information
[`put_params`]: https://hexdocs.pm/req/Req.Steps.html#put_params/1
[`put_path_params`]: https://hexdocs.pm/req/Req.Steps.html#put_path_params/1
[`put_plug`]: https://hexdocs.pm/req/Req.Steps.html#put_plug/1
[`run_plug`]: https://hexdocs.pm/req/Req.Steps.html#run_plug/1
[`put_user_agent`]: https://hexdocs.pm/req/Req.Steps.html#put_user_agent/1
[`put_range`]: https://hexdocs.pm/req/Req.Steps.html#put_range/1
[`retry`]: https://hexdocs.pm/req/Req.Steps.html#retry/1
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ write new ones.

* Easily create test stubs (see [`Req.Test`].)

* Running against a plug (via [`put_plug`] step.)
* Running against a plug (via [`run_plug`] step.)

* Pluggable adapters. By default, Req uses [Finch] (via [`run_finch`] step.)

Expand Down Expand Up @@ -257,7 +257,7 @@ limitations under the License.
[`put_base_url`]: https://hexdocs.pm/req/Req.Steps.html#put_base_url/1
[`put_params`]: https://hexdocs.pm/req/Req.Steps.html#put_params/1
[`put_path_params`]: https://hexdocs.pm/req/Req.Steps.html#put_path_params/1
[`put_plug`]: https://hexdocs.pm/req/Req.Steps.html#put_plug/1
[`run_plug`]: https://hexdocs.pm/req/Req.Steps.html#run_plug/1
[`put_range`]: https://hexdocs.pm/req/Req.Steps.html#put_range/1
[`put_user_agent`]: https://hexdocs.pm/req/Req.Steps.html#put_user_agent/1
[`retry`]: https://hexdocs.pm/req/Req.Steps.html#retry/1
Expand Down
6 changes: 4 additions & 2 deletions lib/req.ex
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,11 @@ defmodule Req do
Request adapters:
* `:adapter` - adapter to use to make the actual HTTP request. See `:adapter` field description
in the `Req.Request` module documentation for more information. Defaults to calling [`run_finch`](`Req.Steps.run_finch/1`).
in the `Req.Request` module documentation for more information.
* `:plug` - if set, calls the given Plug instead of making an HTTP request over the network (via [`put_plug`](`Req.Steps.put_plug/1`) step).
The default is [`run_finch`](`Req.Steps.run_finch/1`).
* `:plug` - if set, calls the given plug instead of making an HTTP request over the network (via [`run_plug`](`Req.Steps.run_plug/1`) step).
Finch options ([`run_finch`](`Req.Steps.run_finch/1`) step)
Expand Down
43 changes: 31 additions & 12 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1088,12 +1088,38 @@ defmodule Req.Steps do
end

@doc """
Runs the request against a plug instead of over the network.
Sets adapter to `run_plug/1`.
See `run_plug/1` for more information.
## Request Options
* `:plug` - if set, the plug to run the request against.
"""
@doc step: :request
def put_plug(request) do
if request.options[:plug] do
%{request | adapter: &run_plug/1}
else
request
end
end

@doc """
Runs the request against a plug instead of over the network.
This step is a Req _adapter_. It is set as the adapter by the `put_plug/1` step
if the `:plug` option is set.
It requires [`:plug`](https://hexdocs.pm/plug) dependency:
{:plug, "~> 1.0"}
## Request Options
* `:plug` - the plug to run the request against.
## Examples
This step is particularly useful to test plugs:
Expand Down Expand Up @@ -1162,18 +1188,11 @@ defmodule Req.Steps do
{:error, %Req.TransportError{reason: :timeout}}
end
"""
@doc step: :request
def put_plug(request) do
if request.options[:plug] do
%{request | adapter: &run_plug/1}
else
request
end
end
def run_plug(request)

if Code.ensure_loaded?(Plug.Test) do
defp run_plug(request) do
plug = request.options[:plug]
def run_plug(request) do
plug = request.options.plug

req_body =
case request.body do
Expand Down Expand Up @@ -1285,7 +1304,7 @@ defmodule Req.Steps do
plug.(conn)
end
else
defp run_plug(_request) do
def run_plug(_request) do
Logger.error("""
Could not find plug dependency.
Expand Down

0 comments on commit 18b3429

Please sign in to comment.