Skip to content

Commit

Permalink
Add request! function
Browse files Browse the repository at this point in the history
  • Loading branch information
jwien001 committed Mar 30, 2023
1 parent eb685ed commit 54938d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/finch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,22 @@ defmodule Finch do
end
end

@doc """
Sends an HTTP request and returns a `Finch.Response` struct
or raises an exception in case of failure.
See `request/3` for more detailed information.
"""
@spec request!(Request.t(), name(), keyword()) ::
Response.t()
def request!(%Request{} = req, name, opts \\ []) do
with {:ok, resp} <- request(req, name, opts) do
resp
else
{:error, exception} -> raise exception
end
end

# Catch-all for backwards compatibility below
def request(name, method, url) do
request(name, method, url, [])
Expand Down
24 changes: 24 additions & 0 deletions test/finch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,30 @@ defmodule FinchTest do
end
end

describe "request!/3" do
test "returns response on successful request", %{bypass: bypass, finch_name: finch_name} do
start_supervised!({Finch, name: finch_name})
query_string = "query=value"

Bypass.expect_once(bypass, "GET", "/", fn conn ->
assert conn.query_string == query_string
Plug.Conn.send_resp(conn, 200, "OK")
end)

assert %{status: 200} =
Finch.build(:get, endpoint(bypass, "?" <> query_string))
|> Finch.request(finch_name)
end

test "raises exception on bad request", %{finch_name: finch_name} do
start_supervised!({Finch, name: finch_name})

assert_raise(Exception, fn ->
Finch.build(:get, "http://idontexist.wat") |> Finch.request(finch_name)
end)
end
end

describe "connection options" do
test "are passed through to the conn", %{bypass: bypass} do
expect_any(bypass)
Expand Down

0 comments on commit 54938d3

Please sign in to comment.