pages-server/server/upstream/domains_test.go
2024-08-25 22:13:08 +01:00

75 lines
1.8 KiB
Go

package upstream
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPageMainDomainGeneratesTheExpectedDomain(t *testing.T) {
defaultOptions := Options{
TargetOwner: "",
TargetRepo: "",
TargetBranch: "",
TargetPath: "",
Host: "",
TryIndexPages: false,
BranchTimestamp: time.Time{},
appendTrailingSlash: false,
redirectIfExists: "",
ServeRaw: false,
}
for _, tc := range []struct {
targetOwner string
targetRepo string
domainSuffix string
expectedDomain string
}{
{"foo", "", ".localhost.mock.directory", "foo.localhost.mock.directory"},
{"foo", "pages", ".localhost.mock.directory", "foo.localhost.mock.directory"},
{"foo", "bar", ".localhost.mock.directory", "foo.localhost.mock.directory/bar"},
} {
options := defaultOptions
options.TargetOwner = tc.targetOwner
options.TargetRepo = tc.targetRepo
actualDomain := options.pageMainDomain(tc.domainSuffix)
assert.Equal(t, tc.expectedDomain, actualDomain)
}
}
func TestNormalizeDomainEntries(t *testing.T) {
for _, tc := range []struct {
domain string
}{
{"abc.com"},
{"ABC.com"},
{" ABC.com"},
{"ABC.com "},
{" ABC.com "},
{"http://ABC.com"},
{"https://ABC.com"},
} {
actualDomains := normalizeDomainEntries(tc.domain)
expectedDomains := []string{"abc.com"}
assert.Equal(t, expectedDomains, actualDomains)
}
for _, tc := range []struct {
domains string
expectedDomains []string
}{
{"", []string{}},
{"ABC.com", []string{"abc.com"}},
{"ABC.com\nhttps://example.com", []string{"abc.com", "example.com"}},
{"\n\nABC.com\n\nhttps://example.com\n", []string{"abc.com", "example.com"}},
} {
actualDomains := normalizeDomainEntries(tc.domains)
assert.Equal(t, tc.expectedDomains, actualDomains)
}
}