@@ -76,3 +76,62 @@ func TestServer(t *testing.T) {
76
76
//nolint:staticcheck // Ignore as we are testing the server
77
77
os .RemoveAll (tempDir )
78
78
}
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