director_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httpproxy
  15. import (
  16. "net/url"
  17. "reflect"
  18. "sort"
  19. "testing"
  20. "time"
  21. )
  22. func TestNewDirectorScheme(t *testing.T) {
  23. tests := []struct {
  24. urls []string
  25. want []string
  26. }{
  27. {
  28. urls: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
  29. want: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
  30. },
  31. {
  32. urls: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
  33. want: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
  34. },
  35. // accept urls without a port
  36. {
  37. urls: []string{"http://192.0.2.8"},
  38. want: []string{"http://192.0.2.8"},
  39. },
  40. // accept urls even if they are garbage
  41. {
  42. urls: []string{"http://."},
  43. want: []string{"http://."},
  44. },
  45. }
  46. for i, tt := range tests {
  47. uf := func() []string {
  48. return tt.urls
  49. }
  50. got := newDirector(uf, time.Minute, time.Minute)
  51. var gep []string
  52. for _, ep := range got.ep {
  53. gep = append(gep, ep.URL.String())
  54. }
  55. sort.Strings(tt.want)
  56. sort.Strings(gep)
  57. if !reflect.DeepEqual(tt.want, gep) {
  58. t.Errorf("#%d: want endpoints = %#v, got = %#v", i, tt.want, gep)
  59. }
  60. }
  61. }
  62. func TestDirectorEndpointsFiltering(t *testing.T) {
  63. d := director{
  64. ep: []*endpoint{
  65. {
  66. URL: url.URL{Scheme: "http", Host: "192.0.2.5:5050"},
  67. Available: false,
  68. },
  69. {
  70. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  71. Available: true,
  72. },
  73. },
  74. }
  75. got := d.endpoints()
  76. want := []*endpoint{
  77. {
  78. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  79. Available: true,
  80. },
  81. }
  82. if !reflect.DeepEqual(want, got) {
  83. t.Fatalf("directed to incorrect endpoint: want = %#v, got = %#v", want, got)
  84. }
  85. }