director_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package proxy
  2. import (
  3. "net/url"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestNewDirectorEndpointValidation(t *testing.T) {
  8. tests := []struct {
  9. good bool
  10. endpoints []string
  11. }{
  12. {true, []string{"http://192.0.2.8"}},
  13. {true, []string{"http://192.0.2.8:8001"}},
  14. {true, []string{"http://example.com"}},
  15. {true, []string{"http://example.com:8001"}},
  16. {true, []string{"http://192.0.2.8:8001", "http://example.com:8002"}},
  17. {false, []string{"://"}},
  18. {false, []string{"http://"}},
  19. {false, []string{"192.0.2.8"}},
  20. {false, []string{"192.0.2.8:8001"}},
  21. {false, []string{""}},
  22. {false, []string{}},
  23. }
  24. for i, tt := range tests {
  25. _, err := newDirector(tt.endpoints)
  26. if tt.good != (err == nil) {
  27. t.Errorf("#%d: expected success = %t, got err = %v", i, tt.good, err)
  28. }
  29. }
  30. }
  31. func TestDirectorEndpointsFiltering(t *testing.T) {
  32. d := director{
  33. ep: []*endpoint{
  34. &endpoint{
  35. URL: url.URL{Scheme: "http", Host: "192.0.2.5:5050"},
  36. Available: false,
  37. },
  38. &endpoint{
  39. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  40. Available: true,
  41. },
  42. },
  43. }
  44. got := d.endpoints()
  45. want := []*endpoint{
  46. &endpoint{
  47. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  48. Available: true,
  49. },
  50. }
  51. if !reflect.DeepEqual(want, got) {
  52. t.Fatalf("directed to incorrect endpoint: want = %#v, got = %#v", want, got)
  53. }
  54. }