Files
upstream-centrifugo/internal/origin/origin_test.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

115 lines
2.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package origin
import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestPatternCheck(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
origin string
originPatterns []string
success bool
}{
{
name: "empty_origin",
origin: "",
originPatterns: []string{
"*.example.com",
},
success: true,
},
{
name: "origin_patterns_match",
origin: "https://two.Example.com",
originPatterns: []string{
"*.example.com",
"foo.com",
},
success: true,
},
{
name: "origin_patterns_no_match",
origin: "https://two.Example.com",
originPatterns: []string{
"foo.com",
"bar.com",
},
success: false,
},
{
name: "origin_patterns_uppercase_in_pattern",
origin: "https://two.example.com",
originPatterns: []string{
"https://*.Example.com",
},
success: true,
},
{
name: "origin_patterns_uppercase_in_pattern_and_origin",
origin: "HTTPS://App.Example.com",
originPatterns: []string{
"https://APP.example.COM",
},
success: true,
},
{
name: "origin_patterns_uppercase_in_pattern_no_match",
origin: "https://two.example.org",
originPatterns: []string{
"https://*.Example.com",
},
success: false,
},
{
name: "origin_patterns_cyrillic_e_in_origin",
origin: "https://two.еxample.com",
originPatterns: []string{
"*.example.com",
"foo.com",
},
success: false,
},
{
name: "file_origin",
origin: "file://",
originPatterns: []string{
"file://*",
},
success: true,
},
{
name: "null_origin",
origin: "null",
originPatterns: []string{
"null",
},
success: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
r := httptest.NewRequest("GET", "https://example.com/websocket/connection", nil)
r.Header.Set("Origin", tc.origin)
a, err := NewPatternChecker(tc.originPatterns)
require.NoError(t, err)
ok := a.Check(r)
if tc.success {
require.True(t, ok)
} else {
require.False(t, ok)
}
})
}
}