-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfileserver_test.go
162 lines (146 loc) · 3.96 KB
/
fileserver_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
package gzipped
import (
"bytes"
"compress/gzip"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/textproto"
"strconv"
"testing"
"github.com/kevinpollet/nego"
)
// Test that the server respects client preferences
func TestPreference(t *testing.T) {
req := http.Request{Header: http.Header{}}
// the client doesn't set any preferences, so we should pick br
for _, info := range []struct {
hdr string // the Accept-Encoding string
expect string // the expected encoding chosen by the server
}{
{"*", "br"},
{"gzip, deflate, br", "br"},
{"gzip, deflate, br;q=0.5", "gzip"},
} {
req.Header.Set("Accept-Encoding", info.hdr)
negenc := nego.NegotiateContentEncoding(&req, preferredEncodings...)
if negenc != info.expect {
t.Errorf("server chose %s but we expected %s for header %s", negenc, info.expect, info.hdr)
}
}
}
func testGet(t *testing.T, acceptGzip bool, urlPath string, expectedBody string) {
fs := FileServer(Dir("./testdata/"))
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", urlPath, nil)
if acceptGzip {
req.Header.Set("Accept-Encoding", "gzip,*")
}
fs.ServeHTTP(rr, req)
h := rr.Header()
// Check the content-length is correct.
clh := h["Content-Length"]
if len(clh) > 0 {
byts, err := strconv.Atoi(clh[0])
if err != nil {
t.Errorf("Invalid Content-Length on response: '%s'", clh[0])
}
n := rr.Body.Len()
if n != byts {
t.Errorf("GET expected %d byts, got %d", byts, n)
}
}
// Check the body content is correct.
ce := h["Content-Encoding"]
var body string
if len(ce) > 0 {
if ce[0] == "gzip" {
rdr, err := gzip.NewReader(bytes.NewReader(rr.Body.Bytes()))
if err != nil {
t.Errorf("Gunzip failed: %s", err)
} else {
bbody, err := ioutil.ReadAll(rdr)
if err != nil {
t.Errorf("Gunzip read failed: %s", err)
} else {
body = string(bbody)
}
}
} else {
t.Errorf("Invalid Content-Encoding in response: '%s'", ce[0])
}
} else {
body = rr.Body.String()
}
if len(body) != len(expectedBody) {
t.Errorf("GET (acceptGzip=%v) returned wrong decoded body length %d, expected %d",
acceptGzip, len(body), len(expectedBody))
}
if body != expectedBody {
t.Errorf("GET (acceptGzip=%v) returned wrong body '%s'", acceptGzip, body)
}
}
func TestOpenStat(t *testing.T) {
fh := &fileHandler{Dir(".")}
_, _, err := fh.openAndStat(".")
if err == nil {
t.Errorf("openAndStat directory succeeded, should have failed")
}
_, _, err = fh.openAndStat("updog")
if err == nil {
t.Errorf("openAndStat nonexistent file succeeded, should have failed")
}
}
func TestNoBrowse(t *testing.T) {
fs := FileServer(Dir("./testdata/"))
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
fs.ServeHTTP(rr, req)
if rr.Code != 404 {
t.Errorf("Directory browse succeeded")
}
}
func TestLeadingSlash(t *testing.T) {
fs := FileServer(Dir("./testdata/"))
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "file.txt", nil)
fs.ServeHTTP(rr, req)
if rr.Code != 200 {
t.Errorf("Missing leading / on HTTP path caused error")
}
}
func Test404(t *testing.T) {
fs := FileServer(Dir("./testdata/"))
rr := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/nonexistent.txt", nil)
fs.ServeHTTP(rr, req)
if rr.Code != 404 {
t.Errorf("Directory browse succeeded")
}
}
func TestGet(t *testing.T) {
testGet(t, false, "/file.txt", "zyxwvutsrqponmlkjihgfedcba\n")
}
func TestGzipGet(t *testing.T) {
testGet(t, true, "/file.txt", "abcdefghijklmnopqrstuvwxyz\n")
}
func TestGetIdentity(t *testing.T) {
testGet(t, false, "/file2.txt", "1234567890987654321\n")
}
func TestGzipGetIdentity(t *testing.T) {
testGet(t, true, "/file2.txt", "1234567890987654321\n")
}
func TestConstHeaders(t *testing.T) {
for _, header := range []string{
acceptEncodingHeader,
contentEncodingHeader,
contentLengthHeader,
rangeHeader,
varyHeader,
} {
canonical := textproto.CanonicalMIMEHeaderKey(header)
if header != canonical {
t.Errorf("%s != %s", header, canonical)
}
}
}