-
Notifications
You must be signed in to change notification settings - Fork 84
/
client_test.go
199 lines (181 loc) · 5.2 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package kubernetes
import (
"crypto/tls"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestClientGet(t *testing.T) {
for _, test := range []struct {
baseURL string
resource string
responseBody string
responseCode int
wantError bool
}{
{"", "/foo", "foo", http.StatusOK, false},
{"", "/bar", "bar", http.StatusNotFound, true},
{"", "/zbr", "xpto", http.StatusInternalServerError, true},
{"http://192.168.0.%31", "/fail", "xpto", http.StatusOK, true},
} {
t.Run(fmt.Sprintf("%d%v", test.responseCode, test.resource), func(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("unexpected HTTP method. wanted GET, got %q", r.Method)
w.WriteHeader(http.StatusBadRequest)
}
if r.URL.Path != test.resource {
t.Errorf("unexpected URL path. wanted %q, got %q", test.resource, r.URL.Path)
w.WriteHeader(http.StatusBadRequest)
}
w.WriteHeader(test.responseCode)
_, err := io.WriteString(w, test.responseBody)
require.NoError(t, err)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
var baseURL = test.baseURL
if baseURL == "" {
baseURL = server.URL
}
cfg := &Config{BaseURL: baseURL}
c, _ := newSimpleClient(cfg, false)
r, err := c.get(test.resource)
if err != nil && !test.wantError {
t.Error("got unexpected error", err)
}
if !test.wantError {
b, err := io.ReadAll(r)
if err != nil {
t.Error("error reading response", err)
}
got := string(b)
if test.responseBody != got {
t.Errorf("unexpected response body. wanted %q, got %q\n", test.responseBody, got)
}
}
})
}
}
func TestClientPatch(t *testing.T) {
for _, test := range []struct {
baseURL string
resource string
payload []byte
responseBody string
responseCode int
wantError bool
}{
{"", "/foo", []byte("foo"), "ok", http.StatusOK, false},
{"", "/bar", []byte("bar"), "ok", http.StatusNotFound, true},
{"", "/zbr", []byte("xpto"), "nok", http.StatusInternalServerError, true},
{"http://192.168.0.%31", "/fail", []byte("fail"), "nok", http.StatusOK, true},
} {
t.Run(fmt.Sprintf("%d%v", test.responseCode, test.resource), func(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method != "PATCH" {
t.Errorf("unexpected HTTP method. wanted PATCH, got %q", r.Method)
w.WriteHeader(http.StatusBadRequest)
return
}
if r.URL.Path != test.resource {
t.Errorf("unexpected URL path. wanted %q, got %q", test.resource, r.URL.Path)
w.WriteHeader(http.StatusBadRequest)
return
}
b, err := io.ReadAll(r.Body)
if err != nil {
t.Error("failure reading request payload", err)
return
}
if !reflect.DeepEqual(b, test.payload) {
t.Errorf("unexpected request payload. wanted %v, got %v\n", test.payload, b)
}
w.WriteHeader(test.responseCode)
_, err = io.WriteString(w, test.responseBody)
require.NoError(t, err)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
var baseURL = test.baseURL
if baseURL == "" {
baseURL = server.URL
}
cfg := &Config{BaseURL: baseURL}
c, _ := newSimpleClient(cfg, false)
r, err := c.patch(test.resource, test.payload)
if err != nil && !test.wantError {
t.Error("got unexpected error", err)
}
if !test.wantError {
b, err := io.ReadAll(r)
if err != nil {
t.Error("error reading response", err)
}
got := string(b)
if test.responseBody != got {
t.Errorf("unexpected response body. wanted %q, got %q\n", test.responseBody, got)
}
}
})
}
}
func TestTLS(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token != "Bearer foo" {
t.Errorf(`wrong auth bearer token. wanted "Bearer: foo" but got %q`, token)
}
buf, err := io.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if string(buf) != "bar" {
t.Errorf(`unexpected request body. wanted "bar" but got %q`, string(buf))
}
w.WriteHeader(http.StatusOK)
_, err = io.Copy(w, r.Body)
require.NoError(t, err)
}
cert, err := tls.LoadX509KeyPair("testdata/cert.pem", "testdata/key.pem")
if err != nil {
t.Error(err)
}
certs := []tls.Certificate{cert}
server := httptest.NewUnstartedServer(http.HandlerFunc(handler))
server.TLS = &tls.Config{Certificates: certs}
server.StartTLS()
defer server.Close()
cfg := &Config{
BaseURL: server.URL,
UserAgent: "kube-ingress-aws-controller",
TokenProvider: mockSecretProvider("foo"),
TLSClientConfig: TLSClientConfig{CAFile: "testdata/cert.pem"},
Timeout: 5 * time.Second,
}
c, err := newSimpleClient(cfg, false)
if err != nil {
t.Error(err)
}
r, err := c.patch("/foo", []byte("bar"))
if err != nil {
t.Error(err)
} else {
defer r.Close()
}
}
// mock the SecretProvider from skipper module
type mockSecretProvider string
func (sp mockSecretProvider) GetSecret(_ string) ([]byte, bool) {
return []byte(sp), true
}
func (sp mockSecretProvider) Add(_ string) error {
return nil
}
func (sp mockSecretProvider) Close() {
}