Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple expectations on the same pipeline #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions lib/bypass.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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.
Expand All @@ -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).
Expand All @@ -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).
Expand All @@ -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.
Expand Down
95 changes: 95 additions & 0 deletions test/bypass_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down