pomerium/config/from_test.go
Caleb Doxsey 18bc86d632
config: add support for wildcard from addresses (#4131)
* config: add support for wildcards

* update policy matching, header generation

* remove deprecated field

* fix test
2023-04-25 13:34:38 -06:00

38 lines
1 KiB
Go

package config
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/pomerium/pomerium/internal/urlutil"
)
func TestFromURLMatchesRequestURL(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
pattern string
input string
matches bool
}{
{"https://from.example.com", "https://from.example.com/some/path", true},
{"https://from.example.com", "https://to.example.com/some/path", false},
{"https://*.example.com", "https://from.example.com/some/path", true},
{"https://*.example.com", "https://example.com/some/path", false},
} {
fromURL := urlutil.MustParseAndValidateURL(tc.pattern)
requestURL := urlutil.MustParseAndValidateURL(tc.input)
assert.Equal(t, tc.matches, FromURLMatchesRequestURL(&fromURL, &requestURL),
"from-url: %s\nrequest-url: %s", tc.pattern, tc.input)
}
}
func TestWildcardToRegex(t *testing.T) {
t.Parallel()
re, err := regexp.Compile(WildcardToRegex("*.internal.*.example.com"))
assert.NoError(t, err)
assert.True(t, re.MatchString("a.internal.b.example.com"))
}