Skip to content

Commit

Permalink
Start using Bandit in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Mar 12, 2024
1 parent 27d4cc8 commit ba093d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ defmodule Req.MixProject do
{:brotli, "~> 0.3.1", optional: true},
{:ezstd, "~> 1.0", optional: true},
{:bypass, "~> 2.1", only: :test},
{:ex_doc, ">= 0.0.0", only: :docs}
{:ex_doc, ">= 0.0.0", only: :docs},
{:bandit, "~> 1.0", only: :test}
]
end

Expand Down
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%{
"aws_signature": {:hex, :aws_signature, "0.3.2", "adf33bc4af00b2089b7708bf20e3246f09c639a905a619b3689f0a0a22c3ef8f", [:rebar3], [], "hexpm", "b0daf61feb4250a8ab0adea60db3e336af732ff71dd3fb22e45ae3dcbd071e44"},
"bandit": {:hex, :bandit, "1.2.3", "a98d664a96fec23b68e776062296d76a94b4459795b38209f4ae89cb4225709c", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "3e29150245a9b5f56944434e5240966e75c917dad248f689ab589b32187a81af"},
"brotli": {:hex, :brotli, "0.3.2", "59cf45a399098516f1d34f70d8e010e5c9bf326659d3ef34c7cc56793339002b", [:rebar3], [], "hexpm", "9ec3ef9c753f80d0c657b4905193c55e5198f169fa1d1c044d8601d4d931a2ad"},
"bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"},
"castore": {:hex, :castore, "1.0.5", "9eeebb394cc9a0f3ae56b813459f990abb0a3dedee1be6b27fdb50301930502f", [:mix], [], "hexpm", "8d7c597c3e4a64c395980882d4bca3cebb8d74197c590dc272cfd3b6a6310578"},
Expand Down Expand Up @@ -27,4 +28,6 @@
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"thousand_island": {:hex, :thousand_island, "1.3.5", "6022b6338f1635b3d32406ff98d68b843ba73b3aa95cfc27154223244f3a6ca5", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2be6954916fdfe4756af3239fb6b6d75d0b8063b5df03ba76fd8a4c87849e180"},
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
}
54 changes: 32 additions & 22 deletions test/req/steps_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,41 @@ defmodule Req.StepsTest do
end

describe "put_base_url" do
test "it works", c do
Bypass.expect(c.bypass, "GET", "", fn conn ->
Plug.Conn.send_resp(conn, 200, "ok")
end)
test "it works" do
%{url: url} =
start_server(fn conn ->
Plug.Conn.send_resp(conn, 200, "ok")
end)

assert Req.get!("/", base_url: c.url).body == "ok"
assert Req.get!("", base_url: c.url).body == "ok"
assert Req.get!("/", base_url: url).body == "ok"
assert Req.get!("", base_url: url).body == "ok"

req = Req.new(base_url: c.url)
req = Req.new(base_url: url)
assert Req.get!(req, url: "/").body == "ok"
assert Req.get!(req, url: "").body == "ok"
end

test "with absolute url", c do
Bypass.expect(c.bypass, "GET", "/", fn conn ->
Plug.Conn.send_resp(conn, 200, "ok")
end)
test "with absolute url" do
%{url: url} =
start_server(fn conn ->
Plug.Conn.send_resp(conn, 200, "ok")
end)

assert Req.get!(c.url, base_url: "ignored").body == "ok"
assert Req.get!(url, base_url: "ignored").body == "ok"
end

test "with base path", c do
Bypass.expect(c.bypass, "GET", "/api/v2/foo", fn conn ->
assert conn.request_path == "/api/v2/foo"
Plug.Conn.send_resp(conn, 200, "ok")
end)
test "with base path" do
%{url: url} =
start_server(fn conn ->
assert conn.request_path == "/api/v2/foo"
Plug.Conn.send_resp(conn, 200, "ok")
end)

assert Req.get!("/foo", base_url: c.url <> "/api/v2", retry: false).body == "ok"
assert Req.get!("foo", base_url: c.url <> "/api/v2").body == "ok"
assert Req.get!("/foo", base_url: c.url <> "/api/v2/").body == "ok"
assert Req.get!("foo", base_url: c.url <> "/api/v2/").body == "ok"
assert Req.get!("", base_url: c.url <> "/api/v2/foo").body == "ok"
assert Req.get!("/foo", base_url: url <> "/api/v2", retry: false).body == "ok"
assert Req.get!("foo", base_url: url <> "/api/v2").body == "ok"
assert Req.get!("/foo", base_url: url <> "/api/v2/").body == "ok"
assert Req.get!("foo", base_url: url <> "/api/v2/").body == "ok"
assert Req.get!("", base_url: url <> "/api/v2/foo").body == "ok"
end

test "function" do
Expand Down Expand Up @@ -2125,4 +2128,11 @@ defmodule Req.StepsTest do
assert :ok = Req.cancel_async_response(resp)
end
end

defp start_server(plug) do
plug = fn conn, _ -> plug.(conn) end
pid = start_supervised!({Bandit, scheme: :http, port: 0, startup_log: false, plug: plug})
{:ok, {_ip, port}} = ThousandIsland.listener_info(pid)
%{pid: pid, url: "http://localhost:#{port}"}
end
end

0 comments on commit ba093d8

Please sign in to comment.