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

add 4 new predicates #2

Merged
merged 4 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,22 @@ If the above example, we only want to expose our Prometheus metrics if the reque

Unplug provides the following predicates out of the box:

| Predicate | Description |
| ------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
| `Unplug.Predicates.AppConfigEquals` | Given an application and a key, execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.AppConfigNotEquals` | Given an application and a key, do not execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.EnvVarEquals` | Given an environment variable, execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.EnvVarNotEquals` | Given an environment variable, do not execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.RequestHeaderEquals` | Given a request header, execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestHeaderNotEquals` | Given a request header, do not execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestPathEquals` | Given a request path, execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestPathNotEquals` | Given a request path, do not execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestPathIn` | Given a request path, execute the plug if the request value is in the the provided list of values |
| `Unplug.Predicates.RequestPathNotIn` | Given a request path, do not execute the plug if the request value is in the the provided list of values |
| Predicate | Description |
| ------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------- |
| `Unplug.Predicates.AppConfigEquals` | Given an application and a key, execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.AppConfigNotEquals` | Given an application and a key, do not execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.AppConfigIn` | Given an application and a key, execute the plug if the configured value is in the provided enumerable of values |
| `Unplug.Predicates.AppConfigNotIn` | Given an application and a key, execute the plug if the configured value is not in the provided enumerable of values |
| `Unplug.Predicates.EnvVarEquals` | Given an environment variable, execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.EnvVarNotEquals` | Given an environment variable, do not execute the plug if the configured value matches the expected value |
| `Unplug.Predicates.EnvVarIn` | Given an environment variable, execute the plug if the environment variable value is in the provided enumerable of values |
| `Unplug.Predicates.EnvVarNotIn` | Given an environment variable, execute the plug if the environment variable value is not in the provided enumerable of values |
| `Unplug.Predicates.RequestHeaderEquals` | Given a request header, execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestHeaderNotEquals` | Given a request header, do not execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestPathEquals` | Given a request path, execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestPathNotEquals` | Given a request path, do not execute the plug if the request value matches the expected value |
| `Unplug.Predicates.RequestPathIn` | Given a request path, execute the plug if the request value is in the the provided enumerable of values |
| `Unplug.Predicates.RequestPathNotIn` | Given a request path, do not execute the plug if the request value is in the the provided enumerable of values |

## Writing Your Own Predicates

Expand Down
20 changes: 20 additions & 0 deletions lib/unplug/predicates/app_config_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.AppConfigIn do
@moduledoc """
Given an application and a key, execute the plug if the configured value
is in the provided enumerable of values.

Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.AppConfigIn, {:my_app, :some_config, [:enabled, :enabled_again]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(_conn, {app, key, expected_values}) do
Application.get_env(app, key) in expected_values
end
end
20 changes: 20 additions & 0 deletions lib/unplug/predicates/app_config_not_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.AppConfigNotIn do
@moduledoc """
Given an application and a key, execute the plug if the configured value
is not in the provided enumerable of values.

Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.AppConfigNotIn, {:my_app, :some_config, [:enabled, :enabled_again]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(conn, opts) do
not Unplug.Predicates.AppConfigIn.call(conn, opts)
end
end
20 changes: 20 additions & 0 deletions lib/unplug/predicates/env_var_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.EnvVarIn do
@moduledoc """
Given an environment variable, execute the plug if the environment
variable value is in the provided enumerable of values.

Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.EnvVarIn, {"ENABLE_DEBUG", ["true", "1"]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(_conn, {env_var, expected_values}) do
System.get_env(env_var) in expected_values
end
end
20 changes: 20 additions & 0 deletions lib/unplug/predicates/env_var_not_in.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Unplug.Predicates.EnvVarNotIn do
@moduledoc """
Given an environment variable, execute the plug if the environment
variable value is not in the provided enumerable of values.

Usage:
```elixir
plug Unplug,
if: {Unplug.Predicates.EnvVarNotIn, {"ENABLE_DEBUG", ["true", "1"]}},
do: MyApp.Plug
```
"""

@behaviour Unplug.Predicate

@impl true
def call(conn, opts) do
not Unplug.Predicates.EnvVarIn.call(conn, opts)
end
end
2 changes: 1 addition & 1 deletion lib/unplug/predicates/request_path_in.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Unplug.Predicates.RequestPathIn do
@moduledoc """
Given a request path, execute the plug if the request value is in
the the provided list of values.
the the provided enumerable of values.

Usage:
```elixir
Expand Down
2 changes: 1 addition & 1 deletion lib/unplug/predicates/request_path_not_in.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Unplug.Predicates.RequestPathNotIn do
@moduledoc """
Given a request path, do not execute the plug if the request value is
in the the provided list of values.
in the the provided enumerable of values.

Usage:
```elixir
Expand Down
27 changes: 27 additions & 0 deletions test/predicates/app_config_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Unplug.Predicates.AppConfigInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return true if the application config key is in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
Application.put_env(:unplug, :app_config_test_two, "some_other_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.AppConfigIn.call(
conn,
{:unplug, :app_config_test, ["some_config_string", "some_other_string"]}
)

assert Unplug.Predicates.AppConfigIn.call(
conn,
{:unplug, :app_config_test_two, ["some_config_string", "some_other_config_string"]}
)
end

test "should return false if the application config key is NOT in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.AppConfigIn.call(conn, {:unplug, :app_config_test, ["invalid"]})
end
end
27 changes: 27 additions & 0 deletions test/predicates/app_config_not_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Unplug.Predicates.AppConfigNotInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return true if the application config key is NOT in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
Application.put_env(:unplug, :app_config_test_two, "some_other_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.AppConfigNotIn.call(
conn,
{:unplug, :app_config_test, ["some_config_string", "some_other_string"]}
)

refute Unplug.Predicates.AppConfigNotIn.call(
conn,
{:unplug, :app_config_test_two, ["some_config_string", "some_other_config_string"]}
)
end

test "should return false if the application config key is in the list" do
Application.put_env(:unplug, :app_config_test, "some_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.AppConfigNotIn.call(conn, {:unplug, :app_config_test, ["invalid"]})
end
end
18 changes: 18 additions & 0 deletions test/predicates/env_var_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Unplug.Predicates.EnvVarInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return true if the env var is in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.EnvVarIn.call(conn, {"UNPLUG_ENV_VAR", ["some_config_string", "some_other_config_string"]})
end

test "should return false if the env var is not in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.EnvVarIn.call(conn, {"UNPLUG_ENV_VAR", ["invalid", "still_invalid"]})
end
end
21 changes: 21 additions & 0 deletions test/predicates/env_var_not_in_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Unplug.Predicates.EnvVarNotInTest do
use ExUnit.Case, async: false
use Plug.Test

test "should return false if the env var is in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

refute Unplug.Predicates.EnvVarNotIn.call(
conn,
{"UNPLUG_ENV_VAR", ["some_config_string", "some_other_config_string"]}
)
end

test "should return true if the env var is not in the list of acceptable keys" do
System.put_env("UNPLUG_ENV_VAR", "some_config_string")
conn = conn(:get, "/")

assert Unplug.Predicates.EnvVarNotIn.call(conn, {"UNPLUG_ENV_VAR", ["invalid", "still_invalid"]})
end
end