Fix HTTP tests

This commit is contained in:
Ben Toogood 2020-04-09 10:28:38 +01:00
parent 8ff86ae08b
commit f102aba4c1
4 changed files with 36 additions and 14 deletions

View File

@ -7,6 +7,8 @@ import (
"testing"
"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"
regRouter "github.com/micro/go-micro/v2/api/router/registry"
"github.com/micro/go-micro/v2/registry"
@ -54,8 +56,10 @@ func testHttp(t *testing.T, path, service, ns string) {
// initialise the handler
rt := regRouter.NewRouter(
router.WithHandler("http"),
router.WithNamespace(ns),
router.WithRegistry(r),
router.WithResolver(res.NewResolver(
resolver.WithNamespace(ns),
)),
)
p := NewHandler(handler.WithRouter(rt))
@ -116,6 +120,8 @@ func TestHttpHandler(t *testing.T) {
}
for _, d := range testData {
testHttp(t, d.path, d.service, d.namespace)
t.Run(d.service, func(t *testing.T) {
testHttp(t, d.path, d.service, d.namespace)
})
}
}

View File

@ -7,7 +7,9 @@ import (
"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) {
return &resolver.Endpoint{
@ -23,5 +25,5 @@ func (r *Resolver) String() string {
}
func NewResolver(opts ...resolver.Option) resolver.Resolver {
return &Resolver{}
return &Resolver{opts: resolver.NewOptions(opts...)}
}

View File

@ -8,15 +8,18 @@ import (
"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) {
if req.URL.Path == "/" {
return nil, resolver.ErrNotFound
}
parts := strings.Split(req.URL.Path[1:], "/")
return &resolver.Endpoint{
Name: parts[0],
Name: r.opts.Namespace + "." + parts[0],
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
@ -28,5 +31,5 @@ func (r *Resolver) String() string {
}
func NewResolver(opts ...resolver.Option) resolver.Resolver {
return &Resolver{}
return &Resolver{opts: resolver.NewOptions(opts...)}
}

View File

@ -3,6 +3,7 @@ package vpath
import (
"errors"
"fmt"
"net/http"
"regexp"
"strings"
@ -10,7 +11,13 @@ import (
"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 (
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")
}
parts := strings.Split(req.URL.Path[1:], "/")
fmt.Println(req.URL.Path)
parts := strings.Split(req.URL.Path[1:], "/")
if len(parts) == 1 {
return &resolver.Endpoint{
Name: parts[0],
Name: addNamespace(r.opts.Namespace, parts...),
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
@ -35,7 +43,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
// /v1/foo
if re.MatchString(parts[0]) {
return &resolver.Endpoint{
Name: parts[1],
Name: addNamespace(r.opts.Namespace, parts[0:2]...),
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
@ -43,7 +51,7 @@ func (r *Resolver) Resolve(req *http.Request) (*resolver.Endpoint, error) {
}
return &resolver.Endpoint{
Name: parts[0],
Name: addNamespace(r.opts.Namespace, parts[0]),
Host: req.Host,
Method: req.Method,
Path: req.URL.Path,
@ -54,6 +62,9 @@ func (r *Resolver) String() string {
return "path"
}
func NewResolver(opts ...resolver.Option) resolver.Resolver {
return &Resolver{}
func addNamespace(ns string, parts ...string) string {
if len(ns) == 0 {
return strings.Join(parts, ".")
}
return strings.Join(append([]string{ns}, parts...), ".")
}