@@ -87,3 +87,102 @@ UTEST(StaticFileHandlerTest, SmallBuffer)
8787 EXPECT_TRUE (str_contains (response, " <!DOCTYPE html>"
8888 " <html><head><title>Example</title></head></html>\n " ));
8989}
90+
91+ UTEST (StaticFileHandlerTest, is_done)
92+ {
93+ RouteConfig rc;
94+ HttpRequest req;
95+ StaticFileHandler test (" www/example/index.html" , rc, req);
96+
97+ char buf[1 ] = {0 };
98+ std::string response;
99+
100+ while (!test.is_done () && test.has_output ()) {
101+ test.read_output (buf, sizeof (buf));
102+ response.append (buf, 1 );
103+ }
104+
105+ EXPECT_TRUE (str_contains (response, " HTTP/1.1 200 OK" ));
106+ EXPECT_TRUE (str_contains (response, " Content-Length: 64" ));
107+ EXPECT_TRUE (str_contains (response, " <!DOCTYPE html>"
108+ " <html><head><title>Example</title></head></html>\n " ));
109+ }
110+ UTEST (StaticFileHandlerTest, needs_input)
111+ {
112+ RouteConfig rc;
113+ HttpRequest req;
114+ StaticFileHandler test (" www/example/index.html" , rc, req);
115+ EXPECT_FALSE (test.needs_input ());
116+ }
117+
118+ UTEST (StaticFileHandlerTest, dir_autoindex_off_with_index_file)
119+ {
120+ RouteConfig rc;
121+ rc.shared .autoindex_enabled = false ;
122+ HttpRequest req;
123+ StaticFileHandler test (" www/example/" , rc, req);
124+
125+ char buf[1 ] = {0 };
126+ std::string response;
127+
128+ while (!test.is_done () && test.has_output ()) {
129+ test.read_output (buf, sizeof (buf));
130+ response.append (buf, 1 );
131+ }
132+ // std::cout << response << std::endl;
133+ EXPECT_TRUE (str_contains (response, " HTTP/1.1 200 OK" ));
134+ }
135+
136+ UTEST (StaticFileHandlerTest, dir_autoindex_off_no_index_file)
137+ {
138+ RouteConfig rc;
139+ rc.shared .autoindex_enabled = false ;
140+ HttpRequest req;
141+ StaticFileHandler test (" www/example/bin/" , rc, req);
142+
143+ char buf[1 ] = {0 };
144+ std::string response;
145+
146+ while (!test.is_done () && test.has_output ()) {
147+ test.read_output (buf, sizeof (buf));
148+ response.append (buf, 1 );
149+ }
150+ // std::cout << response << std::endl;
151+ EXPECT_TRUE (str_contains (response, " HTTP/1.1 403 Forbidden" ));
152+ }
153+
154+ UTEST (StaticFileHandlerTest, dir_autoindex_on_no_index_file)
155+ {
156+ RouteConfig rc;
157+ rc.shared .autoindex_enabled = true ;
158+ HttpRequest req;
159+ StaticFileHandler test (" www/example/bin/" , rc, req);
160+
161+ char buf[1 ] = {0 };
162+ std::string response;
163+
164+ while (!test.is_done () && test.has_output ()) {
165+ test.read_output (buf, sizeof (buf));
166+ response.append (buf, 1 );
167+ }
168+ // std::cout << response << std::endl;
169+ EXPECT_TRUE (str_contains (response, " HTTP/1.1 200 OK" ));
170+ }
171+
172+ UTEST (StaticFileHandlerTest, dir_autoindex_on_no_index_file_redirection)
173+ {
174+ RouteConfig rc;
175+ rc.shared .autoindex_enabled = true ;
176+ HttpRequest req;
177+ StaticFileHandler test (" www/example/bin" , rc, req);
178+
179+ char buf[1 ] = {0 };
180+ std::string response;
181+
182+ while (!test.is_done () && test.has_output ()) {
183+ test.read_output (buf, sizeof (buf));
184+ response.append (buf, 1 );
185+ }
186+ std::cout << response << std::endl;
187+ EXPECT_TRUE (str_contains (response, " HTTP/1.1 301 Moved Permanently" ));
188+ }
0 commit comments