Skip to content

Commit 088e19a

Browse files
committed
add tests and fix linter
1 parent 911392b commit 088e19a

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

main.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +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-
if _, err = os.Stat(filePath + ".html"); os.IsNotExist(err) {
41-
filePath += indexHTML
42-
} else {
43-
filePath += htmlExtension
44-
}
39+
if _, err := os.Stat(filePath + ".html"); err == nil {
40+
filePath += htmlExtension
41+
} else if stat, err := os.Stat(filePath); err == nil && stat.IsDir() {
42+
filePath += indexHTML
4543
}
4644
}
4745

main_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,62 @@ func TestServer(t *testing.T) {
7676
//nolint:staticcheck // Ignore as we are testing the server
7777
os.RemoveAll(tempDir)
7878
}
79+
80+
func TestServer_FileAndDirectorySameName(t *testing.T) {
81+
// Create a temporary directory
82+
tempDir := t.TempDir()
83+
84+
// Create an "index.html" file
85+
indexFilePath := filepath.Join(tempDir, "index.html")
86+
if err := os.WriteFile(indexFilePath, []byte("<html><body>Index File</body></html>"), 0600); err != nil {
87+
t.Fatalf("Failed to write file %s: %v", indexFilePath, err)
88+
}
89+
90+
// Create an "index" directory
91+
indexDirPath := filepath.Join(tempDir, "index")
92+
if err := os.MkdirAll(indexDirPath, 0600); err != nil {
93+
t.Fatalf("Failed to create directory %s: %v", indexDirPath, err)
94+
}
95+
96+
// Create an "index/index.html" file inside the directory
97+
indexDirFilePath := filepath.Join(indexDirPath, "index.html")
98+
if err := os.WriteFile(indexDirFilePath, []byte("<html><body>Index Directory</body></html>"), 0600); err != nil {
99+
t.Fatalf("Failed to write file %s: %v", indexDirFilePath, err)
100+
}
101+
102+
// Set the environment variable for the static file path
103+
t.Setenv("STATIC_DIR_PATH", tempDir)
104+
105+
go main()
106+
107+
time.Sleep(3 * time.Second) // Allow time for the server to start
108+
109+
tests := []struct {
110+
path string
111+
statusCode int
112+
}{
113+
{"/index", http.StatusOK}, // Should serve "index.html" file
114+
{"/index/", http.StatusOK}, // Should serve "index/index.html"
115+
{"/index.html", http.StatusOK},
116+
}
117+
118+
for _, test := range tests {
119+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://localhost:8000"+test.path, http.NoBody)
120+
if err != nil {
121+
t.Fatalf("Failed to create request: %v", err)
122+
}
123+
124+
client := &http.Client{}
125+
126+
resp, err := client.Do(req)
127+
if err != nil {
128+
t.Fatalf("Failed to perform request: %v", err)
129+
}
130+
131+
if resp.StatusCode != test.statusCode {
132+
t.Errorf("Expected status code %v, got %v for path %v", test.statusCode, resp.StatusCode, test.path)
133+
}
134+
135+
resp.Body.Close()
136+
}
137+
}

0 commit comments

Comments
 (0)