2020-11-06 11:41:23 +03:00
|
|
|
package router_test
|
|
|
|
|
|
|
|
import (
|
2021-01-14 08:34:29 +03:00
|
|
|
"bytes"
|
2020-11-06 11:41:23 +03:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-01-14 08:34:29 +03:00
|
|
|
"io"
|
2020-11-06 11:41:23 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-01-16 02:49:58 +03:00
|
|
|
rpc "github.com/unistack-org/micro-api-handler-rpc/v3"
|
2021-01-29 16:47:26 +03:00
|
|
|
rregister "github.com/unistack-org/micro-api-router-register/v3"
|
2021-01-16 02:49:58 +03:00
|
|
|
rstatic "github.com/unistack-org/micro-api-router-static/v3"
|
|
|
|
gcli "github.com/unistack-org/micro-client-grpc/v3"
|
|
|
|
jsoncodec "github.com/unistack-org/micro-codec-json/v3"
|
|
|
|
protocodec "github.com/unistack-org/micro-codec-proto/v3"
|
2021-01-29 16:47:26 +03:00
|
|
|
regRouter "github.com/unistack-org/micro-router-register/v3"
|
2021-01-16 02:49:58 +03:00
|
|
|
gsrv "github.com/unistack-org/micro-server-grpc/v3"
|
2021-02-27 13:51:39 +03:00
|
|
|
gpb "github.com/unistack-org/micro-tests/server/grpc/gproto"
|
2021-01-10 10:18:42 +03:00
|
|
|
pb "github.com/unistack-org/micro-tests/server/grpc/proto"
|
2020-11-06 11:41:23 +03:00
|
|
|
"github.com/unistack-org/micro/v3/api"
|
|
|
|
"github.com/unistack-org/micro/v3/api/handler"
|
|
|
|
"github.com/unistack-org/micro/v3/api/router"
|
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
2021-03-06 22:24:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2021-02-12 20:50:02 +03:00
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-11-06 11:41:23 +03:00
|
|
|
rt "github.com/unistack-org/micro/v3/router"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2021-01-14 08:34:29 +03:00
|
|
|
if req.Name == "Timeout" {
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
rsp.Msg = "Timeout"
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-06 11:41:23 +03:00
|
|
|
rsp.Msg = "Hello " + req.Uuid
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initial(t *testing.T) (server.Server, client.Client) {
|
2021-01-10 10:18:42 +03:00
|
|
|
//logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.TraceLevel))
|
2021-02-12 20:50:02 +03:00
|
|
|
r := register.NewRegister()
|
2021-01-10 10:18:42 +03:00
|
|
|
if err := r.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
b := broker.NewBroker(broker.Register(r))
|
2021-01-10 10:18:42 +03:00
|
|
|
if err := b.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-06 11:41:23 +03:00
|
|
|
|
|
|
|
// create a new client
|
|
|
|
s := gsrv.NewServer(
|
2021-01-10 10:18:42 +03:00
|
|
|
server.Codec("application/grpc+proto", protocodec.NewCodec()),
|
|
|
|
server.Codec("application/grpc+json", protocodec.NewCodec()),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2020-11-06 11:41:23 +03:00
|
|
|
server.Name("foo"),
|
|
|
|
server.Broker(b),
|
2021-01-29 16:47:26 +03:00
|
|
|
server.Register(r),
|
2021-01-10 10:18:42 +03:00
|
|
|
server.RegisterInterval(1*time.Second),
|
2020-11-06 11:41:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
rtr := regRouter.NewRouter(
|
2021-01-29 16:47:26 +03:00
|
|
|
rt.Register(r),
|
2020-11-06 11:41:23 +03:00
|
|
|
)
|
|
|
|
|
2021-01-10 10:18:42 +03:00
|
|
|
if err := rtr.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:41:23 +03:00
|
|
|
// create a new server
|
|
|
|
c := gcli.NewClient(
|
2021-01-10 10:18:42 +03:00
|
|
|
client.Codec("application/grpc+proto", protocodec.NewCodec()),
|
|
|
|
client.Codec("application/grpc+json", protocodec.NewCodec()),
|
|
|
|
client.Codec("application/json", jsoncodec.NewCodec()),
|
2021-01-29 16:47:26 +03:00
|
|
|
client.Register(r),
|
2020-11-06 11:41:23 +03:00
|
|
|
client.Router(rtr),
|
|
|
|
client.Broker(b),
|
|
|
|
)
|
|
|
|
|
|
|
|
h := &testServer{}
|
2021-02-27 13:51:39 +03:00
|
|
|
gpb.RegisterTestServer(s, h)
|
2020-11-06 11:41:23 +03:00
|
|
|
|
2021-01-10 10:18:42 +03:00
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatalf("failed to init: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:41:23 +03:00
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("failed to start: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, c
|
|
|
|
}
|
|
|
|
|
2021-01-14 08:34:29 +03:00
|
|
|
func check(t *testing.T, addr string, path string, expected string, timeout bool) {
|
|
|
|
var r io.Reader
|
|
|
|
|
|
|
|
if timeout {
|
|
|
|
r = bytes.NewBuffer([]byte(`{"name":"Timeout"}`))
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), r)
|
2020-11-06 11:41:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to created http.Request: %v", err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2021-01-14 08:34:29 +03:00
|
|
|
req.Header.Set("Timeout", "1") // set timeout to 1s
|
2020-11-06 11:41:23 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-03-06 22:24:49 +03:00
|
|
|
reterr := errors.Parse(string(buf))
|
|
|
|
experr := errors.Parse(expected)
|
|
|
|
if reterr.Code != 0 || experr.Code != 0 {
|
|
|
|
if !errors.Equal(reterr, experr) {
|
|
|
|
t.Fatalf("invalid message received, parsing error %s != %s", buf, expected)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if string(buf) != expected {
|
|
|
|
t.Fatalf("invalid message received, parsing error %s != %s", buf, expected)
|
|
|
|
}
|
2020-11-06 11:41:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 08:34:29 +03:00
|
|
|
func TestApiTimeout(t *testing.T) {
|
|
|
|
s, c := initial(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
router := rregister.NewRouter(
|
2021-01-14 08:34:29 +03:00
|
|
|
router.WithHandler(rpc.Handler),
|
2021-01-29 16:47:26 +03:00
|
|
|
router.WithRegister(s.Options().Register),
|
2021-01-14 08:34:29 +03:00
|
|
|
)
|
|
|
|
if err := router.Init(); 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/TEST", `{"Id":"go.micro.client","Code":408,"Detail":"context deadline exceeded","Status":"Request Timeout"}`, true)
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
func TestRouterRegisterPcre(t *testing.T) {
|
2020-11-06 11:41:23 +03:00
|
|
|
s, c := initial(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
router := rregister.NewRouter(
|
2020-11-06 11:41:23 +03:00
|
|
|
router.WithHandler(rpc.Handler),
|
2021-01-29 16:47:26 +03:00
|
|
|
router.WithRegister(s.Options().Register),
|
2020-11-06 11:41:23 +03:00
|
|
|
)
|
2021-01-10 10:18:42 +03:00
|
|
|
if err := router.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-11-06 11:41:23 +03:00
|
|
|
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)
|
2021-01-14 08:34:29 +03:00
|
|
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello "}`, false)
|
2020-11-06 11:41:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterStaticPcre(t *testing.T) {
|
|
|
|
s, c := initial(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
router := rstatic.NewRouter(
|
|
|
|
router.WithHandler(rpc.Handler),
|
2021-01-29 16:47:26 +03:00
|
|
|
router.WithRegister(s.Options().Register),
|
2020-11-06 11:41:23 +03:00
|
|
|
)
|
2021-01-10 10:18:42 +03:00
|
|
|
if err := router.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-06 11:41:23 +03:00
|
|
|
|
|
|
|
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)
|
2021-01-14 08:34:29 +03:00
|
|
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`, false)
|
2020-11-06 11:41:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouterStaticGpath(t *testing.T) {
|
|
|
|
s, c := initial(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
router := rstatic.NewRouter(
|
|
|
|
router.WithHandler(rpc.Handler),
|
2021-01-29 16:47:26 +03:00
|
|
|
router.WithRegister(s.Options().Register),
|
2020-11-06 11:41:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
err := router.Register(&api.Endpoint{
|
|
|
|
Name: "foo.Test.Call",
|
|
|
|
Method: []string{"POST"},
|
|
|
|
Path: []string{"/api/v0/test/call/{uuid}"},
|
|
|
|
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)
|
2021-01-14 08:34:29 +03:00
|
|
|
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`, false)
|
2020-11-06 11:41:23 +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),
|
2021-01-29 16:47:26 +03:00
|
|
|
router.WithRegister(s.Options().Register),
|
2020-11-06 11:41:23 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|