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