-
Notifications
You must be signed in to change notification settings - Fork 0
/
lockbox_test.go
232 lines (212 loc) · 6.31 KB
/
lockbox_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package lockbox
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/go-uuid"
"github.com/nsf/jsondiff"
"yall.in"
testinglog "yall.in/testing"
"lockbox.dev/hmac"
)
type errorTest struct {
status int
body []byte
err error
}
func uuidOrFail(t *testing.T) string {
t.Helper()
id, err := uuid.GenerateUUID()
if err != nil {
t.Fatalf("Unexpected error generating ID: %s", err.Error())
}
return id
}
func mustWrite(t *testing.T, w http.ResponseWriter, b []byte) {
if _, err := w.Write(b); err != nil {
t.Errorf("Error writing response: %s", err)
}
}
func staticResponseServer(t *testing.T, code int, body []byte) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
mustWrite(t, w, body)
}))
}
func testClient(ctx context.Context, t *testing.T, baseURL string, auth ...AuthMethod) *Client {
client, err := NewClient(ctx, baseURL, auth...)
if err != nil {
t.Fatalf("Error creating client: %s", err)
}
if testing.Verbose() {
client.EnableLogs()
}
return client
}
func checkURL(t *testing.T, r *http.Request, expected string) {
t.Helper()
if r.URL.String() != expected {
t.Errorf("Expected URL to be %q, got %q", expected, r.URL.String())
}
}
func checkMethod(t *testing.T, r *http.Request, expected string) {
t.Helper()
if r.Method != expected {
t.Errorf("Expected method to be %q, got %q", expected, r.Method)
}
}
func checkJSONBody(t *testing.T, r *http.Request, expected string) {
t.Helper()
req, err := cloneRequest(r)
if err != nil {
t.Fatalf("Error cloning request: %s", err)
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Error reading body: %s", err)
}
defer func() {
if err = req.Body.Close(); err != nil {
t.Errorf("Error closing body: %v", err)
}
}()
opts := jsondiff.DefaultConsoleOptions()
diff, output := jsondiff.Compare(body, []byte(expected), &opts)
if diff != jsondiff.FullMatch {
t.Errorf("Body didn't match expectation: %s", output)
}
}
func checkURLFormEncodedBody(t *testing.T, r *http.Request, expected url.Values) {
t.Helper()
req, err := cloneRequest(r)
if err != nil {
t.Fatalf("Error cloning request: %s", err)
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Error reading body: %s", err)
}
defer func() {
if err = req.Body.Close(); err != nil {
t.Errorf("Error closing body: %v", err)
}
}()
got, err := url.ParseQuery(string(body))
if err != nil {
t.Fatalf("Error parsing body as URL-encoded query string: %s", err)
}
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("Body didn't match expectation (-wanted, +got): %s", diff)
}
}
func checkAuthorization(t *testing.T, r *http.Request, expected string) {
t.Helper()
got := r.Header.Get("Authorization")
if got != expected {
t.Errorf("Expected Authorization header to be %q, got %q", expected, got)
}
}
func checkBearerToken(t *testing.T, r *http.Request, expected string) {
t.Helper()
checkAuthorization(t, r, "Bearer "+expected)
}
func checkBasicAuth(t *testing.T, r *http.Request, username, password string) {
t.Helper()
user, pass, ok := r.BasicAuth()
if !ok && (username != "" || password != "") {
t.Errorf("username expected to be %q, password expected to be %q, but no basic auth set", username, password)
}
if ok && username == "" && password == "" {
t.Errorf("no basic auth expected to be set, but got username of %q and password of %q", user, pass)
}
if user != username {
t.Errorf("expected username to be %q, got %q", username, user)
}
if pass != password {
t.Errorf("expected password to be %q, got %q", password, pass)
}
}
func cloneRequest(r *http.Request) (*http.Request, error) {
req := r.Clone(context.Background())
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("Error reading request body: %w", err)
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(body))
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
return req, nil
}
func checkHMACAuthorization(t *testing.T, r *http.Request, auth HMACAuth) {
t.Helper()
req, err := cloneRequest(r)
if err != nil {
t.Fatalf("Error cloning request: %s", err)
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf("Error reading body: %s", err)
}
defer func() {
if err = req.Body.Close(); err != nil {
t.Errorf("Error closing body: %v", err)
}
}()
hash := base64.StdEncoding.EncodeToString(sha256.New().Sum(body))
signer := hmac.Signer{
Secret: auth.Secret,
MaxSkew: auth.MaxSkew,
OrgKey: auth.OrgKey,
Key: auth.Key,
}
err = signer.AuthenticateRequest(req, hash)
if err != nil {
t.Errorf("Error authenticating request: %s", err)
return
}
}
func TestUserAgent(t *testing.T) {
t.Parallel()
log := yall.New(testinglog.New(t, yall.Debug))
ctx := yall.InContext(context.Background(), log)
type testCase struct {
pre []string
app []string
expected string
}
base := "go-lockbox/" + getVersion()
t.Logf("User-Agent base: %q", base)
cases := map[string]testCase{
"default": {expected: base},
"prepend1": {pre: []string{"test/1.0.0"}, expected: "test/1.0.0 " + base},
"prepend3": {pre: []string{"test/1.0.0", "test2/1.2.3", "test3/4.5.6"}, expected: "test/1.0.0 test2/1.2.3 test3/4.5.6 " + base},
"append1": {app: []string{"test/1.0.0"}, expected: base + " test/1.0.0"},
"append3": {app: []string{"test/1.0.0", "test2/1.2.3", "test3/4.5.6"}, expected: base + " test/1.0.0 test2/1.2.3 test3/4.5.6"},
"both1": {app: []string{"test/1.0.0"}, pre: []string{"test2/1.2.3"}, expected: "test2/1.2.3 " + base + " test/1.0.0"},
"both3": {app: []string{"test/1.0.0", "test2/1.2.3", "test3/4.5.6"}, pre: []string{"test4/1.0.0", "test5/1.2.3", "test6/4.5.6"}, expected: "test4/1.0.0 test5/1.2.3 test6/4.5.6 " + base + " test/1.0.0 test2/1.2.3 test3/4.5.6"},
}
for name, test := range cases {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()
client := testClient(ctx, t, "https://example.com")
for _, s := range test.pre {
client.PrependToUserAgent(s)
}
for _, s := range test.app {
client.AppendToUserAgent(s)
}
ua := client.buildUA()
if ua != test.expected {
t.Errorf("Expected user agent to be %q, got %q", test.expected, ua)
}
})
}
}