Fix HTTP tests
This commit is contained in:
parent
8ff86ae08b
commit
f102aba4c1
@ -7,6 +7,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/go-micro/v2/api/handler"
|
"github.com/micro/go-micro/v2/api/handler"
|
||||||
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
|
res "github.com/micro/go-micro/v2/api/resolver/vpath"
|
||||||
"github.com/micro/go-micro/v2/api/router"
|
"github.com/micro/go-micro/v2/api/router"
|
||||||
regRouter "github.com/micro/go-micro/v2/api/router/registry"
|
regRouter "github.com/micro/go-micro/v2/api/router/registry"
|
||||||
"github.com/micro/go-micro/v2/registry"
|
"github.com/micro/go-micro/v2/registry"
|
||||||
@ -54,8 +56,10 @@ func testHttp(t *testing.T, path, service, ns string) {
|
|||||||
// initialise the handler
|
// initialise the handler
|
||||||
rt := regRouter.NewRouter(
|
rt := regRouter.NewRouter(
|
||||||
router.WithHandler("http"),
|
router.WithHandler("http"),
|
||||||
router.WithNamespace(ns),
|
|
||||||
router.WithRegistry(r),
|
router.WithRegistry(r),
|
||||||
|
router.WithResolver(res.NewResolver(
|
||||||
|
resolver.WithNamespace(ns),
|
||||||
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
p := NewHandler(handler.WithRouter(rt))
|
p := NewHandler(handler.WithRouter(rt))
|
||||||
@ -116,6 +120,8 @@ func TestHttpHandler(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, d := range testData {
|
for _, d := range testData {
|
||||||
|
t.Run(d.service, func(t *testing.T) {
|
||||||
testHttp(t, d.path, d.service, d.namespace)
|
testHttp(t, d.path, d.service, d.namespace)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,9 @@ import (
|
|||||||
"github.com/micro/go-micro/v2/api/resolver"
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver struct{}
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
@ -23,5 +25,5 @@ func (r *Resolver) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
return &Resolver{}
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
@ -8,15 +8,18 @@ import (
|
|||||||
"github.com/micro/go-micro/v2/api/resolver"
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver struct{}
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
||||||
if req.URL.Path == "/" {
|
if req.URL.Path == "/" {
|
||||||
return nil, resolver.ErrNotFound
|
return nil, resolver.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(req.URL.Path[1:], "/")
|
parts := strings.Split(req.URL.Path[1:], "/")
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[0],
|
Name: r.opts.Namespace + "." + parts[0],
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
@ -28,5 +31,5 @@ func (r *Resolver) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
return &Resolver{}
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package vpath
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -10,7 +11,13 @@ import (
|
|||||||
"github.com/micro/go-micro/v2/api/resolver"
|
"github.com/micro/go-micro/v2/api/resolver"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver struct{}
|
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
||||||
|
return &Resolver{opts: resolver.NewOptions(opts...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Resolver struct {
|
||||||
|
opts resolver.Options
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
re = regexp.MustCompile("^v[0-9]+$")
|
re = regexp.MustCompile("^v[0-9]+$")
|
||||||
@ -21,11 +28,12 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|||||||
return nil, errors.New("unknown name")
|
return nil, errors.New("unknown name")
|
||||||
}
|
}
|
||||||
|
|
||||||
parts := strings.Split(req.URL.Path[1:], "/")
|
fmt.Println(req.URL.Path)
|
||||||
|
|
||||||
|
parts := strings.Split(req.URL.Path[1:], "/")
|
||||||
if len(parts) == 1 {
|
if len(parts) == 1 {
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[0],
|
Name: addNamespace(r.opts.Namespace, parts...),
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
@ -35,7 +43,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|||||||
// /v1/foo
|
// /v1/foo
|
||||||
if re.MatchString(parts[0]) {
|
if re.MatchString(parts[0]) {
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[1],
|
Name: addNamespace(r.opts.Namespace, parts[0:2]...),
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
@ -43,7 +51,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &resolver.Endpoint{
|
return &resolver.Endpoint{
|
||||||
Name: parts[0],
|
Name: addNamespace(r.opts.Namespace, parts[0]),
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
Method: req.Method,
|
Method: req.Method,
|
||||||
Path: req.URL.Path,
|
Path: req.URL.Path,
|
||||||
@ -54,6 +62,9 @@ func (r *Resolver) String() string {
|
|||||||
return "path"
|
return "path"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewResolver(opts ...resolver.Option) resolver.Resolver {
|
func addNamespace(ns string, parts ...string) string {
|
||||||
return &Resolver{}
|
if len(ns) == 0 {
|
||||||
|
return strings.Join(parts, ".")
|
||||||
|
}
|
||||||
|
return strings.Join(append([]string{ns}, parts...), ".")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user