package main
import (
"strings"
"testing"
"github.com/vaughan0/go-ini"
)
// loadConf builds an ini.File directly from a literal, bypassing
// config.LoadConfig so the tests need no config.ini on disk and no
// internal-ipnet parsing.
func loadConf(t *testing.T, body string) ini.File {
t.Helper()
conf, err := ini.Load(strings.NewReader(body))
if err != nil {
t.Fatalf("ini.Load: %v", err)
}
return conf
}
func TestResolveSettingsDefaults(t *testing.T) {
conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org
connection-string=postgres://u@localhost/d?sslmode=disable
`)
got, err := resolveSettings(conf)
if err != nil {
t.Fatalf("resolveSettings: %v", err)
}
if got.connString != "postgres://u@localhost/d?sslmode=disable" {
t.Errorf("connString = %q", got.connString)
}
if got.httpHost != "dolt.example.org" {
t.Errorf("httpHost = %q, want dolt.example.org", got.httpHost)
}
if got.reposRoot != defaultReposRoot {
t.Errorf("reposRoot = %q, want %q", got.reposRoot, defaultReposRoot)
}
if got.staticDir != defaultStaticDir {
t.Errorf("staticDir = %q, want %q", got.staticDir, defaultStaticDir)
}
if got.remotesapiAddr != defaultRemotesapiAddr {
t.Errorf("remotesapiAddr = %q, want %q", got.remotesapiAddr, defaultRemotesapiAddr)
}
if got.credsapiAddr != defaultCredsapiAddr {
t.Errorf("credsapiAddr = %q, want %q", got.credsapiAddr, defaultCredsapiAddr)
}
}
func TestResolveSettingsOverrides(t *testing.T) {
conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org:8443
connection-string=postgres://u@localhost/d
repos=/srv/dolt
static-dir=/usr/share/sourcehut/dolt.sr.ht/static
remotesapi-listen=0.0.0.0:6306
credsapi-listen=0.0.0.0:6308
`)
got, err := resolveSettings(conf)
if err != nil {
t.Fatalf("resolveSettings: %v", err)
}
if got.reposRoot != "/srv/dolt" {
t.Errorf("reposRoot = %q", got.reposRoot)
}
if got.staticDir != "/usr/share/sourcehut/dolt.sr.ht/static" {
t.Errorf("staticDir = %q", got.staticDir)
}
if got.remotesapiAddr != "0.0.0.0:6306" {
t.Errorf("remotesapiAddr = %q", got.remotesapiAddr)
}
if got.credsapiAddr != "0.0.0.0:6308" {
t.Errorf("credsapiAddr = %q", got.credsapiAddr)
}
// The port is part of the sealed-URL authority and is preserved.
if got.httpHost != "dolt.example.org:8443" {
t.Errorf("httpHost = %q, want dolt.example.org:8443", got.httpHost)
}
}
func TestResolveSettingsMissingConnString(t *testing.T) {
conf := loadConf(t, `[dolt.sr.ht]
origin=https://dolt.example.org
`)
if _, err := resolveSettings(conf); err == nil {
t.Fatal("expected error for missing connection-string, got nil")
}
}
func TestResolveSettingsMissingOrigin(t *testing.T) {
conf := loadConf(t, `[dolt.sr.ht]
connection-string=postgres://u@localhost/d
`)
if _, err := resolveSettings(conf); err == nil {
t.Fatal("expected error for missing origin, got nil")
}
}
func TestHostFromOrigin(t *testing.T) {
cases := []struct {
origin string
want string
wantErr bool
}{
{"https://dolt.srht.bigb.es", "dolt.srht.bigb.es", false},
{"https://dolt.srht.bigb.es:443", "dolt.srht.bigb.es:443", false},
{"http://127.0.0.1:5307", "127.0.0.1:5307", false},
{"", "", true},
{"not-a-url-with-no-host", "", true},
}
for _, c := range cases {
got, err := hostFromOrigin(c.origin)
if c.wantErr {
if err == nil {
t.Errorf("hostFromOrigin(%q): expected error", c.origin)
}
continue
}
if err != nil {
t.Errorf("hostFromOrigin(%q): %v", c.origin, err)
continue
}
if got != c.want {
t.Errorf("hostFromOrigin(%q) = %q, want %q", c.origin, got, c.want)
}
}
}