director_test.go 2.2 KB

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