github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/transport/internet/grpc/config_test.go (about)

     1  package grpc
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestConfig_GetServiceName(t *testing.T) {
    10  	tests := []struct {
    11  		TestName    string
    12  		ServiceName string
    13  		Expected    string
    14  	}{
    15  		{
    16  			TestName:    "simple no absolute path",
    17  			ServiceName: "hello",
    18  			Expected:    "hello",
    19  		},
    20  		{
    21  			TestName:    "escape no absolute path",
    22  			ServiceName: "hello/world!",
    23  			Expected:    "hello%2Fworld%21",
    24  		},
    25  		{
    26  			TestName:    "absolute path",
    27  			ServiceName: "/my/sample/path/a|b",
    28  			Expected:    "my/sample/path",
    29  		},
    30  		{
    31  			TestName:    "escape absolute path",
    32  			ServiceName: "/hello /world!/a|b",
    33  			Expected:    "hello%20/world%21",
    34  		},
    35  		{
    36  			TestName:    "path with only one '/'",
    37  			ServiceName: "/foo",
    38  			Expected:    "",
    39  		},
    40  	}
    41  	for _, test := range tests {
    42  		t.Run(test.TestName, func(t *testing.T) {
    43  			config := Config{ServiceName: test.ServiceName}
    44  			assert.Equal(t, test.Expected, config.getServiceName())
    45  		})
    46  	}
    47  }
    48  
    49  func TestConfig_GetTunStreamName(t *testing.T) {
    50  	tests := []struct {
    51  		TestName    string
    52  		ServiceName string
    53  		Expected    string
    54  	}{
    55  		{
    56  			TestName:    "no absolute path",
    57  			ServiceName: "hello",
    58  			Expected:    "Tun",
    59  		},
    60  		{
    61  			TestName:    "absolute path server",
    62  			ServiceName: "/my/sample/path/tun_service|multi_service",
    63  			Expected:    "tun_service",
    64  		},
    65  		{
    66  			TestName:    "absolute path client",
    67  			ServiceName: "/my/sample/path/tun_service",
    68  			Expected:    "tun_service",
    69  		},
    70  		{
    71  			TestName:    "escape absolute path client",
    72  			ServiceName: "/m y/sa !mple/pa\\th/tun\\_serv!ice",
    73  			Expected:    "tun%5C_serv%21ice",
    74  		},
    75  	}
    76  	for _, test := range tests {
    77  		t.Run(test.TestName, func(t *testing.T) {
    78  			config := Config{ServiceName: test.ServiceName}
    79  			assert.Equal(t, test.Expected, config.getTunStreamName())
    80  		})
    81  	}
    82  }
    83  
    84  func TestConfig_GetTunMultiStreamName(t *testing.T) {
    85  	tests := []struct {
    86  		TestName    string
    87  		ServiceName string
    88  		Expected    string
    89  	}{
    90  		{
    91  			TestName:    "no absolute path",
    92  			ServiceName: "hello",
    93  			Expected:    "TunMulti",
    94  		},
    95  		{
    96  			TestName:    "absolute path server",
    97  			ServiceName: "/my/sample/path/tun_service|multi_service",
    98  			Expected:    "multi_service",
    99  		},
   100  		{
   101  			TestName:    "absolute path client",
   102  			ServiceName: "/my/sample/path/multi_service",
   103  			Expected:    "multi_service",
   104  		},
   105  		{
   106  			TestName:    "escape absolute path client",
   107  			ServiceName: "/m y/sa !mple/pa\\th/mu%lti\\_serv!ice",
   108  			Expected:    "mu%25lti%5C_serv%21ice",
   109  		},
   110  	}
   111  	for _, test := range tests {
   112  		t.Run(test.TestName, func(t *testing.T) {
   113  			config := Config{ServiceName: test.ServiceName}
   114  			assert.Equal(t, test.Expected, config.getTunMultiStreamName())
   115  		})
   116  	}
   117  }