Skip to content

Commit

Permalink
Merge pull request #219 from jwien001/main
Browse files Browse the repository at this point in the history
Add request! function
  • Loading branch information
sneako authored Apr 11, 2023
2 parents eb685ed + 7b79562 commit a345a78
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
otp: '25.x'
lint: lint

runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- uses: actions/cache@v2
Expand Down
15 changes: 15 additions & 0 deletions lib/finch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,19 @@ defmodule Finch do
build(method, url, headers, body)
|> request(name, opts)
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
case request(req, name, opts) do
{:ok, resp} -> resp
{:error, exception} -> raise exception
end
end
end
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(Mint.TransportError, 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 a345a78

Please sign in to comment.