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
/
storage_http_signer.go
201 lines (159 loc) · 4.79 KB
/
storage_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
package tests
import (
"bytes"
"crypto/sha256"
"errors"
"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/services"
"github.com/beyondstorage/go-storage/v4/types"
)
func TestStorageHTTPSignerRead(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
signer, ok := store.(types.StorageHTTPSigner)
So(ok, ShouldBeTrue)
Convey("When Read via QuerySignHTTPRead", func() {
size := rand.Int63n(4 * 1024 * 1024)
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}
path := uuid.New().String()
_, err = store.Write(path, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()
req, err := signer.QuerySignHTTPRead(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{}
resp, err := client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
})
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
Convey("The content should be match", func() {
So(err, ShouldBeNil)
So(buf, ShouldNotBeNil)
So(resp.ContentLength, ShouldEqual, size)
So(sha256.Sum256(buf), ShouldResemble, sha256.Sum256(content))
})
})
})
}
func TestStorageHTTPSignerWrite(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
signer, ok := store.(types.StorageHTTPSigner)
So(ok, ShouldBeTrue)
Convey("When Write via QuerySignHTTPWrite", func() {
size := rand.Int63n(4 * 1024 * 1024)
content, err := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
if err != nil {
t.Error(err)
}
path := uuid.New().String()
req, err := signer.QuerySignHTTPWrite(path, size, 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{}
_, err = client.Do(req)
Convey("The request returned error should be nil", func() {
So(err, ShouldBeNil)
})
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()
Convey("Read should get object data without error", func() {
var buf bytes.Buffer
n, err := store.Read(path, &buf)
Convey("The content should be match", func() {
So(err, ShouldBeNil)
So(buf, ShouldNotBeNil)
So(n, ShouldEqual, size)
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(content))
})
})
})
})
}
func TestStorageHTTPSignerDelete(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
signer, ok := store.(types.StorageHTTPSigner)
So(ok, ShouldBeTrue)
Convey("When Delete via QuerySignHTTPDelete", func() {
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), size)
path := uuid.New().String()
_, err := store.Write(path, r, size)
if err != nil {
t.Error(err)
}
req, err := signer.QuerySignHTTPDelete(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("Stat should get nil Object and ObjectNotFound error", func() {
o, err := store.Stat(path)
So(errors.Is(err, services.ErrObjectNotExist), ShouldBeTrue)
So(o, ShouldBeNil)
})
})
Convey("When Delete with multipart id via QuerySignHTTPDelete", func() {
mu, ok := store.(types.Multiparter)
So(ok, ShouldBeTrue)
path := uuid.New().String()
o, err := mu.CreateMultipart(path)
if err != nil {
t.Error(err)
}
req, err := signer.QuerySignHTTPDelete(path, time.Duration(time.Hour), pairs.WithMultipartID(o.MustGetMultipartID()))
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 first request returned error should be nil", func() {
So(err, ShouldBeNil)
})
_, err = client.Do(req)
Convey("The second request returned error should be nil", func() {
So(err, ShouldBeNil)
})
})
})
}