-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_test.go
192 lines (162 loc) · 4.43 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
package bunnystorage_test
import (
"crypto/rand"
"net/url"
"os"
"path"
"reflect"
"testing"
"github.com/google/uuid"
bunnystorage "github.com/l0wl3vel/bunny-storage-go-sdk"
)
var password string
var endpoint url.URL
var bunnyclient bunnystorage.Client
var testingDirectory string
func TestMain(m *testing.M) {
password = os.Getenv("BUNNY_PASSWORD")
endpointString := os.Getenv("BUNNY_ENDPOINT")
if password == "" {
panic("No API Key provided (BUNNY_PASSWORD)")
}
if endpointString == "" {
panic("No Endpoint provided (BUNNY_ENDPOINT)")
}
endpoint, err := endpoint.Parse(endpointString)
if err != nil {
panic(err)
}
testingDirectory = uuid.New().String()
bunnyclient = bunnystorage.NewClient(*endpoint, password)
m.Run()
}
func TestRangeDownload(t *testing.T) {
t.Cleanup(func() { DeleteTestPath(t) })
content, filepath := UploadRandomFile1MB(t)
contentRange, err := bunnyclient.DownloadPartial(filepath, 20, 1023)
if err != nil {
t.Error(err)
}
// Go uses [inclusive:exclusive] slice indexing and HTTP Ranges use [inclusive:inclusive]
expected_range := content[20:1024]
if !reflect.DeepEqual(contentRange, expected_range) {
t.Errorf("Ranged download did not return the expected range len_input:%v len_test:%v", len(expected_range), len(contentRange))
}
}
func TestDescribeFile(t *testing.T) {
t.Cleanup(func() { DeleteTestPath(t) })
_, name := UploadRandomFile1MB(t)
obj, err := bunnyclient.Describe(name)
if err != nil {
t.Error(err)
}
empty_object := bunnystorage.Object{}
if obj == empty_object {
t.Errorf("Returned empty DESCRIBE API response")
}
}
func DeleteFileWithPathSetToTrue(t *testing.T) {
t.Cleanup(func() { DeleteTestPath(t) })
_, testpath := UploadRandomFile1MB(t)
err := bunnyclient.Delete(testpath, true)
if err != nil {
t.Error(err)
}
list := ListFilesInTestDir(t)
if len(list) != 0 {
t.Errorf("Returned List not as long as expected: Got: %v Expected %v", len(list), 1)
}
}
func TestGetNonexistentFile(t *testing.T) {
body, err := bunnyclient.Download("thispathdoesnotexist")
if err == nil {
t.Error("Error should not be nil when getting a non-existent file")
}
if body != nil {
t.Error("Returned buffer should be nil when getting a non-existent file")
}
}
func TestDeleteNonexistentFile(t *testing.T) {
err := bunnyclient.Delete("thispathdoesnotexist", false)
if err == nil {
t.Error("Expected \"Not Found\" Error")
}
}
func TestDownloadAfterUpload1M(t *testing.T) {
t.Cleanup(func() { DeleteTestPath(t) })
input, _ := UploadRandomFile1MB(t) // 1MB file size
output := DownloadFile(t)
list := ListFilesInTestDir(t)
if len(list) != 1 {
t.Errorf("Returned List not as long as expected: Got: %v Expected %v", len(list), 1)
}
if !reflect.DeepEqual(input, output) {
t.Error("Downloaded Content does not match uploaded content")
}
}
func TestListAfterUploadWithExtraTrailingSlash(t *testing.T) {
t.Cleanup(func() { DeleteTestPath(t) })
_, _ = UploadRandomFile1MB(t) // 1MB file size
list, err := bunnyclient.List(testingDirectory + "/")
if err != nil {
t.Error(err)
}
if len(list) != 1 {
t.Errorf("Returned List not as long as expected: Got: %v Expected %v", len(list), 1)
}
}
func TestListOnMissingDirectory(t *testing.T) {
list, err := bunnyclient.List(testingDirectory + t.Name())
if err != nil {
t.Error(err)
}
if len(list) != 0 {
t.Errorf("Returned List not as long as expected: Got: %v Expected %v", len(list), 1)
}
}
func ListFilesInTestDir(t *testing.T) []bunnystorage.Object {
items, err := bunnyclient.List(testingDirectory)
if err != nil {
t.Error(err)
}
return items
}
func DownloadFile(t *testing.T) []byte {
t.Helper()
testpath := path.Join(testingDirectory, t.Name())
body, err := bunnyclient.Download(testpath)
if err != nil {
t.Error(err)
}
return body
}
func UploadRandomFile1MB(t *testing.T) ([]byte, string) {
t.Helper()
testpath := path.Join(testingDirectory, t.Name())
testcontent := make([]byte, 1048576)
_, err := rand.Read(testcontent)
if err != nil {
t.Error(err)
}
err = bunnyclient.Upload(testpath, testcontent, true)
if err != nil {
t.Error(err)
}
return testcontent, testpath
}
func DeleteFile(t *testing.T) {
t.Helper()
testpath := path.Join(testingDirectory, t.Name())
err := bunnyclient.Delete(testpath, false)
if err != nil {
t.Error(err)
}
}
func DeleteTestPath(t *testing.T) {
t.Helper()
testpath := path.Join(testingDirectory)
err := bunnyclient.Delete(testpath, true)
if err != nil {
t.Error(err)
}
}