-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
142 lines (122 loc) · 2.98 KB
/
main_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
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestPrepareDirectory(t *testing.T) {
dir := "docs"
dir = prepareDirectory(dir)
if dir == "docs" {
t.Log("Wanted absolute pathfrom 'docs', still got 'docs'")
t.FailNow()
}
// give absolute dir
dir2 := prepareDirectory(dir)
if dir != dir2 {
t.Logf("Wanted no change if given absolute path, got: %q != %q", dir, dir2)
t.FailNow()
}
}
func TestFileIsGood(t *testing.T) {
dir := prepareDirectory("docs")
req := dir + "index.md" // index.md exists
if !fileisgood(req) {
// docs is a normal directory, not a symlink. should never be false
t.Logf("%s should be good, got bad", req)
t.FailNow()
}
}
func TestRefuseSymlinks(t *testing.T) {
dir := prepareDirectory("docs")
os.Remove(dir + "index.link")
err := os.Symlink(dir+"index.md", dir+"index.link")
defer os.Remove(dir + "index.link")
if err != nil {
t.Log("Error creating symlink:", err)
t.FailNow()
}
req := dir + "index.link"
if fileisgood(req) {
// index.link is a symlink. should never be true
t.Logf("%s should be good, got bad", req)
t.FailNow()
}
}
func TestRefuseDotDots(t *testing.T) {
dir := prepareDirectory("docs")
req, _ := http.NewRequest("GET", "/../main.go", nil)
w := httptest.NewRecorder()
h := &Handler{
Root: http.Dir(dir),
RootString: dir,
}
h.ServeHTTP(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusNotFound {
t.Log("Expected 404, got:", resp.StatusCode)
t.FailNow()
}
if len(body) != 19 {
t.Log("Expected 404 body, got:", len(body), string(body))
t.FailNow()
}
if string(body) != "404 page not found\n" {
t.Logf("Expected %q, got: %q", "404 page not found\n", string(body))
t.FailNow()
}
}
func TestHTMLHeaderFooter(t *testing.T) {
dir := prepareDirectory("docs")
req, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h := &Handler{
Root: http.Dir(dir),
RootString: dir,
header: []byte("001"),
footer: []byte("002"),
}
h.ServeHTTP(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
t.Log("Expected 200, got:", resp.StatusCode)
t.FailNow()
}
bodystr := string(body)
if !strings.HasPrefix(bodystr, "001") {
t.Log("Expected prefix of '001'")
t.Fail()
}
if !strings.HasSuffix(bodystr, "002") {
t.Log("Expected suffix of '002'")
t.Fail()
}
}
func sendRequest(req *http.Request) *http.Response {
dir := prepareDirectory("docs")
w := httptest.NewRecorder()
h := &Handler{
Root: http.Dir(dir),
RootString: dir,
}
h.ServeHTTP(w, req)
resp := w.Result()
return resp
}
func TestBadMethods(t *testing.T) {
methods := []string{"POST", "PUT", "DELETE", "HEAD",
"OPTIONS", "TRACE", "CONNECT", "DUMMY"}
for _, method := range methods {
req, _ := http.NewRequest(method, "/", nil)
resp := sendRequest(req)
if resp.StatusCode != 404 {
t.Log("Expected 404, got:", resp.StatusCode)
t.FailNow()
}
}
}