add health handler tests
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -3,20 +3,25 @@ package grpc_test
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
gclient "github.com/unistack-org/micro-client-grpc/v3"
|
||||
protocodec "github.com/unistack-org/micro-codec-proto/v3"
|
||||
regRouter "github.com/unistack-org/micro-router-register/v3"
|
||||
gserver "github.com/unistack-org/micro-server-grpc/v3"
|
||||
httpsrv "github.com/unistack-org/micro-server-http/v3"
|
||||
gpb "github.com/unistack-org/micro-tests/server/grpc/gproto"
|
||||
pb "github.com/unistack-org/micro-tests/server/grpc/proto"
|
||||
"github.com/unistack-org/micro/v3/broker"
|
||||
"github.com/unistack-org/micro/v3/client"
|
||||
"github.com/unistack-org/micro/v3/codec"
|
||||
"github.com/unistack-org/micro/v3/errors"
|
||||
"github.com/unistack-org/micro/v3/register"
|
||||
"github.com/unistack-org/micro/v3/router"
|
||||
"github.com/unistack-org/micro/v3/server"
|
||||
health "github.com/unistack-org/micro/v3/server/health"
|
||||
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
@@ -46,20 +51,40 @@ func (g *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response
|
||||
func TestGRPCServer(t *testing.T) {
|
||||
var err error
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
r := register.NewRegister()
|
||||
b := broker.NewBroker(broker.Register(r))
|
||||
s := gserver.NewServer(server.Codec("application/grpc+proto", protocodec.NewCodec()), server.Address(":0"), server.Register(r), server.Name("helloworld"), gserver.Reflection(true),
|
||||
s := gserver.NewServer(
|
||||
server.Codec("application/grpc+proto", protocodec.NewCodec()),
|
||||
server.Address(":0"), server.Register(r), server.Name("helloworld"), gserver.Reflection(true),
|
||||
server.WrapHandler(NewServerHandlerWrapper()),
|
||||
)
|
||||
// create router
|
||||
rtr := regRouter.NewRouter(router.Register(r))
|
||||
|
||||
h := &testServer{}
|
||||
err = gpb.RegisterTestServer(s, h)
|
||||
if err != nil {
|
||||
if err = gpb.RegisterTestServer(s, h); err != nil {
|
||||
t.Fatalf("can't register handler: %v", err)
|
||||
}
|
||||
|
||||
srv := httpsrv.NewServer(
|
||||
server.Address(":0"),
|
||||
server.Codec("text/plain", codec.NewCodec()),
|
||||
)
|
||||
if err = health.RegisterHealthServer(srv, health.NewHandler(health.Version("0.0.1"))); err != nil {
|
||||
t.Fatalf("cant register health handler: %v", err)
|
||||
}
|
||||
|
||||
if err = srv.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = srv.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = s.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -69,11 +94,32 @@ func TestGRPCServer(t *testing.T) {
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err = srv.Stop(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err = s.Stop(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
hr, err := http.NewRequestWithContext(ctx, "GET", "http://"+srv.Options().Address+"/version", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hr.Header.Set("Content-Type", "text/plain")
|
||||
rsp, err := http.DefaultClient.Do(hr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer rsp.Body.Close()
|
||||
buf, err := io.ReadAll(rsp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if string(buf) != "0.0.1" {
|
||||
t.Fatalf("unknown version returned from health handler: %s", buf)
|
||||
}
|
||||
|
||||
// create client
|
||||
c := gclient.NewClient(client.Codec("application/grpc+proto", protocodec.NewCodec()), client.Router(rtr), client.Register(r), client.Broker(b))
|
||||
|
||||
|
Reference in New Issue
Block a user