micro/api/server/auth/auth_test.go

39 lines
1.1 KiB
Go
Raw Normal View History

2020-04-07 11:40:40 +03:00
package auth
import (
"net/http"
2020-04-07 13:37:04 +03:00
"net/url"
2020-04-07 11:40:40 +03:00
"testing"
"github.com/micro/go-micro/v2/auth"
)
func TestNamespaceFromRequest(t *testing.T) {
tt := []struct {
Host string
Namespace string
}{
{Host: "micro.mu", Namespace: auth.DefaultNamespace},
2020-04-07 12:28:39 +03:00
{Host: "micro.com.au", Namespace: auth.DefaultNamespace},
2020-04-07 11:40:40 +03:00
{Host: "web.micro.mu", Namespace: auth.DefaultNamespace},
{Host: "api.micro.mu", Namespace: auth.DefaultNamespace},
{Host: "myapp.com", Namespace: auth.DefaultNamespace},
{Host: "staging.myapp.com", Namespace: "staging"},
{Host: "staging.myapp.m3o.app", Namespace: "myapp.staging"},
{Host: "127.0.0.1", Namespace: auth.DefaultNamespace},
{Host: "localhost", Namespace: auth.DefaultNamespace},
{Host: "81.151.101.146", Namespace: auth.DefaultNamespace},
}
2020-04-07 12:28:39 +03:00
h := &authHandler{namespace: "domain"}
2020-04-07 11:40:40 +03:00
for _, tc := range tt {
t.Run(tc.Host, func(t *testing.T) {
2020-04-07 13:37:04 +03:00
ns := h.NamespaceFromRequest(&http.Request{Host: tc.Host, URL: &url.URL{Host: tc.Host}})
2020-04-07 11:40:40 +03:00
if ns != tc.Namespace {
t.Errorf("Expected namespace %v for host %v, actually got %v", tc.Namespace, tc.Host, ns)
}
})
}
}