diff --git a/lib/bypass.ex b/lib/bypass.ex index 65607dd..9c11399 100644 --- a/lib/bypass.ex +++ b/lib/bypass.ex @@ -151,9 +151,11 @@ defmodule Bypass do end) ``` """ - @spec expect(Bypass.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: :ok - def expect(%Bypass{pid: pid}, fun), - do: Bypass.Instance.call(pid, {:expect, fun}) + @spec expect(Bypass.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: Bypass.t() + def expect(%Bypass{pid: pid} = bypass, fun) do + :ok = Bypass.Instance.call(pid, {:expect, fun}) + bypass + end @doc """ Expects the passed function to be called at least once for the specified route (method and path). @@ -169,9 +171,11 @@ defmodule Bypass do end) ``` """ - @spec expect(Bypass.t(), String.t(), String.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: :ok - def expect(%Bypass{pid: pid}, method, path, fun), - do: Bypass.Instance.call(pid, {:expect, method, path, fun}) + @spec expect(Bypass.t(), String.t(), String.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: Bypass.t() + def expect(%Bypass{pid: pid} = bypass, method, path, fun) do + :ok = Bypass.Instance.call(pid, {:expect, method, path, fun}) + bypass + end @doc """ Expects the passed function to be called exactly once regardless of the route. @@ -184,9 +188,11 @@ defmodule Bypass do end) ``` """ - @spec expect_once(Bypass.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: :ok - def expect_once(%Bypass{pid: pid}, fun), - do: Bypass.Instance.call(pid, {:expect_once, fun}) + @spec expect_once(Bypass.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: Bypass.t() + def expect_once(%Bypass{pid: pid} = bypass, fun) do + :ok = Bypass.Instance.call(pid, {:expect_once, fun}) + bypass + end @doc """ Expects the passed function to be called exactly once for the specified route (method and path). @@ -202,9 +208,12 @@ defmodule Bypass do end) ``` """ - @spec expect_once(Bypass.t(), String.t(), String.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: :ok - def expect_once(%Bypass{pid: pid}, method, path, fun), - do: Bypass.Instance.call(pid, {:expect_once, method, path, fun}) + @spec expect_once(Bypass.t(), String.t(), String.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: + Bypass.t() + def expect_once(%Bypass{pid: pid} = bypass, method, path, fun) do + :ok = Bypass.Instance.call(pid, {:expect_once, method, path, fun}) + bypass + end @doc """ Allows the function to be invoked zero or many times for the specified route (method and path). @@ -220,9 +229,11 @@ defmodule Bypass do end) ``` """ - @spec stub(Bypass.t(), String.t(), String.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: :ok - def stub(%Bypass{pid: pid}, method, path, fun), - do: Bypass.Instance.call(pid, {:stub, method, path, fun}) + @spec stub(Bypass.t(), String.t(), String.t(), (Plug.Conn.t() -> Plug.Conn.t())) :: Bypass.t() + def stub(%Bypass{pid: pid} = bypass, method, path, fun) do + :ok = Bypass.Instance.call(pid, {:stub, method, path, fun}) + bypass + end @doc """ Makes a expection to pass. diff --git a/test/bypass_test.exs b/test/bypass_test.exs index 7a30ea8..87337bd 100644 --- a/test/bypass_test.exs +++ b/test/bypass_test.exs @@ -529,6 +529,101 @@ defmodule BypassTest do bypass end + test "Bypass.expect_once/4 pipe expectations" do + bypass = Bypass.open() + + bypass + |> Bypass.expect_once("POST", "/foo", fn conn -> + assert conn.method == "POST" + assert conn.request_path == "/foo" + Plug.Conn.send_resp(conn, 200, "foo response") + end) + |> Bypass.expect_once("POST", "/bar", fn conn -> + assert conn.method == "POST" + assert conn.request_path == "/bar" + Plug.Conn.send_resp(conn, 200, "bar response") + end) + + capture_log(fn -> + assert {:ok, 200, "foo response"} = request(bypass.port, "/foo") + assert {:ok, 200, "bar response"} = request(bypass.port, "/bar") + end) + end + + test "Bypass.expect/4 pipe expectations" do + bypass = Bypass.open() + + bypass + |> Bypass.expect("POST", "/foo", fn conn -> + assert conn.method == "POST" + assert conn.request_path == "/foo" + Plug.Conn.send_resp(conn, 200, "foo response") + end) + |> Bypass.expect("POST", "/bar", fn conn -> + assert conn.method == "POST" + assert conn.request_path == "/bar" + Plug.Conn.send_resp(conn, 200, "bar response") + end) + + capture_log(fn -> + assert {:ok, 200, "foo response"} = request(bypass.port, "/foo") + assert {:ok, 200, "bar response"} = request(bypass.port, "/bar") + end) + end + + test "Bypass.stub/4 pipe expectations" do + bypass = Bypass.open() + + bypass + |> Bypass.stub("POST", "/foo", fn conn -> + assert conn.method == "POST" + assert conn.request_path == "/foo" + Plug.Conn.send_resp(conn, 200, "foo response") + end) + |> Bypass.stub("POST", "/bar", fn conn -> + assert conn.method == "POST" + assert conn.request_path == "/bar" + Plug.Conn.send_resp(conn, 200, "bar response") + end) + + capture_log(fn -> + assert {:ok, 200, "foo response"} = request(bypass.port, "/foo") + assert {:ok, 200, "bar response"} = request(bypass.port, "/bar") + end) + end + + test "Bypass.expect/2 pipe expectations" do + bypass = Bypass.open() + + bypass + |> Bypass.expect(fn conn -> + Plug.Conn.send_resp(conn, 200, "foo response") + end) + |> Bypass.expect(fn conn -> + Plug.Conn.send_resp(conn, 200, "bar response") + end) + + capture_log(fn -> + assert {:ok, 200, "bar response"} = request(bypass.port, "/bar") + end) + end + + test "Bypass.expect_once/2 pipe expectations" do + bypass = Bypass.open() + + bypass + |> Bypass.expect_once(fn conn -> + Plug.Conn.send_resp(conn, 200, "foo response") + end) + |> Bypass.expect_once(fn conn -> + Plug.Conn.send_resp(conn, 200, "bar response") + end) + + capture_log(fn -> + assert {:ok, 200, "bar response"} = request(bypass.port, "/bar") + end) + end + test "Bypass.verify_expectations! - with ExUnit it will raise an exception" do bypass = Bypass.open()