forked from lindenlab/caddy-s3-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browse_test.go
126 lines (119 loc) · 2.8 KB
/
browse_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
package caddys3proxy
import (
"net/http"
"net/url"
"reflect"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
)
func TestConstructListObjInput(t *testing.T) {
type testCase struct {
name string
key string
bucket string
queryString string
expected s3.ListObjectsV2Input
}
testCases := []testCase{
testCase{
name: "no query options",
bucket: "myBucket",
key: "/mypath/",
expected: s3.ListObjectsV2Input{
Bucket: aws.String("myBucket"),
Delimiter: aws.String("/"),
Prefix: aws.String("mypath/"),
},
},
testCase{
name: "max option",
bucket: "myBucket",
key: "/mypath/",
queryString: "?max=20",
expected: s3.ListObjectsV2Input{
Bucket: aws.String("myBucket"),
Delimiter: aws.String("/"),
Prefix: aws.String("mypath/"),
MaxKeys: aws.Int64(20),
},
},
testCase{
name: "max with next",
bucket: "myBucket",
key: "/mypath/",
queryString: "?max=20&next=FOO",
expected: s3.ListObjectsV2Input{
Bucket: aws.String("myBucket"),
Delimiter: aws.String("/"),
Prefix: aws.String("mypath/"),
MaxKeys: aws.Int64(20),
ContinuationToken: aws.String("FOO"),
},
},
}
for _, tc := range testCases {
r := http.Request{}
u, _ := url.Parse(tc.queryString)
r.URL = u
p := S3Proxy{
Bucket: tc.bucket,
}
result := p.ConstructListObjInput(&r, tc.key)
if !reflect.DeepEqual(tc.expected, result) {
t.Errorf("Expected obj %v, got %v.", tc.expected, result)
}
}
}
func TestMakePageObj(t *testing.T) {
p := S3Proxy{}
listOutput := s3.ListObjectsV2Output{
KeyCount: aws.Int64(20),
NextContinuationToken: aws.String("next_token"),
MaxKeys: aws.Int64(20),
CommonPrefixes: []*s3.CommonPrefix{
&s3.CommonPrefix{
Prefix: aws.String("/mydir"),
},
&s3.CommonPrefix{
Prefix: aws.String("/otherdir"),
},
},
Contents: []*s3.Object{
&s3.Object{
Key: aws.String("/path/to/myobj"),
Size: aws.Int64(1024),
LastModified: aws.Time(time.Date(1845, time.November, 10, 23, 0, 0, 0, time.UTC)),
},
},
}
result := p.MakePageObj(&listOutput)
expected := PageObj{
Count: 20,
MoreLink: "?max=20&next=next_token",
Items: []Item{
Item{
Url: "./mydir/",
IsDir: true,
Name: "mydir",
},
Item{
Url: "./otherdir/",
IsDir: true,
Name: "otherdir",
},
Item{
Url: "./myobj",
Key: "/path/to/myobj",
IsDir: false,
Name: "myobj",
Size: "1.0 kB",
LastModified: "a long while ago",
},
},
}
if !reflect.DeepEqual(expected, result) {
t.Errorf("Expected obj %v, got %v.", expected, result)
}
}