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