This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
multipart_http_signer.go
211 lines (164 loc) · 5.01 KB
/
multipart_http_signer.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
package tests
import (
"bytes"
"io"
"io/ioutil"
"math/rand"
"net/http"
"testing"
"time"
"github.com/google/uuid"
. "github.com/smartystreets/goconvey/convey"
"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/randbytes"
"github.com/beyondstorage/go-storage/v4/types"
)
func TestMultipartHTTPSigner(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
signer, ok := store.(types.MultipartHTTPSigner)
So(ok, ShouldBeTrue)
Convey("When CreateMultipart via QuerySignHTTPCreateMultipart", func() {
path := uuid.New().String()
req, err := signer.QuerySignHTTPCreateMultipart(path, time.Duration(time.Hour))
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
So(req, ShouldNotBeNil)
So(req.URL, ShouldNotBeNil)
})
client := http.Client{}
_, err = client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("List with ModePart should get the object without error", func() {
it, err := store.List(path, pairs.WithListMode(types.ListModePart))
So(err, ShouldBeNil)
o, err := it.Next()
So(err, ShouldBeNil)
So(o, ShouldNotBeNil)
So(o.Path, ShouldEqual, path)
})
defer func() {
it, err := store.List(path, pairs.WithListMode(types.ListModePart))
if err != nil {
t.Error(err)
}
o, err := it.Next()
if err != nil {
t.Error(err)
}
err = store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID()))
if err != nil {
t.Error(err)
}
}()
})
Convey("When WriteMultipart via QuerySignHTTPWriteMultipart", func() {
path := uuid.New().String()
o, err := store.(types.Multiparter).CreateMultipart(path)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID()))
if err != nil {
t.Error(err)
}
}()
size := rand.Int63n(4 * 1024 * 1024)
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}
req, err := signer.QuerySignHTTPWriteMultipart(o, size, 0, time.Duration(time.Hour))
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
So(req, ShouldNotBeNil)
So(req.URL, ShouldNotBeNil)
})
req.Body = ioutil.NopCloser(bytes.NewReader(content))
client := http.Client{}
resp, err := client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
})
Convey("The size should be match", func() {
So(resp.Request.ContentLength, ShouldEqual, size)
})
})
Convey("When ListMultiPart via QuerySignHTTPListMultiPart", func() {
mu, ok := store.(types.Multiparter)
So(ok, ShouldBeTrue)
path := uuid.New().String()
o, err := mu.CreateMultipart(path)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path, pairs.WithMultipartID(o.MustGetMultipartID()))
if err != nil {
t.Error(err)
}
}()
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
partNumber := rand.Intn(1000) // Choose a random part number from [0, 1000)
r := io.LimitReader(randbytes.NewRand(), size)
_, _, err = mu.WriteMultipart(o, r, size, partNumber)
if err != nil {
t.Error(err)
}
req, err := signer.QuerySignHTTPListMultipart(o, time.Duration(time.Hour))
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
So(req, ShouldNotBeNil)
So(req.URL, ShouldNotBeNil)
})
client := http.Client{}
_, err = client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When CompletePart via QuerySignHTTPCompletePart", func() {
mu, ok := store.(types.Multiparter)
So(ok, ShouldBeTrue)
path := uuid.New().String()
o, err := mu.CreateMultipart(path)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
// Set 0 to `partNumber` here as the part numbers must be continuous for `CompleteMultipartUpload` in `cos` which is different with other storages.
partNumber := 0
r := io.LimitReader(randbytes.NewRand(), size)
_, part, err := mu.WriteMultipart(o, r, size, partNumber)
if err != nil {
t.Error(err)
}
req, err := signer.QuerySignHTTPCompleteMultipart(o, []*types.Part{part}, time.Duration(time.Hour))
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
So(req, ShouldNotBeNil)
So(req.URL, ShouldNotBeNil)
})
client := http.Client{}
_, err = client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The object should be readable after complete", func() {
ro, err := store.Stat(path)
So(err, ShouldBeNil)
So(ro.Mode.IsRead(), ShouldBeTrue)
So(ro.Mode.IsPart(), ShouldBeFalse)
})
})
})
}