2020-08-25 13:44:41 +03:00
|
|
|
// +build ignore
|
|
|
|
|
2020-04-15 17:50:51 +03:00
|
|
|
package router_test
|
2020-03-30 11:04:59 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/api"
|
|
|
|
"github.com/unistack-org/micro/v3/api/handler"
|
|
|
|
"github.com/unistack-org/micro/v3/api/handler/rpc"
|
|
|
|
"github.com/unistack-org/micro/v3/api/router"
|
|
|
|
rregistry "github.com/unistack-org/micro/v3/api/router/registry"
|
|
|
|
rstatic "github.com/unistack-org/micro/v3/api/router/static"
|
2020-08-20 14:45:47 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
bmemory "github.com/unistack-org/micro/v3/broker/memory"
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
gcli "github.com/unistack-org/micro/v3/client/grpc"
|
|
|
|
rmemory "github.com/unistack-org/micro/v3/registry/memory"
|
|
|
|
rt "github.com/unistack-org/micro/v3/router"
|
|
|
|
regRouter "github.com/unistack-org/micro/v3/router/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
gsrv "github.com/unistack-org/micro/v3/server/grpc"
|
|
|
|
pb "github.com/unistack-org/micro/v3/server/grpc/proto"
|
2020-03-30 11:04:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// server is used to implement helloworld.GreeterServer.
|
|
|
|
type testServer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHello implements helloworld.GreeterServer
|
|
|
|
func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
2020-04-15 17:50:51 +03:00
|
|
|
rsp.Msg = "Hello " + req.Uuid
|
2020-03-30 11:04:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:23:10 +03:00
|
|
|
// TestHello implements helloworld.GreeterServer
|
|
|
|
func (s *testServer) CallPcre(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
|
|
|
rsp.Msg = "Hello " + req.Uuid
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestHello implements helloworld.GreeterServer
|
|
|
|
func (s *testServer) CallPcreInvalid(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
|
|
|
|
rsp.Msg = "Hello " + req.Uuid
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-15 17:50:51 +03:00
|
|
|
func initial(t *testing.T) (server.Server, client.Client) {
|
2020-03-30 11:04:59 +03:00
|
|
|
r := rmemory.NewRegistry()
|
2020-08-20 14:45:47 +03:00
|
|
|
b := bmemory.NewBroker(broker.Registry(r))
|
2020-04-12 14:29:38 +01:00
|
|
|
|
|
|
|
// create a new client
|
2020-03-30 11:04:59 +03:00
|
|
|
s := gsrv.NewServer(
|
|
|
|
server.Name("foo"),
|
2020-08-20 14:45:47 +03:00
|
|
|
server.Broker(b),
|
2020-03-30 11:04:59 +03:00
|
|
|
server.Registry(r),
|
|
|
|
)
|
2020-04-12 14:29:38 +01:00
|
|
|
|
2020-08-17 22:44:45 +01:00
|
|
|
rtr := regRouter.NewRouter(
|
|
|
|
rt.Registry(r),
|
|
|
|
)
|
|
|
|
|
2020-04-12 14:29:38 +01:00
|
|
|
// create a new server
|
2020-03-30 11:04:59 +03:00
|
|
|
c := gcli.NewClient(
|
2020-08-17 22:44:45 +01:00
|
|
|
client.Router(rtr),
|
2020-08-20 14:45:47 +03:00
|
|
|
client.Broker(b),
|
2020-03-30 11:04:59 +03:00
|
|
|
)
|
2020-04-12 14:29:38 +01:00
|
|
|
|
2020-03-30 11:04:59 +03:00
|
|
|
h := &testServer{}
|
|
|
|
pb.RegisterTestHandler(s, h)
|
|
|
|
|
2020-04-12 14:29:38 +01:00
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start: %v", err)
|
2020-03-30 11:04:59 +03:00
|
|
|
}
|
2020-04-15 17:50:51 +03:00
|
|
|
|
|
|
|
return s, c
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:31:34 +03:00
|
|
|
func check(t *testing.T, addr string, path string, expected string) {
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), nil)
|
2020-04-15 17:50:51 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to created http.Request: %v", err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
rsp, err := (&http.Client{}).Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to created http.Request: %v", err)
|
|
|
|
}
|
|
|
|
defer rsp.Body.Close()
|
|
|
|
|
|
|
|
buf, err := ioutil.ReadAll(rsp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-19 00:31:34 +03:00
|
|
|
jsonMsg := expected
|
2020-04-15 17:50:51 +03:00
|
|
|
if string(buf) != jsonMsg {
|
|
|
|
t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:23:10 +03:00
|
|
|
func TestRouterRegistryPcre(t *testing.T) {
|
2020-04-15 17:50:51 +03:00
|
|
|
s, c := initial(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
router := rregistry.NewRouter(
|
|
|
|
router.WithHandler(rpc.Handler),
|
|
|
|
router.WithRegistry(s.Options().Registry),
|
|
|
|
)
|
|
|
|
hrpc := rpc.NewHandler(
|
|
|
|
handler.WithClient(c),
|
|
|
|
handler.WithRouter(router),
|
|
|
|
)
|
|
|
|
hsrv := &http.Server{
|
|
|
|
Handler: hrpc,
|
|
|
|
Addr: "127.0.0.1:6543",
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
IdleTimeout: 20 * time.Second,
|
|
|
|
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Println(hsrv.ListenAndServe())
|
|
|
|
}()
|
|
|
|
|
|
|
|
defer hsrv.Close()
|
|
|
|
time.Sleep(1 * time.Second)
|
2020-04-19 00:31:34 +03:00
|
|
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`)
|
2020-04-15 17:50:51 +03:00
|
|
|
}
|
|
|
|
|
2020-04-19 00:31:34 +03:00
|
|
|
func TestRouterStaticPcre(t *testing.T) {
|
|
|
|
s, c := initial(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
router := rstatic.NewRouter(
|
|
|
|
router.WithHandler(rpc.Handler),
|
|
|
|
router.WithRegistry(s.Options().Registry),
|
|
|
|
)
|
|
|
|
|
|
|
|
err := router.Register(&api.Endpoint{
|
|
|
|
Name: "foo.Test.Call",
|
|
|
|
Method: []string{"POST"},
|
|
|
|
Path: []string{"^/api/v0/test/call/?$"},
|
|
|
|
Handler: "rpc",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hrpc := rpc.NewHandler(
|
|
|
|
handler.WithClient(c),
|
|
|
|
handler.WithRouter(router),
|
|
|
|
)
|
|
|
|
hsrv := &http.Server{
|
|
|
|
Handler: hrpc,
|
|
|
|
Addr: "127.0.0.1:6543",
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
IdleTimeout: 20 * time.Second,
|
|
|
|
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Println(hsrv.ListenAndServe())
|
|
|
|
}()
|
|
|
|
defer hsrv.Close()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`)
|
|
|
|
}
|
|
|
|
|
2020-04-29 15:23:10 +03:00
|
|
|
func TestRouterStaticGpath(t *testing.T) {
|
2020-04-15 17:50:51 +03:00
|
|
|
s, c := initial(t)
|
2020-04-12 14:29:38 +01:00
|
|
|
defer s.Stop()
|
2020-03-30 11:04:59 +03:00
|
|
|
|
|
|
|
router := rstatic.NewRouter(
|
2020-04-12 14:29:38 +01:00
|
|
|
router.WithHandler(rpc.Handler),
|
2020-04-15 17:50:51 +03:00
|
|
|
router.WithRegistry(s.Options().Registry),
|
2020-03-30 11:04:59 +03:00
|
|
|
)
|
2020-04-12 14:29:38 +01:00
|
|
|
|
|
|
|
err := router.Register(&api.Endpoint{
|
2020-03-30 11:04:59 +03:00
|
|
|
Name: "foo.Test.Call",
|
2020-04-15 17:50:51 +03:00
|
|
|
Method: []string{"POST"},
|
|
|
|
Path: []string{"/api/v0/test/call/{uuid}"},
|
2020-03-30 11:04:59 +03:00
|
|
|
Handler: "rpc",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-04-12 14:29:38 +01:00
|
|
|
hrpc := rpc.NewHandler(
|
|
|
|
handler.WithClient(c),
|
|
|
|
handler.WithRouter(router),
|
2020-03-30 11:04:59 +03:00
|
|
|
)
|
|
|
|
hsrv := &http.Server{
|
|
|
|
Handler: hrpc,
|
|
|
|
Addr: "127.0.0.1:6543",
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
IdleTimeout: 20 * time.Second,
|
|
|
|
MaxHeaderBytes: 1024 * 1024 * 1, // 1Mb
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Println(hsrv.ListenAndServe())
|
|
|
|
}()
|
2020-04-15 17:50:51 +03:00
|
|
|
defer hsrv.Close()
|
2020-03-30 11:04:59 +03:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2020-04-19 00:31:34 +03:00
|
|
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`)
|
2020-03-30 11:04:59 +03:00
|
|
|
}
|
2020-04-29 15:23:10 +03:00
|
|
|
|
|
|
|
func TestRouterStaticPcreInvalid(t *testing.T) {
|
|
|
|
var ep *api.Endpoint
|
|
|
|
var err error
|
|
|
|
|
|
|
|
s, c := initial(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
router := rstatic.NewRouter(
|
|
|
|
router.WithHandler(rpc.Handler),
|
|
|
|
router.WithRegistry(s.Options().Registry),
|
|
|
|
)
|
|
|
|
|
|
|
|
ep = &api.Endpoint{
|
|
|
|
Name: "foo.Test.Call",
|
|
|
|
Method: []string{"POST"},
|
|
|
|
Path: []string{"^/api/v0/test/call/?"},
|
|
|
|
Handler: "rpc",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = router.Register(ep)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("invalid endpoint %v", ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep = &api.Endpoint{
|
|
|
|
Name: "foo.Test.Call",
|
|
|
|
Method: []string{"POST"},
|
|
|
|
Path: []string{"/api/v0/test/call/?$"},
|
|
|
|
Handler: "rpc",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = router.Register(ep)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("invalid endpoint %v", ep)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = c
|
|
|
|
}
|