-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.bats
106 lines (85 loc) · 2.05 KB
/
test.bats
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
# utilities
debug () {
sed 's/^/# /' >&3
}
debug_cmd () {
stderr "$@" | debug
}
stderr () {
"$@" 2>&1 >/dev/null
}
curl () {
URL=$1; shift
command curl -m 1 -s "$@" "localhost:$PORT/$URL"
}
curl_status () {
URL=$1; shift
curl "$URL" -o /dev/null -w "%{http_code}" "$@"
}
content_size () {
grep "Content-Length: " <<< "$@" |
sed 's/Content-Length: //; s/\s//g' | tr -d '\r'
}
# tests
@test "serves files" {
for file in index.html hey how are you; do
echo hi > $file
[ "$(curl $file)" = "hi" ]
done
}
@test "resolves directories" {
echo hi > index.html
[ "$(curl /)" = "hi" ]
}
@test "prevents path traversal" {
[ "$(curl_status ../../../../../../../../../etc/passwd)" -eq 404 ]
}
@test "returns 404 if not found" {
rm -f not_found
[ "$(curl_status not_found "$@")" -eq 404 ]
}
@test "returns 403 if permission denied" {
touch not_allowed
chmod 000 not_allowed
[ "$(curl_status not_allowed "$@")" -eq 403 ]
}
@test "supports HEAD" {
echo hi > index.html
output="$(curl / -I)"
[ "$(echo "$output" | head -n 1)" = "$(echo -e 'HTTP/1.1 200 OK\r')" ]
[ $(content_size "$output") -eq 3 ]
}
@test "Content-Length is accurate" {
echo hi > blah
[ "$(content_size "$(curl /blah -i)")" = 3 ]
> empty
[ "$(content_size "$(curl /empty -i)")" = 0 ]
}
@test "HEAD returns 403 if permission denied" {
test_returns_403_if_permission_denied -I
}
@test "HEAD returns 404 if not found" {
test_returns_404_if_not_found -I
}
@test "Content-Length for HEAD is accurate" {
> blah
run curl /blah -I
[ "$(content_size "$output")" = 0 ]
echo hi > hey
[ "$(content_size "$(curl /hey -I)")" = 3 ]
}
@test "Resolves subdirectories" {
mkdir -p subdir
echo hi > subdir/blah
[ $(content_size "$(curl /subdir/blah -I)") -eq 3 ] | debug
}
@test "Closes connection for HTTP/1.0" {
ab -s 1 localhost:$PORT/
}
@test "Maintains connection for HTTP/1.1+" {
curl / localhost:$PORT -v 2>&1 | grep "Re-using existing connection"
}
@test "Gives 404 for file with trailing slash" {
touch blah
[ "$(curl_status blah/)" -eq 404 ]
}