Skip to content

Commit db1e88b

Browse files
authored
Merge pull request #75 from freshlineapp/more-robust-host-config
feat: more robust host config
2 parents 3cf0968 + 25c1363 commit db1e88b

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

lib/elixir_auth_google.ex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,22 @@ defmodule ElixirAuthGoogle do
2525
`get_baseurl_from_conn/1` derives the base URL from the conn struct
2626
"""
2727
@spec get_baseurl_from_conn(conn) :: String.t()
28-
def get_baseurl_from_conn(%{host: h, port: p}) when h == "localhost" do
29-
"http://#{h}:#{p}"
28+
def get_baseurl_from_conn(%{host: h, port: p, scheme: s}) when p != 80 do
29+
"#{Atom.to_string(s)}://#{h}:#{p}"
3030
end
3131

32-
def get_baseurl_from_conn(%{host: h}) do
33-
"https://#{h}"
32+
def get_baseurl_from_conn(%{host: h, scheme: s}) do
33+
"#{Atom.to_string(s)}://#{h}"
34+
end
35+
36+
def get_baseurl_from_conn(%{host: h} = conn) do
37+
scheme =
38+
case h do
39+
"localhost" -> :http
40+
_ -> :https
41+
end
42+
43+
get_baseurl_from_conn(Map.put(conn, :scheme, scheme))
3444
end
3545

3646
@doc """

test/elixir_auth_google_test.exs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,56 @@ defmodule ElixirAuthGoogleTest do
55

66
import Mock
77

8-
test "get_baseurl_from_conn(conn) detects the URL based on conn.host" do
8+
test "get_baseurl_from_conn(conn) detects the URL based on conn.host HTTP" do
99
conn = %{
1010
host: "localhost",
11-
port: 4000
11+
port: 4000,
12+
scheme: :http
1213
}
1314

1415
assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "http://localhost:4000"
1516
end
1617

17-
test "get_baseurl_from_conn(conn) detects the URL for production" do
18+
test "get_baseurl_from_conn(conn) detects the URL based on conn.host HTTPS" do
19+
conn = %{
20+
host: "localhost",
21+
port: 4000,
22+
scheme: :https
23+
}
24+
25+
assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "https://localhost:4000"
26+
end
27+
28+
test "get_baseurl_from_conn(conn) detects the URL for production HTTPS" do
1829
conn = %{
1930
host: "dwyl.com",
20-
port: 80
31+
port: 80,
32+
scheme: :https
2133
}
2234

2335
assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "https://dwyl.com"
2436
end
2537

38+
test "get_baseurl_from_conn(conn) detects the URL for production HTTP" do
39+
conn = %{
40+
host: "dwyl.com",
41+
port: 80,
42+
scheme: :http
43+
}
44+
45+
assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "http://dwyl.com"
46+
end
47+
48+
test "get_baseurl_from_conn(conn) detects the URL for production HTTPS, non-standard port" do
49+
conn = %{
50+
host: "dwyl.com",
51+
port: 8080,
52+
scheme: :https
53+
}
54+
55+
assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "https://dwyl.com:8080"
56+
end
57+
2658
test "get Google login url" do
2759
conn = %{
2860
host: "localhost",

0 commit comments

Comments
 (0)