VictoriaMetrics/lib/flagutil/url_test.go
Roman Khavronenko 029060af60
app/vmbackup: introduce new flag type URL (#6152)
The new flag type is supposed to be used for specifying URL values which
could contain sensitive information such as auth tokens in GET params or
HTTP basic authentication.

The URL flag also allows loading its value from files if `file://`
prefix is specified. As example, the new flag type was used in
app/vmbackup as it requires specifying `authKey` param for making the
snapshot.

See related issue
https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5973

Thanks to @wasim-nihal for initial implementation
https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6060

---------

Signed-off-by: hagen1778 <roman@victoriametrics.com>
2024-04-24 10:57:54 +02:00

46 lines
1.1 KiB
Go

package flagutil
import (
"os"
"testing"
)
func TestNewURL(t *testing.T) {
u := &URL{}
f := func(s, exp string) {
t.Helper()
if err := u.Set(s); err != nil {
t.Fatalf("failed to set %q value: %s", s, err)
}
if u.String() != exp {
t.Fatalf("expected to get %q; got %q instead", exp, u.String())
}
}
f("", "")
f("http://foo:8428", "http://foo:8428")
f("http://username:password@foo:8428", "http://xxxxx:xxxxx@foo:8428")
f("http://foo:8428?authToken=bar", "http://foo:8428?authToken=xxxxx")
f("http://username:password@foo:8428?authToken=bar", "http://xxxxx:xxxxx@foo:8428?authToken=xxxxx")
file, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Remove(file.Name()) }()
writeToFile(t, file.Name(), "http://foo:8428")
f("file://"+file.Name(), "http://foo:8428")
writeToFile(t, file.Name(), "http://xxxxx:password@foo:8428?authToken=bar")
f("file://"+file.Name(), "http://xxxxx:xxxxx@foo:8428?authToken=xxxxx")
}
func writeToFile(t *testing.T, file, b string) {
t.Helper()
err := os.WriteFile(file, []byte(b), 0644)
if err != nil {
t.Fatal(err)
}
}