-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockbox_test.go
More file actions
145 lines (130 loc) · 4.04 KB
/
lockbox_test.go
File metadata and controls
145 lines (130 loc) · 4.04 KB
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
package lockbox
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/nsf/jsondiff"
"yall.in"
testinglog "yall.in/testing"
)
type errorTest struct {
status int
body []byte
err error
}
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, _ *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(ctx context.Context, t *testing.T, r *http.Request, expected string) {
t.Helper()
req, err := cloneRequest(ctx, r)
if err != nil {
t.Fatalf("Error cloning request: %s", err)
}
body, err := io.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 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 cloneRequest(ctx context.Context, r *http.Request) (*http.Request, error) {
req := r.Clone((ctx))
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, fmt.Errorf("Error reading request body: %w", err)
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
req.Body = io.NopCloser(bytes.NewBuffer(body))
return req, nil
}
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 {
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)
}
})
}
}