-
Notifications
You must be signed in to change notification settings - Fork 3
/
filesystem_test.go
163 lines (158 loc) · 3.99 KB
/
filesystem_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
package gemini
import (
"context"
"crypto/sha256"
"encoding/hex"
"io"
"io/ioutil"
"net/url"
"testing"
)
var geminiSuccessHeader = Header{
Code: CodeSuccess,
Meta: DefaultMIMEType,
}
func TestFileSystemHandler(t *testing.T) {
var tests = []struct {
name string
url string
expectedHeader Header
expectedBody string
}{
{
name: "directories without a trailing slash are redirected",
url: "/a",
expectedHeader: Header{Code: CodeRedirectPermanent, Meta: "/a/"},
expectedBody: "",
},
{
name: "if a directory contains index.gmi, it is used",
url: "/a/",
expectedHeader: geminiSuccessHeader,
expectedBody: "# /tests/a/index.gmi\n",
},
{
name: "files can be accessed directly",
url: "/a/index.gmi",
expectedHeader: geminiSuccessHeader,
expectedBody: "# /tests/a/index.gmi\n",
},
{
name: "non-existent files return a 51 status code",
url: "/a/non-existent.gmi",
expectedHeader: Header{
Code: CodeNotFound,
Meta: "not found",
},
expectedBody: "",
},
{
name: "a slash prefix is added if missing",
url: "a/index.gmi",
expectedHeader: geminiSuccessHeader,
expectedBody: "# /tests/a/index.gmi\n",
},
{
name: "if a directory does not contain an index, a listing is returned",
url: "/b/",
expectedHeader: geminiSuccessHeader,
expectedBody: `# Index of /b/
=> ../
=> c/
=> not_index
`,
},
{
name: "directory traversal attacks are deflected",
url: "../a/index.gmi",
expectedHeader: Header{
Code: CodeBadRequest,
Meta: "bad request",
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
dir := Dir("tests")
h := FileSystemHandler(dir)
u, err := url.Parse(tt.url)
if err != nil {
t.Fatalf("failed to parse URL %q", tt.url)
}
r := &Request{
Context: context.Background(),
URL: u,
}
resp, err := Record(r, h)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.expectedHeader.Code != resp.Header.Code {
t.Errorf("expected header code %v, got %v", tt.expectedHeader.Code, resp.Header.Code)
}
if tt.expectedHeader.Meta != resp.Header.Meta {
t.Errorf("expected header meta %q, got %q", tt.expectedHeader.Meta, resp.Header.Meta)
}
bdy, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("unexpected error reading body: %v", err)
}
if tt.expectedBody != string(bdy) {
t.Errorf("expected\n%v\nactual\n%v", tt.expectedBody, string(bdy))
}
})
}
}
func TestFileSystemBinaryHandling(t *testing.T) {
var tests = []struct {
name string
url string
expectedHeader Header
expectedHash string
}{
{
name: "mp3 files are served as expected",
url: "cordova.mp3",
expectedHeader: Header{
Code: CodeSuccess,
Meta: "audio/mpeg",
},
expectedHash: "9f3bd09333627c218b3b0af371d9c7f4c6ac6a8b1c60b9e9aba0ca68c602e511",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
dir := Dir("tests")
h := FileSystemHandler(dir)
u, err := url.Parse(tt.url)
if err != nil {
t.Fatalf("failed to parse URL %q", tt.url)
}
r := &Request{
Context: context.Background(),
URL: u,
}
resp, err := Record(r, h)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.expectedHeader.Code != resp.Header.Code {
t.Errorf("expected header code %v, got %v", tt.expectedHeader.Code, resp.Header.Code)
}
if tt.expectedHeader.Meta != resp.Header.Meta {
t.Errorf("expected header meta %q, got %q", tt.expectedHeader.Meta, resp.Header.Meta)
}
hash := sha256.New()
_, err = io.Copy(hash, resp.Body)
if err != nil {
t.Fatalf("unexpected error reading body: %v", err)
}
actualHash := hex.EncodeToString(hash.Sum(nil))
if tt.expectedHash != actualHash {
t.Errorf("expected\n%v\nactual\n%v", tt.expectedHash, string(actualHash))
}
})
}
}