Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions lib/elixir_auth_google.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ defmodule ElixirAuthGoogle do
`get_baseurl_from_conn/1` derives the base URL from the conn struct
"""
@spec get_baseurl_from_conn(conn) :: String.t()
def get_baseurl_from_conn(%{host: h, port: p}) when h == "localhost" do
"http://#{h}:#{p}"
def get_baseurl_from_conn(%{host: h, port: p, scheme: s}) when p != 80 do
"#{Atom.to_string(s)}://#{h}:#{p}"
end

def get_baseurl_from_conn(%{host: h}) do
"https://#{h}"
def get_baseurl_from_conn(%{host: h, scheme: s}) do
"#{Atom.to_string(s)}://#{h}"
end

def get_baseurl_from_conn(%{host: h} = conn) do
scheme =
case h do
"localhost" -> :http
_ -> :https
end

get_baseurl_from_conn(Map.put(conn, :scheme, scheme))
end

@doc """
Expand Down
40 changes: 36 additions & 4 deletions test/elixir_auth_google_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,56 @@ defmodule ElixirAuthGoogleTest do

import Mock

test "get_baseurl_from_conn(conn) detects the URL based on conn.host" do
test "get_baseurl_from_conn(conn) detects the URL based on conn.host HTTP" do
conn = %{
host: "localhost",
port: 4000
port: 4000,
scheme: :http
}

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

test "get_baseurl_from_conn(conn) detects the URL for production" do
test "get_baseurl_from_conn(conn) detects the URL based on conn.host HTTPS" do
conn = %{
host: "localhost",
port: 4000,
scheme: :https
}

assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "https://localhost:4000"
end

test "get_baseurl_from_conn(conn) detects the URL for production HTTPS" do
conn = %{
host: "dwyl.com",
port: 80
port: 80,
scheme: :https
}

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

test "get_baseurl_from_conn(conn) detects the URL for production HTTP" do
conn = %{
host: "dwyl.com",
port: 80,
scheme: :http
}

assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "http://dwyl.com"
end

test "get_baseurl_from_conn(conn) detects the URL for production HTTPS, non-standard port" do
conn = %{
host: "dwyl.com",
port: 8080,
scheme: :https
}

assert ElixirAuthGoogle.get_baseurl_from_conn(conn) == "https://dwyl.com:8080"
end

test "get Google login url" do
conn = %{
host: "localhost",
Expand Down