director_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package proxy
  2. import (
  3. "net/url"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestNewDirectorScheme(t *testing.T) {
  8. tests := []struct {
  9. scheme string
  10. addrs []string
  11. want []string
  12. }{
  13. {
  14. scheme: "http",
  15. addrs: []string{"192.0.2.8:4002", "example.com:8080"},
  16. want: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
  17. },
  18. {
  19. scheme: "https",
  20. addrs: []string{"192.0.2.8:4002", "example.com:8080"},
  21. want: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
  22. },
  23. // accept addrs without a port
  24. {
  25. scheme: "http",
  26. addrs: []string{"192.0.2.8"},
  27. want: []string{"http://192.0.2.8"},
  28. },
  29. // accept addrs even if they are garbage
  30. {
  31. scheme: "http",
  32. addrs: []string{"."},
  33. want: []string{"http://."},
  34. },
  35. }
  36. for i, tt := range tests {
  37. got, err := newDirector(tt.scheme, tt.addrs)
  38. if err != nil {
  39. t.Errorf("#%d: newDirectory returned unexpected error: %v", i, err)
  40. }
  41. for ii, wep := range tt.want {
  42. gep := got.ep[ii].URL.String()
  43. if !reflect.DeepEqual(wep, gep) {
  44. t.Errorf("#%d: want endpoints[%d] = %#v, got = %#v", i, ii, wep, gep)
  45. }
  46. }
  47. }
  48. }
  49. func TestDirectorEndpointsFiltering(t *testing.T) {
  50. d := director{
  51. ep: []*endpoint{
  52. &endpoint{
  53. URL: url.URL{Scheme: "http", Host: "192.0.2.5:5050"},
  54. Available: false,
  55. },
  56. &endpoint{
  57. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  58. Available: true,
  59. },
  60. },
  61. }
  62. got := d.endpoints()
  63. want := []*endpoint{
  64. &endpoint{
  65. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  66. Available: true,
  67. },
  68. }
  69. if !reflect.DeepEqual(want, got) {
  70. t.Fatalf("directed to incorrect endpoint: want = %#v, got = %#v", want, got)
  71. }
  72. }