config_test.go 2.5 KB

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