Skip to content

Commit

Permalink
Extend possible check origins (#102)
Browse files Browse the repository at this point in the history
* Extend possible check origins

* Add more test cases for read_check_origin
  • Loading branch information
Rados13 authored Oct 11, 2023
1 parent 5b38ff7 commit 63783c1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ config :jellyfish, JellyfishWeb.Endpoint,
http: [ip: ip, port: port],
url: [host: host_name, port: host_port]

check_origin = ConfigReader.read_boolean("JF_CHECK_ORIGIN")
check_origin = ConfigReader.read_check_origin("JF_CHECK_ORIGIN")

if check_origin != nil do
config :jellyfish, JellyfishWeb.Endpoint, check_origin: check_origin
Expand Down
17 changes: 15 additions & 2 deletions lib/jellyfish/config_reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ defmodule Jellyfish.ConfigReader do
end
end

def read_boolean(env) do
def read_check_origin(env) do
read_boolean(env, fn
":conn" ->
:conn

value ->
String.split(value, " ")
end)
end

def read_boolean(env, fallback \\ nil) do
if value = System.get_env(env) do
case String.downcase(value) do
"true" ->
Expand All @@ -59,8 +69,11 @@ defmodule Jellyfish.ConfigReader do
"false" ->
false

_other ->
_other when is_nil(fallback) ->
raise "Bad #{env} environment variable value. Expected true or false, got: #{value}"

_other ->
fallback.(value)
end
end
end
Expand Down
30 changes: 29 additions & 1 deletion test/jellyfish/config_reader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Jellyfish.ConfigReaderTest do
alias Jellyfish.ConfigReader

defmacrop with_env(env, do: body) do
# get current env(s) value(s),
# get current env(s) value(s),
# execute test code,
# put back original env(s) value(s)
#
Expand Down Expand Up @@ -71,6 +71,34 @@ defmodule Jellyfish.ConfigReaderTest do
end
end

test "read_check_origin/1" do
env_name = "JF_CHECK_ORIGIN"

with_env env_name do
System.put_env(env_name, "false")
assert ConfigReader.read_check_origin(env_name) == false
System.put_env(env_name, "true")
assert ConfigReader.read_check_origin(env_name) == true
System.put_env(env_name, "jellyfish.ovh jellyfish2.ovh jellyfish3.ovh")

assert ConfigReader.read_check_origin(env_name) == [
"jellyfish.ovh",
"jellyfish2.ovh",
"jellyfish3.ovh"
]

# Case from phoenix documentation
System.put_env(env_name, "//another.com:888 //*.other.com")
assert ConfigReader.read_check_origin(env_name) == ["//another.com:888", "//*.other.com"]

System.put_env(env_name, "localhost")
assert ConfigReader.read_check_origin(env_name) == ["localhost"]

System.put_env(env_name, ":conn")
assert ConfigReader.read_check_origin(env_name) == :conn
end
end

test "read_port_range/1" do
env_name = "JF_CONF_READER_TEST_PORT_RANGE"

Expand Down

0 comments on commit 63783c1

Please sign in to comment.