defmodule DiogramosWeb.PublicUrlTest do
use DiogramosWeb.ConnCase, async: false
alias DiogramosWeb.PublicUrl
setup do
on_exit(fn -> System.delete_env("DIOGRAMOS_PUBLIC_URL") end)
:ok
end
defp conn_at(host, port, scheme, headers \\ []) do
conn = %{Phoenix.ConnTest.build_conn() | host: host, port: port, scheme: scheme}
Enum.reduce(headers, conn, fn {k, v}, acc -> Plug.Conn.put_req_header(acc, k, v) end)
end
test "uses the env override above everything else" do
System.put_env("DIOGRAMOS_PUBLIC_URL", "https://diagrams.example.com/")
conn = conn_at("localhost", 4000, :http, [{"x-forwarded-proto", "http"}])
assert PublicUrl.derive(conn) == "https://diagrams.example.com"
end
test "falls back to the request's own scheme and host" do
assert PublicUrl.derive(conn_at("example.com", 80, :http)) == "http://example.com"
assert PublicUrl.derive(conn_at("example.com", 443, :https)) == "https://example.com"
end
test "keeps a non-default port, which is what LAN and emulator use" do
assert PublicUrl.derive(conn_at("192.168.1.50", 4000, :http)) == "http://192.168.1.50:4000"
end
test "honours x-forwarded-proto while the proxy passes Host through" do
# The Traefik/nginx default, and the case that matters: getting this
# wrong puts http:// in the QR behind a TLS-terminating proxy.
conn = conn_at("diagrams.example.com", 80, :http, [{"x-forwarded-proto", "https"}])
assert PublicUrl.derive(conn) == "https://diagrams.example.com"
end
test "honours x-forwarded-host" do
conn =
conn_at("10.0.0.5", 4000, :http, [
{"x-forwarded-proto", "https"},
{"x-forwarded-host", "diagrams.example.com"}
])
assert PublicUrl.derive(conn) == "https://diagrams.example.com"
end
test "takes the client-most value from a chained proxy list" do
conn =
conn_at("10.0.0.5", 4000, :http, [
{"x-forwarded-proto", "https, http"},
{"x-forwarded-host", "edge.example.com, inner.example.com"}
])
assert PublicUrl.derive(conn) == "https://edge.example.com"
end
test "an x-forwarded-host carrying its own port is passed through" do
conn = conn_at("10.0.0.5", 4000, :http, [{"x-forwarded-host", "example.com:8443"}])
assert PublicUrl.derive(conn) == "http://example.com:8443"
end
end
neiam
/diogramos
diogramos
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
2.3 KiB · text
History
dc2edab