aboutsummaryrefslogtreecommitdiff
path: root/internal/bunny/api_test.go
blob: e74a3b9b7b9a69d9eed75d296590ae6dd1e9fc37 (plain) (blame)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package bunny

import (
	"context"
	"crypto/sha256"
	"encoding/hex"
	"errors"
	"net/http"
	"net/http/httptest"
	"strings"
	"sync/atomic"
	"testing"
)

func newTestAPI(srvURL string) *API {
	api := NewAPI(srvURL, "zone", "key")
	api.http = &http.Client{Transport: rewriteTransport{}}
	return api
}

// rewriteTransport downgrades https://host/... to http://host/... for tests.
type rewriteTransport struct{}

func (rewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	req.URL.Scheme = "http"
	return http.DefaultTransport.RoundTrip(req)
}

func TestUploadSuccess(t *testing.T) {
	var gotKey, gotChecksum, gotPath string
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gotKey = r.Header.Get("AccessKey")
		gotChecksum = r.Header.Get("Checksum")
		gotPath = r.URL.Path
		if r.Header.Get("Content-Type") != "" {
			t.Errorf("unexpected Content-Type %q", r.Header.Get("Content-Type"))
		}
		w.WriteHeader(201)
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	if err := api.Upload(context.Background(), "a/b.js", []byte("data")); err != nil {
		t.Fatalf("Upload: %v", err)
	}
	sum := sha256.Sum256([]byte("data"))
	wantChecksum := strings.ToUpper(hex.EncodeToString(sum[:]))
	if gotKey != "key" || gotChecksum != wantChecksum {
		t.Fatalf("headers: key=%q checksum=%q (want %q)", gotKey, gotChecksum, wantChecksum)
	}
	if gotPath != "/zone/a/b.js" {
		t.Fatalf("path = %q", gotPath)
	}
}

func TestUploadRetriesOn429WithRetryAfter(t *testing.T) {
	var n int32
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if atomic.AddInt32(&n, 1) <= 2 {
			w.Header().Set("Retry-After", "0")
			w.WriteHeader(429)
			return
		}
		w.WriteHeader(200)
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	if err := api.Upload(context.Background(), "x", []byte("d")); err != nil {
		t.Fatalf("Upload: %v", err)
	}
	if n != 3 {
		t.Fatalf("attempts = %d, want 3", n)
	}
}

func TestUploadFailsOn429NoRetryAfter(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(429)
	}))
	defer srv.Close()
	api := newTestAPI(srv.URL)
	if err := api.Upload(context.Background(), "x", []byte("d")); err == nil {
		t.Fatal("expected error")
	}
}

func TestUploadFailsOn5xx(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
	}))
	defer srv.Close()
	api := newTestAPI(srv.URL)
	if err := api.Upload(context.Background(), "x", []byte("d")); err == nil {
		t.Fatal("expected error")
	}
}

func TestUploadRetriesExhausted(t *testing.T) {
	var n int32
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		atomic.AddInt32(&n, 1)
		w.Header().Set("Retry-After", "0")
		w.WriteHeader(429)
	}))
	defer srv.Close()
	api := newTestAPI(srv.URL)
	if err := api.Upload(context.Background(), "x", []byte("d")); err == nil {
		t.Fatal("expected error")
	}
	if n != 4 { // initial + 3 retries
		t.Fatalf("attempts = %d, want 4", n)
	}
}

func TestUploadClassifiesStatus(t *testing.T) {
	cases := []struct {
		status int
		kind   errorKind
	}{
		{http.StatusUnauthorized, errorKindAuth},
		{http.StatusForbidden, errorKindAuth},
		{http.StatusBadRequest, errorKindInternal},
		{http.StatusNotFound, errorKindInternal},
		{http.StatusInternalServerError, errorKindServer},
		{http.StatusBadGateway, errorKindServer},
	}
	for _, tc := range cases {
		srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(tc.status)
		}))
		api := newTestAPI(srv.URL)
		err := api.Upload(context.Background(), "x", []byte("d"))
		srv.Close()

		var apiErr *apiError
		if !errors.As(err, &apiErr) {
			t.Fatalf("status %d: want *APIError, got %T (%v)", tc.status, err, err)
		}
		if apiErr.kind != tc.kind {
			t.Fatalf("status %d: kind = %d, want %d", tc.status, apiErr.kind, tc.kind)
		}
		if apiErr.status != tc.status {
			t.Fatalf("status %d: APIError.Status = %d", tc.status, apiErr.status)
		}
	}
}

func TestUploadSurfacesBunnyErrorMessage(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(`{"HttpCode":400,"Message":"checksum mismatch"}`))
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	err := api.Upload(context.Background(), "x", []byte("d"))

	var apiErr *apiError
	if !errors.As(err, &apiErr) {
		t.Fatalf("want *apiError, got %T (%v)", err, err)
	}
	if got := apiErr.ErrorVerbose(); !strings.Contains(got, "checksum mismatch") {
		t.Fatalf("verbose detail = %q, want it to contain Bunny's Message", got)
	}
}

