forked from ThomasLeister/prosody-filer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prosody-filer_test.go
237 lines (188 loc) · 5.46 KB
/
prosody-filer_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
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
package main
/*
* Manual testing with CURL
* Send with:
* curl -X PUT "http://localhost:5050/upload/thomas/abc/catmetal.jpg?v=e17531b1e88bc9a5cbf816eca8a82fc09969c9245250f3e1b2e473bb564e4be0" --data-binary '@catmetal.jpg'
* HMAC: e17531b1e88bc9a5cbf816eca8a82fc09969c9245250f3e1b2e473bb564e4be0
*/
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
minio "github.com/minio/minio-go"
)
func mockUpload() {
_, err := s3Client.FPutObject(context.Background(), conf.S3Bucket, "/thomas/abc/catmetal.jpg", "./catmetal.jpg", minio.PutObjectOptions{})
if err != nil {
log.Fatal(err)
}
}
func cleanup() {
err := s3Client.RemoveObject(context.Background(), conf.S3Bucket, "/thomas/abc/catmetal.jpg", minio.RemoveObjectOptions{})
if err != nil {
log.Fatal(err)
}
}
func TestReadConfig(t *testing.T) {
// Set config
err := readConfig("config.toml", &conf)
if err != nil {
t.Fatal(err)
}
}
func TestUploadValid(t *testing.T) {
// Set config
readConfig("config.toml", &conf)
s3Login()
// Read catmetal file
catmetalfile, err := ioutil.ReadFile("catmetal.jpg")
if err != nil {
t.Fatal(err)
}
// Create request
req, err := http.NewRequest("PUT", "/upload/thomas/abc/catmetal.jpg", bytes.NewBuffer(catmetalfile))
q := req.URL.Query()
q.Add("v", "1924ba5c934977747c91039b772b460664e5cee4104ae85c31449114ad194cfa")
req.URL.RawQuery = q.Encode()
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleRequest)
// Send request and record response
handler.ServeHTTP(rr, req)
// Check status code
if status := rr.Code; status != http.StatusCreated {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, http.StatusOK, rr.Body.String())
}
// clean up
cleanup()
}
func TestUploadMissingMAC(t *testing.T) {
// Set config
readConfig("config.toml", &conf)
s3Login()
// Read catmetal file
catmetalfile, err := ioutil.ReadFile("catmetal.jpg")
if err != nil {
t.Fatal(err)
}
// Create request
req, err := http.NewRequest("PUT", "/upload/thomas/abc/catmetal.jpg", bytes.NewBuffer(catmetalfile))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleRequest)
// Send request and record response
handler.ServeHTTP(rr, req)
// Check status code
if status := rr.Code; status != http.StatusForbidden {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, http.StatusForbidden, rr.Body.String())
}
}
func TestUploadInvalidMAC(t *testing.T) {
// Set config
readConfig("config.toml", &conf)
s3Login()
// Read catmetal file
catmetalfile, err := ioutil.ReadFile("catmetal.jpg")
if err != nil {
t.Fatal(err)
}
// Create request
req, err := http.NewRequest("PUT", "/upload/thomas/abc/catmetal.jpg", bytes.NewBuffer(catmetalfile))
q := req.URL.Query()
q.Add("v", "abc")
req.URL.RawQuery = q.Encode()
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleRequest)
// Send request and record response
handler.ServeHTTP(rr, req)
// Check status code
if status := rr.Code; status != http.StatusForbidden {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, http.StatusForbidden, rr.Body.String())
}
}
func TestUploadInvalidMethod(t *testing.T) {
// Set config
readConfig("config.toml", &conf)
s3Login()
// Read catmetal file
catmetalfile, err := ioutil.ReadFile("catmetal.jpg")
if err != nil {
t.Fatal(err)
}
// Create request
req, err := http.NewRequest("POST", "/upload/thomas/abc/catmetal.jpg", bytes.NewBuffer(catmetalfile))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleRequest)
// Send request and record response
handler.ServeHTTP(rr, req)
// Check status code
if status := rr.Code; status != http.StatusMethodNotAllowed {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, http.StatusMethodNotAllowed, rr.Body.String())
}
}
func TestDownloadOK(t *testing.T) {
// Set config
readConfig("config.toml", &conf)
s3Login()
// Mock upload
mockUpload()
for _, method := range []string{"GET", "HEAD"} {
for _, proxy := range []bool{false, true} {
conf.ProxyMode = proxy
t.Run(fmt.Sprintf("method %s proxy %t", method, proxy), func(t *testing.T) {
// Create request
req, err := http.NewRequest("HEAD", "/upload/thomas/abc/catmetal.jpg", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleRequest)
// Send request and record response
handler.ServeHTTP(rr, req)
// Check status code
var wanted = http.StatusFound
if proxy {
wanted = http.StatusOK
}
if status := rr.Code; status != wanted {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, wanted, rr.Body.String())
}
})
}
}
// cleanup
cleanup()
}
func TestEmptyGet(t *testing.T) {
// Set config
readConfig("config.toml", &conf)
s3Login()
// Create request
req, err := http.NewRequest("GET", "", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(handleRequest)
// Send request and record response
handler.ServeHTTP(rr, req)
// Check status code
if status := rr.Code; status != http.StatusBadGateway {
t.Errorf("handler returned wrong status code: got %v want %v. HTTP body: %s", status, http.StatusBadGateway, rr.Body.String())
}
}