Skip to content

Commit 1d7f3b8

Browse files
Fix Incorrect Priority When Serving filePath.html vs. filePath/index.html
1 parent 20cc193 commit 1d7f3b8

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ func main() {
3636
if r.URL.Path == rootPath {
3737
filePath += indexHTML
3838
} else if !ok {
39-
if stat, err := os.Stat(filePath); err == nil && stat.IsDir() {
40-
filePath += indexHTML
41-
} else {
39+
if _, err := os.Stat(filePath + ".html"); err == nil {
4240
filePath += htmlExtension
41+
} else if stat, err := os.Stat(filePath); err == nil && stat.IsDir() {
42+
filePath += indexHTML
4343
}
4444
}
4545

main_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,20 @@ func TestServer(t *testing.T) {
1414
//nolint:staticcheck // Ignore as we are testing the server
1515
tempDir := os.TempDir()
1616

17+
// Create an "index" directory
18+
indexDirPath := filepath.Join(tempDir, "docs")
19+
if err := os.MkdirAll(indexDirPath, 0600); err != nil {
20+
t.Fatalf("Failed to create directory %s: %v", indexDirPath, err)
21+
}
22+
1723
// Create necessary files
1824
files := []struct {
1925
name string
2026
content string
2127
}{
2228
{"/index.html", "<html><body>Index</body></html>"},
2329
{"/404.html", "<html><body>404 Not Found</body></html>"},
30+
{"/docs.html", "<html><body>Index</body></html>"},
2431
}
2532

2633
for _, file := range files {
@@ -46,6 +53,7 @@ func TestServer(t *testing.T) {
4653
statusCode int
4754
}{
4855
{"/", http.StatusOK},
56+
{"/docs", http.StatusOK},
4957
{"/index", http.StatusOK},
5058
{"/index/", http.StatusOK},
5159
{tempDir + "/index.html", http.StatusNotFound},

0 commit comments

Comments
 (0)