@@ -1153,6 +1153,55 @@ Deno.test(async function serveFileHeadRequest() {
11531153 assertEquals ( res . headers . get ( "content-length" ) , "10034" ) ;
11541154} ) ;
11551155
1156+ Deno . test ( "serveDir() handles HEAD request for a file" , async ( ) => {
1157+ const req = new Request ( "http://localhost/test_file.txt" , {
1158+ method : "HEAD" ,
1159+ } ) ;
1160+ const res = await serveDir ( req , serveDirOptions ) ;
1161+
1162+ assert ( ! res . body ) ;
1163+ assertEquals ( res . status , 200 ) ;
1164+ assertEquals ( res . statusText , "OK" ) ;
1165+ assertEquals ( res . headers . get ( "content-type" ) , "text/plain; charset=UTF-8" ) ;
1166+ assertEquals ( res . headers . get ( "content-length" ) , `${ TEST_FILE_SIZE } ` ) ;
1167+ assertEquals ( res . headers . get ( "etag" ) , TEST_FILE_ETAG ) ;
1168+ assertEquals ( res . headers . get ( "last-modified" ) , TEST_FILE_LAST_MODIFIED ) ;
1169+ assertEquals ( res . headers . get ( "accept-ranges" ) , "bytes" ) ;
1170+ } ) ;
1171+
1172+ Deno . test ( "serveDir() handles HEAD request for index.html" , async ( ) => {
1173+ const req = new Request (
1174+ "http://localhost/http/testdata/subdir-with-index/" ,
1175+ { method : "HEAD" } ,
1176+ ) ;
1177+ const res = await serveDir ( req , { showIndex : true } ) ;
1178+
1179+ assert ( ! res . body ) ;
1180+ assertEquals ( res . status , 200 ) ;
1181+ assertEquals ( res . headers . get ( "content-type" ) , "text/html; charset=UTF-8" ) ;
1182+ } ) ;
1183+
1184+ Deno . test ( "serveDir() handles HEAD request for directory listing" , async ( ) => {
1185+ const req = new Request ( "http://localhost/" , { method : "HEAD" } ) ;
1186+ const res = await serveDir ( req , serveDirOptions ) ;
1187+
1188+ assert ( ! res . body ) ;
1189+ assertEquals ( res . status , 200 ) ;
1190+ assertEquals ( res . headers . get ( "content-type" ) , "text/html; charset=UTF-8" ) ;
1191+ assert ( Number ( res . headers . get ( "content-length" ) ) > 0 ) ;
1192+ } ) ;
1193+
1194+ Deno . test ( "serveDir() handles HEAD request for missing file" , async ( ) => {
1195+ const req = new Request ( "http://localhost/no_such_file.txt" , {
1196+ method : "HEAD" ,
1197+ } ) ;
1198+ const res = await serveDir ( req , serveDirOptions ) ;
1199+ await res . body ?. cancel ( ) ;
1200+
1201+ assertEquals ( res . status , 404 ) ;
1202+ assertEquals ( res . statusText , "Not Found" ) ;
1203+ } ) ;
1204+
11561205Deno . test ( "(unstable) serveDir() serves files without the need of html extension when cleanUrls=true" , async ( ) => {
11571206 const req = new Request ( "http://localhost/hello" ) ;
11581207 const res = await unstableServeDir ( req , {
0 commit comments