Files
magpie/web_cli_test.go
T
yetone a690d8d7a1 main: magpie web behind a reverse proxy prints its link through the proxy, key and all (#1479, uclort)
magpie web on a server, listening on 127.0.0.1:3430 behind nginx or
Caddy, printed only http://127.0.0.1:3430/?k=…, and the reporter had to
join "/?k=…" onto their domain by hand. It now also prints
"through your proxy https://magpie.example.com/?k=…" when it is told
the proxy's address: MAGPIE_WEB_URL (the page's own address there), or
else MAGPIE_PUBLIC_URL with no port of its own, which NetworkLinks
already takes as a reverse proxy for --lan (#372). A MAGPIE_WEB_URL with
a query, a fragment, credentials or another scheme is ignored.

Only the printed link changes: the key, its cookie and webGuard are as
they were, and nothing new is listened on. The key itself was already
fixed with MAGPIE_WEB_KEY (16+ letters, digits, - . _ ~); the usage text
and docs/reference.md say both.

Run: go test -tags nogui -run TestWebProxyLink . (red with proxyLink
returning ""); a built magpie web under a sandbox HOME with
MAGPIE_WEB_URL=https://magpie.example.com, MAGPIE_WEB_KEY set, on
127.0.0.1:3493 printed the proxy line, answered 401 with no key and 303
with the key under Host: magpie.example.com.
2026-10-10 18:31:32 +08:00

38 lines
1.5 KiB
Go

package main
import "testing"
// magpie web on loopback behind a reverse proxy (#1479) prints the proxy's
// link with the key when told the proxy's address, so nobody joins
// "/?k=…" onto it by hand: MAGPIE_WEB_URL, else MAGPIE_PUBLIC_URL with no
// port (one with a port isn't a proxy). None is printed without either.
func TestWebProxyLink(t *testing.T) {
key := "/?k=0123456789abcdef"
for _, c := range []struct{ env, want string }{
{"https://magpie.example.com", "https://magpie.example.com/?k=0123456789abcdef"},
{"https://magpie.example.com/", "https://magpie.example.com/?k=0123456789abcdef"},
{"magpie.example.com", "http://magpie.example.com/?k=0123456789abcdef"},
{"https://example.com/magpie/", "https://example.com/magpie/?k=0123456789abcdef"},
{"http://192.168.1.20:3425", ""},
{"", ""},
} {
t.Setenv("MAGPIE_PUBLIC_URL", c.env)
if got := proxyLink(key); got != c.want {
t.Errorf("MAGPIE_PUBLIC_URL=%q: %q, want %q", c.env, got, c.want)
}
}
// the page's own proxy address, when the gateway's is elsewhere
t.Setenv("MAGPIE_PUBLIC_URL", "https://api.example.com")
for _, c := range []struct{ env, want string }{
{"https://magpie.example.com/", "https://magpie.example.com/?k=0123456789abcdef"},
{"magpie.example.com:8443", "https://magpie.example.com:8443/?k=0123456789abcdef"},
{"https://magpie.example.com/?x=1", ""},
{"ftp://magpie.example.com", ""},
} {
t.Setenv("MAGPIE_WEB_URL", c.env)
if got := proxyLink(key); got != c.want {
t.Errorf("MAGPIE_WEB_URL=%q: %q, want %q", c.env, got, c.want)
}
}
}