director_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 CoreOS, Inc.
  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 proxy
  15. import (
  16. "net/url"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestNewDirectorScheme(t *testing.T) {
  21. tests := []struct {
  22. urls []string
  23. want []string
  24. }{
  25. {
  26. urls: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
  27. want: []string{"http://192.0.2.8:4002", "http://example.com:8080"},
  28. },
  29. {
  30. urls: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
  31. want: []string{"https://192.0.2.8:4002", "https://example.com:8080"},
  32. },
  33. // accept urls without a port
  34. {
  35. urls: []string{"http://192.0.2.8"},
  36. want: []string{"http://192.0.2.8"},
  37. },
  38. // accept urls even if they are garbage
  39. {
  40. urls: []string{"http://."},
  41. want: []string{"http://."},
  42. },
  43. }
  44. for i, tt := range tests {
  45. uf := func() []string {
  46. return tt.urls
  47. }
  48. got := newDirector(uf)
  49. for ii, wep := range tt.want {
  50. gep := got.ep[ii].URL.String()
  51. if !reflect.DeepEqual(wep, gep) {
  52. t.Errorf("#%d: want endpoints[%d] = %#v, got = %#v", i, ii, wep, gep)
  53. }
  54. }
  55. }
  56. }
  57. func TestDirectorEndpointsFiltering(t *testing.T) {
  58. d := director{
  59. ep: []*endpoint{
  60. &endpoint{
  61. URL: url.URL{Scheme: "http", Host: "192.0.2.5:5050"},
  62. Available: false,
  63. },
  64. &endpoint{
  65. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  66. Available: true,
  67. },
  68. },
  69. }
  70. got := d.endpoints()
  71. want := []*endpoint{
  72. &endpoint{
  73. URL: url.URL{Scheme: "http", Host: "192.0.2.4:4000"},
  74. Available: true,
  75. },
  76. }
  77. if !reflect.DeepEqual(want, got) {
  78. t.Fatalf("directed to incorrect endpoint: want = %#v, got = %#v", want, got)
  79. }
  80. }