code.gitea.io/gitea@v1.21.7/routers/api/actions/ping/ping_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package ping
     5  
     6  import (
     7  	"context"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	pingv1 "code.gitea.io/actions-proto-go/ping/v1"
    13  	"code.gitea.io/actions-proto-go/ping/v1/pingv1connect"
    14  	"github.com/bufbuild/connect-go"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestService(t *testing.T) {
    20  	mux := http.NewServeMux()
    21  	mux.Handle(pingv1connect.NewPingServiceHandler(
    22  		&Service{},
    23  	))
    24  	MainServiceTest(t, mux)
    25  }
    26  
    27  func MainServiceTest(t *testing.T, h http.Handler) {
    28  	t.Parallel()
    29  	server := httptest.NewUnstartedServer(h)
    30  	server.EnableHTTP2 = true
    31  	server.StartTLS()
    32  	defer server.Close()
    33  
    34  	connectClient := pingv1connect.NewPingServiceClient(
    35  		server.Client(),
    36  		server.URL,
    37  	)
    38  
    39  	grpcClient := pingv1connect.NewPingServiceClient(
    40  		server.Client(),
    41  		server.URL,
    42  		connect.WithGRPC(),
    43  	)
    44  
    45  	grpcWebClient := pingv1connect.NewPingServiceClient(
    46  		server.Client(),
    47  		server.URL,
    48  		connect.WithGRPCWeb(),
    49  	)
    50  
    51  	clients := []pingv1connect.PingServiceClient{connectClient, grpcClient, grpcWebClient}
    52  	t.Run("ping request", func(t *testing.T) {
    53  		for _, client := range clients {
    54  			result, err := client.Ping(context.Background(), connect.NewRequest(&pingv1.PingRequest{
    55  				Data: "foobar",
    56  			}))
    57  			require.NoError(t, err)
    58  			assert.Equal(t, "Hello, foobar!", result.Msg.Data)
    59  		}
    60  	})
    61  }