From c10414a018735335b43e901cf81a17d8784c9cbd Mon Sep 17 00:00:00 2001 From: "Bo-Yi.Wu" Date: Mon, 22 Aug 2022 21:35:56 +0800 Subject: [PATCH] test(ping): add testing for gRPC --- routers/api/bots/bots.go | 4 ++- routers/api/bots/bots_test.go | 16 +++++++++ routers/api/bots/ping/ping_test.go | 20 +++++++++++ routers/api/bots/ping/testping.go | 54 ++++++++++++++++++++++++++++++ routers/init.go | 2 +- 5 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 routers/api/bots/bots_test.go create mode 100644 routers/api/bots/ping/ping_test.go create mode 100644 routers/api/bots/ping/testping.go diff --git a/routers/api/bots/bots.go b/routers/api/bots/bots.go index 3c761eeffa..bddd4c92eb 100644 --- a/routers/api/bots/bots.go +++ b/routers/api/bots/bots.go @@ -24,7 +24,7 @@ func gRPCRouter(r *web.Route, fn grpc.RouteFn) { r.Post(p+"{name}", grpcHandler(h)) } -func Routes(r *web.Route) { +func Routes(r *web.Route) *web.Route { // socket connection r.Get("/socket", socketServe) @@ -33,4 +33,6 @@ func Routes(r *web.Route) { gRPCRouter(r, grpc.HealthRoute) gRPCRouter(r, grpc.PingRoute) gRPCRouter(r, grpc.RunnerRoute) + + return r } diff --git a/routers/api/bots/bots_test.go b/routers/api/bots/bots_test.go new file mode 100644 index 0000000000..6ccb0ea3ec --- /dev/null +++ b/routers/api/bots/bots_test.go @@ -0,0 +1,16 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package bots + +import ( + "testing" + + "code.gitea.io/gitea/modules/web" + "code.gitea.io/gitea/routers/api/bots/ping" +) + +func TestPingService(t *testing.T) { + ping.MainServiceTest(t, Routes(web.NewRoute())) +} diff --git a/routers/api/bots/ping/ping_test.go b/routers/api/bots/ping/ping_test.go new file mode 100644 index 0000000000..30a9262bdc --- /dev/null +++ b/routers/api/bots/ping/ping_test.go @@ -0,0 +1,20 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package ping + +import ( + "net/http" + "testing" + + "gitea.com/gitea/proto-go/ping/v1/pingv1connect" +) + +func TestService(t *testing.T) { + mux := http.NewServeMux() + mux.Handle(pingv1connect.NewPingServiceHandler( + &Service{}, + )) + MainServiceTest(t, mux) +} diff --git a/routers/api/bots/ping/testping.go b/routers/api/bots/ping/testping.go new file mode 100644 index 0000000000..3398b30579 --- /dev/null +++ b/routers/api/bots/ping/testping.go @@ -0,0 +1,54 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package ping + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + pingv1 "gitea.com/gitea/proto-go/ping/v1" + "gitea.com/gitea/proto-go/ping/v1/pingv1connect" + + "github.com/bufbuild/connect-go" + "github.com/stretchr/testify/assert" +) + +func MainServiceTest(t *testing.T, h http.Handler) { + t.Parallel() + server := httptest.NewUnstartedServer(h) + server.EnableHTTP2 = true + server.StartTLS() + defer server.Close() + + connectClient := pingv1connect.NewPingServiceClient( + server.Client(), + server.URL, + ) + + grpcClient := pingv1connect.NewPingServiceClient( + server.Client(), + server.URL, + connect.WithGRPC(), + ) + + grpcWebClient := pingv1connect.NewPingServiceClient( + server.Client(), + server.URL, + connect.WithGRPCWeb(), + ) + + clients := []pingv1connect.PingServiceClient{connectClient, grpcClient, grpcWebClient} + t.Run("ping request", func(t *testing.T) { + for _, client := range clients { + result, err := client.Ping(context.Background(), connect.NewRequest(&pingv1.PingRequest{ + Data: "foobar", + })) + assert.NoError(t, err) + assert.Equal(t, "Hello, foobar!", result.Msg.Data) + } + }) +} diff --git a/routers/init.go b/routers/init.go index aef71792f5..65bf7b55d8 100644 --- a/routers/init.go +++ b/routers/init.go @@ -198,6 +198,6 @@ func NormalRoutes(ctx context.Context) *web.Route { // This implements the OCI API (Note this is not preceded by /api but is instead /v2) r.Mount("/v2", packages_router.ContainerRoutes(ctx)) } - bots_router.Routes(r) + _ = bots_router.Routes(r) return r }