-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_directory_listing.sh
More file actions
executable file
·125 lines (102 loc) · 3.24 KB
/
test_directory_listing.sh
File metadata and controls
executable file
·125 lines (102 loc) · 3.24 KB
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
#!/bin/bash
# Test script for directory listing feature
PORT=9994
SERVER_PID=""
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo "=========================================="
echo " Directory Listing Test Suite"
echo "=========================================="
echo ""
# Create test structure
echo -e "${BLUE}Setting up test directories...${NC}"
mkdir -p public/testdir public/testdir/subdir public/testdir/emptydir
echo "Test file 1" > public/testdir/file1.txt
echo "Test file 2" > public/testdir/file2.txt
echo "<h1>Subdir Index</h1>" > public/testdir/subdir/index.html
echo "Large file content" > public/testdir/largefile.txt
for i in {1..100}; do echo "Line $i with some content" >> public/testdir/largefile.txt; done
echo -e "${GREEN}✓ Test structure created${NC}\n"
# Start server
echo -e "${BLUE}Starting server on port $PORT...${NC}"
./server $PORT >/dev/null 2>&1 &
SERVER_PID=$!
sleep 1
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo -e "${RED}✗ Failed to start server${NC}"
exit 1
fi
echo -e "${GREEN}✓ Server started (PID: $SERVER_PID)${NC}\n"
# Test function
test_http() {
local url=$1
local test_name=$2
local expected=$3
echo -e "${BLUE}Test:${NC} $test_name"
echo -e " URL: $url"
response=$(curl -s "$url")
if echo "$response" | grep -q "$expected"; then
echo -e " ${GREEN}✓ PASS${NC} - Found: $expected"
else
echo -e " ${RED}✗ FAIL${NC} - Expected to find: $expected"
echo -e " ${YELLOW}Response:${NC}"
echo "$response" | head -5
fi
echo ""
}
# Run tests
echo "=========================================="
echo " Running Tests"
echo "=========================================="
echo ""
test_http "http://localhost:$PORT/testdir/" \
"Directory listing - table headers" \
"Name</th>"
test_http "http://localhost:$PORT/testdir/" \
"Directory listing - file1.txt" \
"file1.txt"
test_http "http://localhost:$PORT/testdir/" \
"Directory listing - file2.txt" \
"file2.txt"
test_http "http://localhost:$PORT/testdir/" \
"Directory listing - subdirectory" \
"subdir/"
test_http "http://localhost:$PORT/testdir/" \
"Directory listing - parent link" \
"Parent Directory"
test_http "http://localhost:$PORT/testdir/" \
"Directory listing - file sizes" \
" B</td>"
test_http "http://localhost:$PORT/testdir/" \
"Directory listing - dates" \
"2026-"
test_http "http://localhost:$PORT/testdir/subdir/" \
"Subdirectory with index.html" \
"Subdir Index"
test_http "http://localhost:$PORT/testdir/emptydir/" \
"Empty directory listing" \
"Index of"
# Visual test
echo "=========================================="
echo " Visual Inspection"
echo "=========================================="
echo ""
echo -e "${YELLOW}Directory listing HTML:${NC}"
curl -s "http://localhost:$PORT/testdir/" | head -40
echo ""
echo "..."
echo ""
# Stop server
echo "=========================================="
echo -e "${BLUE}Stopping server...${NC}"
kill $SERVER_PID 2>/dev/null
wait $SERVER_PID 2>/dev/null
echo -e "${GREEN}✓ Server stopped${NC}"
echo ""
echo "=========================================="
echo " Test Suite Complete"
echo "=========================================="