endpoints_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package targets
  2. import (
  3. "net/url"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "google.golang.org/grpc/resolver"
  7. )
  8. func TestGetAuthority(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. url string
  12. want string
  13. }{
  14. {
  15. name: "test",
  16. url: "direct://my_authority/localhost",
  17. want: "my_authority",
  18. },
  19. {
  20. name: "test with port",
  21. url: "direct://my_authority/localhost:8080",
  22. want: "my_authority",
  23. },
  24. {
  25. name: "test with multiple hosts",
  26. url: "direct://my_authority1,my_authority2/localhost,localhost",
  27. want: "my_authority1,my_authority2",
  28. },
  29. {
  30. name: "test with multiple hosts with port",
  31. url: "direct://my_authority1:3000,my_authority2:3001/localhost:8080,localhost:8081",
  32. want: "my_authority1:3000,my_authority2:3001",
  33. },
  34. }
  35. for _, test := range tests {
  36. t.Run(test.name, func(t *testing.T) {
  37. uri, err := url.Parse(test.url)
  38. assert.Nil(t, err)
  39. target := resolver.Target{
  40. URL: *uri,
  41. }
  42. assert.Equal(t, test.want, GetAuthority(target))
  43. })
  44. }
  45. }
  46. func TestGetEndpoints(t *testing.T) {
  47. tests := []struct {
  48. name string
  49. url string
  50. want string
  51. }{
  52. {
  53. name: "test",
  54. url: "direct:///localhost",
  55. want: "localhost",
  56. },
  57. {
  58. name: "test with port",
  59. url: "direct:///localhost:8080",
  60. want: "localhost:8080",
  61. },
  62. {
  63. name: "test with multiple hosts",
  64. url: "direct:///localhost,localhost",
  65. want: "localhost,localhost",
  66. },
  67. {
  68. name: "test with multiple hosts with port",
  69. url: "direct:///localhost:8080,localhost:8081",
  70. want: "localhost:8080,localhost:8081",
  71. },
  72. }
  73. for _, test := range tests {
  74. t.Run(test.name, func(t *testing.T) {
  75. uri, err := url.Parse(test.url)
  76. assert.Nil(t, err)
  77. target := resolver.Target{
  78. URL: *uri,
  79. }
  80. assert.Equal(t, test.want, GetEndpoints(target))
  81. })
  82. }
  83. }