-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Gladear/add-composition-predicates
add composition predicates
- Loading branch information
Showing
6 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Unplug.Compose.All do | ||
@moduledoc """ | ||
Given a list of predicates, execute the plug if all of the predicates return | ||
true. | ||
Usage: | ||
```elixir | ||
plug Unplug, | ||
if: {Unplug.Compose.All, [ | ||
{Unplug.Predicates.AppConfigEquals, {:my_app, :some_config, :enabled}}, | ||
MyApp.CustomPredicate | ||
]}, | ||
do: MyApp.Plug | ||
``` | ||
""" | ||
|
||
@behaviour Unplug.Predicate | ||
|
||
@impl true | ||
def call(conn, predicates) do | ||
Enum.all?(predicates, fn | ||
{module, opts} -> module.call(conn, opts) | ||
module when is_atom(module) -> module.call(conn, []) | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Unplug.Compose.Any do | ||
@moduledoc """ | ||
Given a list of predicates, execute the plug if any of the predicates return | ||
true. | ||
Usage: | ||
```elixir | ||
plug Unplug, | ||
if: {Unplug.Compose.Any, [ | ||
{Unplug.Predicates.AppConfigEquals, {:my_app, :some_config, :enabled}}, | ||
MyApp.CustomPredicate | ||
]}, | ||
do: MyApp.Plug | ||
``` | ||
""" | ||
|
||
@behaviour Unplug.Predicate | ||
|
||
@impl true | ||
def call(conn, predicates) do | ||
Enum.any?(predicates, fn | ||
{module, opts} -> module.call(conn, opts) | ||
module when is_atom(module) -> module.call(conn, []) | ||
end) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Unplug.Compose.AllTest do | ||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
|
||
test "should return true if all predicates return true" do | ||
conn = | ||
:get | ||
|> conn("/some_path") | ||
|> put_req_header("x-my-custom-header", "some_config_string") | ||
|
||
assert Unplug.Compose.All.call(conn, [ | ||
Unplug.TestPredicates.AlwaysTrue, | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_path"} | ||
]) | ||
end | ||
|
||
test "should return false if any of the predicates return false" do | ||
conn = | ||
:get | ||
|> conn("/some_path") | ||
|> put_req_header("x-my-custom-header", "some_config_string") | ||
|
||
refute Unplug.Compose.All.call(conn, [ | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_other_path"} | ||
]) | ||
|
||
refute Unplug.Compose.All.call(conn, [ | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_other_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_path"} | ||
]) | ||
|
||
refute Unplug.Compose.All.call(conn, [ | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_other_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_other_path"} | ||
]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule Unplug.Compose.AnyTest do | ||
use ExUnit.Case, async: true | ||
use Plug.Test | ||
|
||
test "should return true if any predicate return true" do | ||
conn = | ||
:get | ||
|> conn("/some_path") | ||
|> put_req_header("x-my-custom-header", "some_config_string") | ||
|
||
assert Unplug.Compose.Any.call(conn, [ | ||
Unplug.TestPredicates.AlwaysTrue, | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_path"} | ||
]) | ||
|
||
assert Unplug.Compose.Any.call(conn, [ | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_other_config_string"}}, | ||
Unplug.TestPredicates.AlwaysTrue, | ||
{Unplug.Predicates.RequestPathEquals, "/some_other_path"} | ||
]) | ||
|
||
assert Unplug.Compose.Any.call(conn, [ | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_other_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_path"} | ||
]) | ||
|
||
assert Unplug.Compose.Any.call(conn, [ | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_other_path"} | ||
]) | ||
end | ||
|
||
test "should return false if all predicates return false" do | ||
conn = | ||
:get | ||
|> conn("/some_path") | ||
|> put_req_header("x-my-custom-header", "some_config_string") | ||
|
||
refute Unplug.Compose.Any.call(conn, [ | ||
{Unplug.Predicates.RequestHeaderEquals, {"x-my-custom-header", "some_other_config_string"}}, | ||
{Unplug.Predicates.RequestPathEquals, "/some_other_path"} | ||
]) | ||
end | ||
end |