func TestListParsesFullPaths(t *testing.T) {
	var gotPath string
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gotPath = r.URL.Path
		w.Write([]byte(`[
			{"Path":"/zone/assets/","ObjectName":"app.abc.js","Length":14,"IsDirectory":false,"LastChanged":"2023-06-29T12:00:00.000"},
			{"Path":"/zone/assets/","ObjectName":"sub","Length":0,"IsDirectory":true,"LastChanged":"2023-06-29T12:00:00"}
		]`))
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	objs, err := api.listDir(context.Background(), "assets")
	if err != nil {
		t.Fatalf("listDir: %v", err)
	}
	if gotPath != "/zone/assets/" {
		t.Fatalf("list path = %q, want trailing slash", gotPath)
	}
	if len(objs) != 2 {
		t.Fatalf("objs = %v", objs)
	}
	if objs[0].Path != "assets/app.abc.js" || objs[0].Size != 14 || objs[0].IsDirectory {
		t.Fatalf("file object = %+v", objs[0])
	}
	if objs[1].Path != "assets/sub" || !objs[1].IsDirectory {
		t.Fatalf("dir object = %+v", objs[1])
	}
}

func TestListRootTrailingSlash(t *testing.T) {
	var gotPath string
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gotPath = r.URL.Path
		w.Write([]byte(`[]`))
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	if _, err := api.listDir(context.Background(), ""); err != nil {
		t.Fatalf("listDir: %v", err)
	}
	if gotPath != "/zone/" {
		t.Fatalf("root list path = %q, want /zone/", gotPath)
	}
}

func TestWalkBuildsTreeAndDescends(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/zone/":
			w.Write([]byte(`[
				{"Path":"/zone/","ObjectName":"index.html","Length":6,"IsDirectory":false},
				{"Path":"/zone/","ObjectName":"assets","IsDirectory":true}
			]`))
		case "/zone/assets/":
			w.Write([]byte(`[{"Path":"/zone/assets/","ObjectName":"app.js","Length":3,"IsDirectory":false}]`))
		default:
			t.Errorf("unexpected list path %q", r.URL.Path)
		}
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	it := api.List(context.Background(), "")
	var got []Object
	for it.Next() {
		got = append(got, it.Object())
	}
	if err := it.Err(); err != nil {
		t.Fatalf("Walk: %v", err)
	}
	if len(got) != 3 {
		t.Fatalf("got %d entries: %+v", len(got), got)
	}
	var sawDir, sawNested bool
	for _, o := range got {
		if o.Path == "assets" && o.IsDirectory {
			sawDir = true
		}
		if o.Path == "assets/app.js" && !o.IsDirectory && o.Size == 3 {
			sawNested = true
		}
	}
	if !sawDir || !sawNested {
		t.Fatalf("expected descent into assets; got %+v", got)
	}
}

func TestWalk404IsEmpty(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(404)
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	it := api.List(context.Background(), "")
	for it.Next() {
		t.Fatalf("unexpected entry %+v", it.Object())
	}
	if err := it.Err(); err != nil {
		t.Fatalf("Walk: %v", err)
	}
}

func TestWalkFatalOnNon404(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	it := api.List(context.Background(), "")
	for it.Next() {
	}
	if it.Err() == nil {
		t.Fatal("expected error")
	}
}

func TestUploadRateLimitedIsTyped(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(429)
	}))
	defer srv.Close()
	api := newTestAPI(srv.URL)
	err := api.Upload(context.Background(), "x", []byte("d"))
	var apiErr *apiError
	if !errors.As(err, &apiErr) || apiErr.kind != errorKindRateLimited {
		t.Fatalf("want KindRateLimited APIError, got %v", err)
	}
}

func TestDeleteSuccess(t *testing.T) {
	var gotMethod, gotKey, gotPath string
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gotMethod = r.Method
		gotKey = r.Header.Get("AccessKey")
		gotPath = r.URL.Path
		w.WriteHeader(200)
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	if err := api.Delete(context.Background(), "a/b.js"); err != nil {
		t.Fatalf("Delete: %v", err)
	}
	if gotMethod != http.MethodDelete {
		t.Fatalf("method = %q", gotMethod)
	}
	if gotKey != "key" || gotPath != "/zone/a/b.js" {
		t.Fatalf("key=%q path=%q", gotKey, gotPath)
	}
}

func TestDeleteFailsOn5xx(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(500)
	}))
	defer srv.Close()

	api := newTestAPI(srv.URL)
	if err := api.Delete(context.Background(), "a/b.js"); err == nil {
		t.Fatal("expected error on 5xx")
	}
}

// TestAPIErrorUnwrapsCause ensures a network apiError preserves its cause in the
// error chain, so errors.Is can detect e.g. context.Canceled from an aborted
// in-flight upload. Without this, the worker pool cannot filter cancellation
// collateral out of a fail-fast failure report.
func TestAPIErrorUnwrapsCause(t *testing.T) {
	req, err := http.NewRequest(http.MethodPut, "https://example.test/zone/a.js", nil)
	if err != nil {
		t.Fatal(err)
	}
	// Simulate a transport error whose cause is context.Canceled.
	apiErr := newAPIError(errorKindNetwork, context.Canceled, req, 0)
	if !errors.Is(apiErr, context.Canceled) {
		t.Fatalf("apiError must unwrap to its cause; got chain that is not context.Canceled")
	}
}