forked from espebra/filebin2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_file_test.go
507 lines (490 loc) · 12.6 KB
/
http_file_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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package main
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"net/http"
//"net/http/httputil"
"net/url"
"os"
"path"
"strings"
"testing"
)
type TestCase struct {
Description string
Method string
Bin string
Filename string
UploadContent string
DownloadContent string
MD5 string
SHA256 string
StatusCode int
}
func (tc TestCase) String() string {
return fmt.Sprintf("Test case details:\n\nDescription: %s\nMethod: %s\nBin: %s\nFilename: %s\nUpload content: %s\nDownload content: %s\nMD5: %s\nSHA256: %s\nExpected status code: %d\n\n", tc.Description, tc.Method, tc.Bin, tc.Filename, tc.UploadContent, tc.DownloadContent, tc.MD5, tc.SHA256, tc.StatusCode)
}
func httpRequest(tc TestCase) (statuscode int, body string, err error) {
u, err := url.Parse("http://localhost:8080")
if err != nil {
log.Fatal(err)
}
if tc.Bin != "" {
u.Path = path.Join(u.Path, tc.Bin)
}
if tc.Filename != "" {
u.Path = path.Join(u.Path, tc.Filename)
}
var req *http.Request
if tc.UploadContent == "" {
req, err = http.NewRequest(tc.Method, u.String(), nil)
} else {
req, err = http.NewRequest(tc.Method, u.String(), strings.NewReader(tc.UploadContent))
if tc.SHA256 != "" {
fmt.Printf("Content-SHA256 request header: %s\n", tc.SHA256)
req.Header.Set("Content-SHA256", tc.SHA256)
}
if tc.MD5 != "" {
checksum := string(base64.StdEncoding.EncodeToString([]byte(tc.MD5)))
fmt.Printf("Content-MD5 request header: %s\n", checksum)
req.Header.Set("Content-MD5", checksum)
}
}
if err != nil {
return -1, "", err
}
for name, values := range req.Header {
for _, value := range values {
fmt.Printf("Request header: %s=%s\n", name, value)
}
}
req.Close = true
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return -2, "", err
}
//dump, err := httputil.DumpResponse(resp, true)
//if err != nil {
// log.Fatal(err)
//}
//fmt.Printf("Response headers: %q", dump)
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -3, "", err
}
body = string(content)
resp.Body.Close()
return resp.StatusCode, body, err
}
func runTests(tcs []TestCase, t *testing.T) {
for i, tc := range tcs {
statusCode, body, err := httpRequest(tc)
if err != nil {
t.Errorf("Test case %d: Did not expect http request to fail: %s\n", i, err.Error())
t.Errorf("%s\n", tc.String())
os.Exit(1)
}
if tc.StatusCode != statusCode {
t.Errorf("Test case %d\n", i)
t.Errorf(" Expected response code %d, got %d\n", tc.StatusCode, statusCode)
t.Errorf(" Response body: %s\n", body)
t.Errorf(" %s\n", tc.String())
os.Exit(1)
}
if tc.DownloadContent != "" {
if tc.DownloadContent != body {
t.Errorf("Test case %d: Expected body %s, got %s\n", i, tc.DownloadContent, body)
t.Errorf("%s\n", tc.String())
os.Exit(1)
}
}
}
}
func TestUploadFile(t *testing.T) {
tcs := []TestCase{
{
Description: "Upload ok to specify everything",
Method: "POST",
Bin: "mytestbin",
Filename: "a",
UploadContent: "content a",
SHA256: "0069ffe8481777aa403982d9e9b3fa48957015a07cfa0f66dae32050b95bda54",
MD5: "d8114b361885ee54897e52ce2308e274",
StatusCode: 201,
}, {
Description: "Ok to not specify MD5 and SHA256",
Method: "POST",
Bin: "mytestbin",
Filename: "b",
UploadContent: "content b",
StatusCode: 201,
}, {
Description: "Missing filename should fail",
Method: "POST",
Bin: "mytestbin",
Filename: "",
UploadContent: "some content",
StatusCode: 405,
}, {
Description: "No content should fail",
Method: "POST",
Bin: "mytestbin",
Filename: "c",
UploadContent: "",
StatusCode: 400,
}, {
Description: "Wrong MD5 checksum should fail",
Method: "POST",
Bin: "mytestbin",
Filename: "d",
UploadContent: "some more content",
MD5: "wrong checksum",
StatusCode: 400,
}, {
Description: "Wrong SHA256 checksum should fail",
Method: "POST",
Bin: "mytestbin",
Filename: "e",
UploadContent: "some more content",
SHA256: "wrong checksum",
StatusCode: 400,
}, {
Description: "Upload new file that will be updated later",
Method: "POST",
Bin: "mytestbin",
Filename: "f",
UploadContent: "first revision",
StatusCode: 201,
}, {
Description: "Update file that will be updated later",
Method: "POST",
Bin: "mytestbin",
Filename: "f",
UploadContent: "second revision",
StatusCode: 201,
}, {
Description: "Ok to specify everything on download",
Method: "GET",
Bin: "mytestbin",
Filename: "a",
DownloadContent: "content a",
StatusCode: 200,
}, {
Description: "Try to download non-existing file",
Method: "GET",
Bin: "mytestbin",
Filename: "unknown",
StatusCode: 404,
}, {
Description: "Try to view non-existing bin",
Method: "GET",
Bin: "unknown",
Filename: "unknown",
StatusCode: 404,
},
}
runTests(tcs, t)
}
func TestUploadToDeletedAtBin(t *testing.T) {
tcs := []TestCase{
{
Description: "Create file to set up test case",
Method: "POST",
Bin: "mytestbin2",
Filename: "a",
UploadContent: "content a",
StatusCode: 201,
}, {
Description: "Delete bin",
Method: "DELETE",
Bin: "mytestbin2",
StatusCode: 200,
}, {
Description: "Create the file again, it should fail",
Method: "POST",
Bin: "mytestbin2",
Filename: "a",
UploadContent: "content a",
StatusCode: 405,
},
}
runTests(tcs, t)
}
func TestDeleteFile(t *testing.T) {
tcs := []TestCase{
{
Description: "Create file to set up test case",
Method: "POST",
Bin: "mytestbin",
Filename: "a",
UploadContent: "content a",
StatusCode: 201,
}, {
Description: "Delete file before trying to fetch it",
Method: "DELETE",
Bin: "mytestbin",
Filename: "a",
StatusCode: 200,
}, {
Description: "Get file after it was deleted, should fail",
Method: "GET",
Bin: "mytestbin",
Filename: "a",
StatusCode: 404,
},
}
runTests(tcs, t)
}
func TestLockAndDeleteBin(t *testing.T) {
tcs := []TestCase{
{
Description: "Fetch bin that does not exist",
Method: "GET",
Bin: "mytestbin",
StatusCode: 200,
}, {
Description: "Create file to set up test case",
Method: "POST",
Bin: "mytestbin",
Filename: "a",
UploadContent: "content a",
StatusCode: 201,
}, {
Description: "Lock the bin",
Method: "PUT",
Bin: "mytestbin",
StatusCode: 200,
}, {
Description: "Lock the bin again",
Method: "PUT",
Bin: "mytestbin",
StatusCode: 200,
}, {
Description: "Try to update existing file, should be rejected",
Method: "POST",
Bin: "mytestbin",
Filename: "a",
UploadContent: "content a",
StatusCode: 405,
}, {
Description: "Try to create a new file, should be rejected",
Method: "POST",
Bin: "mytestbin",
Filename: "b",
UploadContent: "content b",
StatusCode: 405,
}, {
Description: "Delete bin, should be accepted",
Method: "DELETE",
Bin: "mytestbin",
StatusCode: 200,
}, {
Description: "Delete bin again, should not be found",
Method: "DELETE",
Bin: "mytestbin",
StatusCode: 404,
}, {
Description: "Get the file from the bin that was deleted, should fail",
Method: "GET",
Bin: "mytestbin",
Filename: "a",
StatusCode: 404,
}, {
Description: "Delete the bin that was deleted, should be not found",
Method: "DELETE",
Bin: "mytestbin",
StatusCode: 404,
}, {
Description: "Lock the bin that was deleted, should fail",
Method: "PUT",
Bin: "mytestbin",
StatusCode: 404,
},
}
runTests(tcs, t)
}
func TestNotExistingBinsAndFiles(t *testing.T) {
tcs := []TestCase{
{
Description: "Get bin that doesn't exist",
Method: "GET",
Bin: "unknownbin",
StatusCode: 200,
}, {
Description: "Lock bin that doesn't exist",
Method: "PUT",
Bin: "unknownbin",
StatusCode: 404,
}, {
Description: "Delete bin that doesn't exist",
Method: "DELETE",
Bin: "unknownbin",
StatusCode: 404,
}, {
Description: "Delete file that doesn't exist in bin that doesn't exist",
Method: "DELETE",
Bin: "unknownbin2",
Filename: "unknownfile",
StatusCode: 404,
}, {
Description: "Create new bin",
Method: "POST",
Bin: "mytestbin3",
Filename: "a",
UploadContent: "content a",
StatusCode: 201,
}, {
Description: "Get bin",
Method: "GET",
Bin: "mytestbin3",
StatusCode: 200,
}, {
Description: "Get file",
Method: "GET",
Bin: "mytestbin3",
Filename: "a",
DownloadContent: "content a",
StatusCode: 200,
}, {
Description: "Delete file that doesn't exist in bin that exists",
Method: "DELETE",
Bin: "mytestbin3",
Filename: "unknownfile",
StatusCode: 404,
}, {
Description: "Delete file that exists in bin that exists",
Method: "DELETE",
Bin: "mytestbin3",
Filename: "a",
StatusCode: 200,
}, {
Description: "Delete file again that no longer exists in bin that exists",
Method: "DELETE",
Bin: "mytestbin3",
Filename: "a",
StatusCode: 404,
}, {
Description: "Get file that was deleted",
Method: "GET",
Bin: "mytestbin3",
Filename: "a",
StatusCode: 404,
}, {
Description: "Create file again",
Method: "POST",
Bin: "mytestbin3",
Filename: "a",
UploadContent: "content a",
StatusCode: 201,
}, {
Description: "Delete bin",
Method: "DELETE",
Bin: "mytestbin3",
StatusCode: 200,
}, {
Description: "Get the bin that was deleted",
Method: "GET",
Bin: "mytestbin3",
StatusCode: 404,
}, {
Description: "Get file from bin that was deleted",
Method: "GET",
Bin: "mytestbin3",
Filename: "a",
StatusCode: 404,
}, {
Description: "Delete file from the bin that is deleted",
Method: "DELETE",
Bin: "mytestbin3",
Filename: "a",
StatusCode: 404,
}, {
Description: "Lock bin that is deleted",
Method: "PUT",
Bin: "mytestbin3",
StatusCode: 404,
},
}
runTests(tcs, t)
}
func TestLimitFileDownloads(t *testing.T) {
tcs := []TestCase{
{
Description: "Create new bin",
Method: "POST",
Bin: "mytestbin4",
Filename: "a",
UploadContent: "content a",
StatusCode: 201,
}, {
Description: "Get file first time",
Method: "GET",
Bin: "mytestbin4",
Filename: "a",
DownloadContent: "content a",
StatusCode: 200,
}, {
Description: "Get file second time",
Method: "GET",
Bin: "mytestbin4",
Filename: "a",
DownloadContent: "content a",
StatusCode: 200,
}, {
Description: "Get file third time, above the download limit of 2",
Method: "GET",
Bin: "mytestbin4",
Filename: "a",
StatusCode: 403,
},
}
runTests(tcs, t)
}
func TestBinInputValidation(t *testing.T) {
tcs := []TestCase{
{
Description: "Too long bin",
Method: "POST",
Bin: "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
Filename: "a",
UploadContent: "content a",
StatusCode: 400,
},
{
Description: "Too short bin",
Method: "POST",
Bin: "yyyy",
Filename: "a",
UploadContent: "content a",
StatusCode: 400,
},
{
Description: "Bin with invalid characters",
Method: "POST",
Bin: "asdf$%&=^*",
Filename: "a",
UploadContent: "content a",
StatusCode: 404,
},
{
Description: "Bin with reserved name (/admin/)",
Method: "POST",
Bin: "admin",
Filename: "a",
UploadContent: "content a",
StatusCode: 400,
},
{
Description: "Bin with reserved name (/archive/)",
Method: "POST",
Bin: "archive",
Filename: "a",
UploadContent: "content a",
StatusCode: 400,
},
}
runTests(tcs, t)
}