add api rpc tests

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-01-14 08:34:29 +03:00
parent 957c488cbe
commit f149027dd8
3 changed files with 64 additions and 21 deletions

View File

@ -1,8 +1,10 @@
package router_test
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
@ -35,18 +37,11 @@ type testServer struct {
// TestHello implements helloworld.GreeterServer
func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response) error {
rsp.Msg = "Hello " + req.Uuid
if req.Name == "Timeout" {
time.Sleep(2 * time.Second)
rsp.Msg = "Timeout"
return nil
}
// 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
}
@ -106,12 +101,18 @@ func initial(t *testing.T) (server.Server, client.Client) {
return s, c
}
func check(t *testing.T, addr string, path string, expected string) {
req, err := http.NewRequest("POST", fmt.Sprintf(path, addr), nil)
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)
if err != nil {
t.Fatalf("Failed to created http.Request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Timeout", "1") // set timeout to 1s
rsp, err := (&http.Client{}).Do(req)
if err != nil {
t.Fatalf("Failed to created http.Request: %v", err)
@ -129,6 +130,40 @@ func check(t *testing.T, addr string, path string, expected string) {
}
}
func TestApiTimeout(t *testing.T) {
s, c := initial(t)
defer s.Stop()
router := rregistry.NewRouter(
router.WithHandler(rpc.Handler),
router.WithRegistry(s.Options().Registry),
)
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)
}
func TestRouterRegistryPcre(t *testing.T) {
s, c := initial(t)
defer s.Stop()
@ -160,7 +195,7 @@ func TestRouterRegistryPcre(t *testing.T) {
defer hsrv.Close()
time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello "}`)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello "}`, false)
}
func TestRouterStaticPcre(t *testing.T) {
@ -204,7 +239,7 @@ func TestRouterStaticPcre(t *testing.T) {
defer hsrv.Close()
time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`)
check(t, hsrv.Addr, "http://%s/api/v0/test/call", `{"msg":"Hello "}`, false)
}
func TestRouterStaticGpath(t *testing.T) {
@ -245,7 +280,7 @@ func TestRouterStaticGpath(t *testing.T) {
defer hsrv.Close()
time.Sleep(1 * time.Second)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`)
check(t, hsrv.Addr, "http://%s/api/v0/test/call/TEST", `{"msg":"Hello TEST"}`, false)
}
func TestRouterStaticPcreInvalid(t *testing.T) {

8
go.mod
View File

@ -9,13 +9,13 @@ require (
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/client_model v0.2.0
github.com/stretchr/testify v1.6.1
github.com/unistack-org/micro-api-handler-rpc v0.0.0-20210109162638-64670d8535df
github.com/unistack-org/micro-api-router-registry v0.0.0-20210110010133-d6468aa91d66
github.com/unistack-org/micro-api-router-static v0.0.0-20210109231342-6a822df8185c
github.com/unistack-org/micro-api-handler-rpc v0.0.0-20210113163127-3f36e7b3d99a
github.com/unistack-org/micro-api-router-registry v0.0.0-20210110113004-b9ccb4324370
github.com/unistack-org/micro-api-router-static v0.0.0-20210110113147-58f8ed2f7347
github.com/unistack-org/micro-broker-http v0.0.0-20201125231853-bb4bd204b8c0
github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd
github.com/unistack-org/micro-client-grpc v0.0.2-0.20201228123319-bbd07bb0914a
github.com/unistack-org/micro-client-http v0.0.0-20210105043204-d541c7cc54f5
github.com/unistack-org/micro-client-http v0.0.0-20210110114810-59d77f7b8cf9
github.com/unistack-org/micro-codec-grpc v0.0.0-20201220205513-cad30014cbf2
github.com/unistack-org/micro-codec-json v0.0.0-20201220205604-ed33fab21d87
github.com/unistack-org/micro-codec-proto v0.0.0-20201220205718-066176ab59b7

8
go.sum
View File

@ -426,10 +426,16 @@ github.com/transip/gotransip v0.0.0-20190812104329-6d8d9179b66f/go.mod h1:i0f4R4
github.com/uber-go/atomic v1.3.2/go.mod h1:/Ct5t2lcmbJ4OSe/waGBoaVvVqtO0bmtfVNex1PFV8g=
github.com/unistack-org/micro-api-handler-rpc v0.0.0-20210109162638-64670d8535df h1:DHlto3KT/EZ+rVQztapOvh07iWbF/8p2FFRxy0knmMY=
github.com/unistack-org/micro-api-handler-rpc v0.0.0-20210109162638-64670d8535df/go.mod h1:z1hfVzV132MuyGjUY+mqfiSkjZiFnfRTqCC87puYDF4=
github.com/unistack-org/micro-api-handler-rpc v0.0.0-20210113163127-3f36e7b3d99a h1:+0v96L2UKv/+12B9HkrdiwUC2g8jIm5oV4/yCQu79YE=
github.com/unistack-org/micro-api-handler-rpc v0.0.0-20210113163127-3f36e7b3d99a/go.mod h1:z1hfVzV132MuyGjUY+mqfiSkjZiFnfRTqCC87puYDF4=
github.com/unistack-org/micro-api-router-registry v0.0.0-20210110010133-d6468aa91d66 h1:FoCCu5zME0rvxmftdz69UA/KhoYNTEt3OCcd6g4eYvE=
github.com/unistack-org/micro-api-router-registry v0.0.0-20210110010133-d6468aa91d66/go.mod h1:yOm0OvGBKnSan5fgIrhHMZh0P3pHAEIJkjniZaRghc8=
github.com/unistack-org/micro-api-router-registry v0.0.0-20210110113004-b9ccb4324370 h1:rlEYcsTtjrJ7r8gK93gSHTe5RSdZQh2nReLpngs/U+0=
github.com/unistack-org/micro-api-router-registry v0.0.0-20210110113004-b9ccb4324370/go.mod h1:yOm0OvGBKnSan5fgIrhHMZh0P3pHAEIJkjniZaRghc8=
github.com/unistack-org/micro-api-router-static v0.0.0-20210109231342-6a822df8185c h1:bRESfIXdXknwxm3Vu9d3fZv16sSQTuFUP6iNqBs1qmk=
github.com/unistack-org/micro-api-router-static v0.0.0-20210109231342-6a822df8185c/go.mod h1:X+ufm/HOyg3rFiyi9nmBam/azXw++Zk17F0Q/nuhwAg=
github.com/unistack-org/micro-api-router-static v0.0.0-20210110113147-58f8ed2f7347 h1:zJVHLFmrsMa4fWEiIu3HtZncjRfBZMEJhIMHFD757kg=
github.com/unistack-org/micro-api-router-static v0.0.0-20210110113147-58f8ed2f7347/go.mod h1:X+ufm/HOyg3rFiyi9nmBam/azXw++Zk17F0Q/nuhwAg=
github.com/unistack-org/micro-broker-http v0.0.0-20201125231853-bb4bd204b8c0 h1:5jC+fK9XYUo8SnoVPVtGvYLMei6fuJuIM0NK07jcqdo=
github.com/unistack-org/micro-broker-http v0.0.0-20201125231853-bb4bd204b8c0/go.mod h1:WtKt1GCpw7WSbF83g1rg020u9uJiwF61c+dAHMLLfI0=
github.com/unistack-org/micro-broker-memory v0.0.2-0.20201105185131-5ff932308afd h1:LNMB1G3yZEI1zpX4SGodLqUfOw4Zan30rcpcvKTDMCI=
@ -438,6 +444,8 @@ github.com/unistack-org/micro-client-grpc v0.0.2-0.20201228123319-bbd07bb0914a h
github.com/unistack-org/micro-client-grpc v0.0.2-0.20201228123319-bbd07bb0914a/go.mod h1:+/sN0ylUHLaDODABbxPBx8y7WUY8PUjzMF1L7cmNk4M=
github.com/unistack-org/micro-client-http v0.0.0-20210105043204-d541c7cc54f5 h1:uoL2SK2HiXAReHaemoTLSxmNQvV+bzvP6Ci8V0wsN+8=
github.com/unistack-org/micro-client-http v0.0.0-20210105043204-d541c7cc54f5/go.mod h1:vb+5C+r23Q8H5FTpcwGT3XwaenKeGBY7SMcFo+UUV4k=
github.com/unistack-org/micro-client-http v0.0.0-20210110114810-59d77f7b8cf9 h1:AZWjvFu+xnjC4jrF1cL5UcDH6YdIUj6IOefSIKfP5sY=
github.com/unistack-org/micro-client-http v0.0.0-20210110114810-59d77f7b8cf9/go.mod h1:vb+5C+r23Q8H5FTpcwGT3XwaenKeGBY7SMcFo+UUV4k=
github.com/unistack-org/micro-codec-bytes v0.0.0-20200827104921-3616a69473a6/go.mod h1:g5sOI8TWgGZiVHe8zoUPdtz7+0oLnqTnfBoai6Qb7jE=
github.com/unistack-org/micro-codec-bytes v0.0.0-20200828083432-4e49e953d844/go.mod h1:g5sOI8TWgGZiVHe8zoUPdtz7+0oLnqTnfBoai6Qb7jE=
github.com/unistack-org/micro-codec-grpc v0.0.0-20201220205513-cad30014cbf2 h1:pfkUfnSi5kSkZkHBcqOKCm/Dv/IcMwMxWlei8T+5ADc=