-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_test.go
168 lines (153 loc) · 4.44 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
package main
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"net/url"
"reflect"
"testing"
"testing/fstest"
"github.com/prometheus/client_golang/api"
)
const (
validBearerToken = "Any Regular ASCII string"
validBearerHeader = "Bearer " + validBearerToken
)
// mockClient does a poor job at mocking prometheus api.Client
type mockClient struct {
header http.Header
reqURL *url.URL
}
// URL implements api.Client
func (m *mockClient) URL(ep string, args map[string]string) *url.URL {
return nil
}
// Do implements api.Client
func (m *mockClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
m.header = req.Header
m.reqURL = req.URL
return nil, nil, nil
}
// TestBearerToken validates bearer token reading and insertion
func TestBearerToken(t *testing.T) {
fsys := fstest.MapFS{
"empty_bearer": {
Data: []byte{},
},
"empty_first_line_bearer": {
Data: []byte("\nFirst line cannot be emtpy"),
},
"invalid_bearer": {
Data: []byte("bearer must be ascii: áéíóú"),
},
"valid_bearer": {
Data: []byte(validBearerToken),
},
"valid_bearer_with_whitespace": {
Data: []byte(" " + validBearerToken + " "),
},
}
read := func(fileName string) (*mockClient, api.Client, error) {
m := &mockClient{}
b, err := readBearerToken(fsys, fileName)
if err != nil {
return m, nil, err
}
c, err := newClient(m, withToken(b))
return m, c, err
}
// Check invalid files raise proper errors
invalid := map[string]error{
"empty_bearer": EmptyBearerFileError,
"empty_first_line_bearer": InvalidBearerTokenError,
"invalid_bearer": InvalidBearerTokenError,
}
for fileName, expectedErr := range invalid {
t.Run("Read bearer from "+fileName, func(t *testing.T) {
_, _, err := read(fileName)
if !errors.Is(err, expectedErr) {
t.Fatalf("Expected error %v, got %v", expectedErr, err)
}
})
}
// Check valid files send proper token
valid := []string{
"valid_bearer",
"valid_bearer_with_whitespace",
}
for _, fileName := range valid {
t.Run("Read bearer from "+fileName, func(t *testing.T) {
m, c, err := read(fileName)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
req, _ := http.NewRequest(http.MethodGet, "http://localhost/test", nil)
_, _, err = c.Do(context.TODO(), req)
if err != nil {
t.Fatal("Error:", err)
}
if m.header == nil || len(m.header) <= 0 {
t.Fatal("Empty headers in request")
}
if authz := m.header.Get("Authorization"); authz != validBearerHeader {
t.Fatalf("Expected Authorization header '%s', got '%s'", validBearerHeader, authz)
}
})
}
}
// TestWithGet validates GET request filtering
func TestWithGet(t *testing.T) {
post := func(t *testing.T, opts ...clientOption) *http.Response {
c, err := newClient(&mockClient{}, opts...)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
req, _ := http.NewRequest(http.MethodPost, "http://localhost/test", io.NopCloser(bytes.NewReader([]byte{})))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _, err := c.Do(context.TODO(), req)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
return res
}
t.Run("regular client permits POST", func(t *testing.T) {
if res := post(t); res != nil {
t.Fatalf("Expected nil response (from mock), got %v", res)
}
})
t.Run("withGet client rejects POST", func(t *testing.T) {
res := post(t, withGet)
if res == nil {
t.Fatal("Expected not-nil response")
}
if res.StatusCode != http.StatusMethodNotAllowed {
t.Fatalf("Expected status code %d, got %v", http.StatusMethodNotAllowed, res)
}
})
}
// TestWithValues validates params passing through context
func TestWithValues(t *testing.T) {
m := mockClient{}
c, err := newClient(&m)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
v, _ := url.ParseQuery("key1=val1&key2=val2")
ctx := addValues(context.TODO(), v)
req, _ := http.NewRequest(http.MethodGet, "http://localhost/test", nil)
if _, _, err := c.Do(ctx, req); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
urlQuery := m.reqURL.Query()
if !reflect.DeepEqual(v, urlQuery) {
t.Fatalf("Expected query parameters %v, got %v", v, urlQuery)
}
}
// TestNilOption makes sure config doesn't panic if a nil option is passed
func TestNilOption(t *testing.T) {
if _, err := newClient(&mockClient{}, nil); !errors.Is(err, NilOptionError) {
t.Fatalf("Expected error %v, got %v", NilOptionError, err)
}
}