test(ping): add testing for gRPC

This commit is contained in:
Bo-Yi.Wu 2022-08-22 21:35:56 +08:00 committed by Jason Song
parent 9b4eb5f497
commit c10414a018
5 changed files with 94 additions and 2 deletions

View File

@ -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
}

View File

@ -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()))
}

View File

@ -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)
}

View File

@ -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)
}
})
}

View File

@ -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
}