go.uber.org/yarpc@v1.72.1/transport/grpc/util_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package grpc
    22  
    23  import (
    24  	"net/url"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestProcedureNameFunctionsBidirectional(t *testing.T) {
    32  	t.Parallel()
    33  	for _, tt := range []struct {
    34  		ProcedureName string
    35  		ServiceName   string
    36  		MethodName    string
    37  		FullMethod    string
    38  	}{
    39  		{
    40  			ProcedureName: "KeyValue::GetValue",
    41  			ServiceName:   "KeyValue",
    42  			MethodName:    "GetValue",
    43  			FullMethod:    "/KeyValue/GetValue",
    44  		},
    45  		{
    46  			ProcedureName: "foo",
    47  			ServiceName:   "__default__",
    48  			MethodName:    "foo",
    49  			FullMethod:    "/__default__/foo",
    50  		},
    51  		{
    52  			ProcedureName: "foo/bar",
    53  			ServiceName:   "__default__",
    54  			MethodName:    url.QueryEscape("foo/bar"),
    55  			FullMethod:    "/__default__/" + url.QueryEscape("foo/bar"),
    56  		},
    57  	} {
    58  		t.Run(tt.ProcedureName, func(t *testing.T) {
    59  			serviceName, methodName, err := procedureNameToServiceNameMethodName(tt.ProcedureName)
    60  			assert.NoError(t, err)
    61  			assert.Equal(t, tt.ServiceName, serviceName)
    62  			assert.Equal(t, tt.MethodName, methodName)
    63  			procedureName, err := procedureToName(serviceName, methodName)
    64  			assert.NoError(t, err)
    65  			assert.Equal(t, tt.ProcedureName, procedureName)
    66  			assert.Equal(t, tt.FullMethod, toFullMethod(serviceName, methodName))
    67  		})
    68  	}
    69  }
    70  
    71  func TestProcedureNameEmpty(t *testing.T) {
    72  	_, _, err := procedureNameToServiceNameMethodName("")
    73  	require.Error(t, err)
    74  	_, err = procedureNameToFullMethod("")
    75  	require.Error(t, err)
    76  }
    77  
    78  func TestToFullMethodEmptyServiceName(t *testing.T) {
    79  	require.Equal(t, "/__default__/foo", toFullMethod("", "foo"))
    80  }
    81  
    82  func TestProcedureToNameInvalidNames(t *testing.T) {
    83  	_, err := procedureToName("%%A", "foo")
    84  	require.Error(t, err)
    85  	_, err = procedureToName("foo", "%%A")
    86  	require.Error(t, err)
    87  }