mirror of
https://github.com/go-gitea/gitea.git
synced 2025-04-08 17:05:45 +02:00
test(ping): add testing for gRPC
This commit is contained in:
parent
9b4eb5f497
commit
c10414a018
@ -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
|
||||
}
|
||||
|
16
routers/api/bots/bots_test.go
Normal file
16
routers/api/bots/bots_test.go
Normal 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()))
|
||||
}
|
20
routers/api/bots/ping/ping_test.go
Normal file
20
routers/api/bots/ping/ping_test.go
Normal 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)
|
||||
}
|
54
routers/api/bots/ping/testping.go
Normal file
54
routers/api/bots/ping/testping.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user