Skip to main content
Module

std/http/file_server_test.ts

Deno standard library
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { assert, assertEquals, assertStringIncludes,} from "../testing/asserts.ts";import { stub } from "../testing/mock.ts";import { iterateReader } from "../streams/iterate_reader.ts";import { writeAll } from "../streams/write_all.ts";import { TextLineStream } from "../streams/text_line_stream.ts";import { serveDir, serveFile } from "./file_server.ts";import { calculate } from "./etag.ts";import { dirname, fromFileUrl, join, resolve, toFileUrl } from "../path/mod.ts";import { isWindows } from "../_util/os.ts";import { VERSION } from "../version.ts";import { retry } from "../async/retry.ts";
let child: Deno.ChildProcess;
interface FileServerCfg { port?: string; cors?: boolean; "dir-listing"?: boolean; dotfiles?: boolean; host?: string; cert?: string; key?: string; help?: boolean; target?: string; headers?: string[];}
const moduleDir = dirname(fromFileUrl(import.meta.url));const testdataDir = resolve(moduleDir, "testdata");
async function startFileServer({ target = ".", port = "4507", "dir-listing": dirListing = true, dotfiles = true, headers = [],}: FileServerCfg = {}) { const fileServer = new Deno.Command(Deno.execPath(), { args: [ "run", "--no-check", "--quiet", "--allow-read", "--allow-net", "file_server.ts", target, "--cors", "-p", `${port}`, `${dirListing ? "" : "--no-dir-listing"}`, `${dotfiles ? "" : "--no-dotfiles"}`, ...headers.map((header) => "-H=" + header), ], cwd: moduleDir, stdout: "piped", stderr: "inherit", }); child = fileServer.spawn(); // Once fileServer is ready it will write to its stdout. const r = child.stdout.pipeThrough(new TextDecoderStream()).pipeThrough( new TextLineStream(), ); const reader = r.getReader(); const res = await reader.read(); assert(!res.done && res.value.includes("Listening")); reader.releaseLock();}
async function startFileServerAsLibrary({}: FileServerCfg = {}) { const fileServer = new Deno.Command(Deno.execPath(), { args: [ "run", "--no-check", "--quiet", "--allow-read", "--allow-net", "testdata/file_server_as_library.ts", ], cwd: moduleDir, stdout: "piped", stderr: "null", }); child = fileServer.spawn(); const r = child.stdout.pipeThrough(new TextDecoderStream()).pipeThrough( new TextLineStream(), ); const reader = r.getReader(); const res = await reader.read(); assert(!res.done && res.value.includes("Server running...")); reader.releaseLock();}
async function killFileServer() { // Note: We retry this because 'Access is denied' error is thrown sometimes // on windows await retry(() => { try { child.kill("SIGKILL"); } catch (e) { if ( e instanceof TypeError && e.message === "Child process has already terminated." ) { return; } throw e; } }); await child.status;}
/* HTTP GET request allowing arbitrary paths */async function fetchExactPath( hostname: string, port: number, path: string,): Promise<Response> { const encoder = new TextEncoder(); const decoder = new TextDecoder(); const request = encoder.encode("GET " + path + " HTTP/1.1\r\n\r\n"); let conn: void | Deno.Conn; try { conn = await Deno.connect( { hostname: hostname, port: port, transport: "tcp" }, ); await writeAll(conn, request); let currentResult = ""; let contentLength = -1; let startOfBody = -1; for await (const chunk of iterateReader(conn)) { currentResult += decoder.decode(chunk); if (contentLength === -1) { const match = /^content-length: (.*)$/m.exec(currentResult); if (match && match[1]) { contentLength = Number(match[1]); } } if (startOfBody === -1) { const ind = currentResult.indexOf("\r\n\r\n"); if (ind !== -1) { startOfBody = ind + 4; } } if (startOfBody !== -1 && contentLength !== -1) { const byteLen = encoder.encode(currentResult).length; if (byteLen >= contentLength + startOfBody) { break; } } } const status = /^HTTP\/1.1 (...)/.exec(currentResult); let statusCode = 0; if (status && status[1]) { statusCode = Number(status[1]); }
const body = currentResult.slice(startOfBody); const headersStr = currentResult.slice(0, startOfBody); const headersReg = /^(.*): (.*)$/mg; const headersObj: { [i: string]: string } = {}; let match = headersReg.exec(headersStr); while (match !== null) { if (match[1] && match[2]) { headersObj[match[1]] = match[2]; } match = headersReg.exec(headersStr); } return new Response(body, { status: statusCode, headers: new Headers(headersObj), }); } finally { if (conn) { conn.close(); } }}
Deno.test( "file_server serveFile", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/mod.ts"); assertEquals( res.headers.get("content-type"), "video/mp2t", ); const downloadedFile = await res.text(); const localFile = new TextDecoder().decode( await Deno.readFile(join(moduleDir, "mod.ts")), ); assertEquals(downloadedFile, localFile); } finally { await killFileServer(); } },);
Deno.test( "file_server serveFile in testdata", async () => { await startFileServer({ target: "./testdata" }); try { const res = await fetch("http://localhost:4507/hello.html"); assertEquals(res.headers.get("content-type"), "text/html; charset=UTF-8"); const downloadedFile = await res.text(); const localFile = new TextDecoder().decode( await Deno.readFile(join(testdataDir, "hello.html")), ); assertEquals(downloadedFile, localFile); } finally { await killFileServer(); } },);
Deno.test( "file_server serveFile with filename including hash symbol", async () => { await startFileServer({ target: "./testdata" }); try { const res = await fetch("http://localhost:4507/file%232.txt"); assertEquals( res.headers.get("content-type"), "text/plain; charset=UTF-8", ); const downloadedFile = await res.text(); const localFile = new TextDecoder().decode( await Deno.readFile(join(testdataDir, "file#2.txt")), ); assertEquals(downloadedFile, localFile); } finally { await killFileServer(); } },);
Deno.test("serveDirIndex", async function () { await startFileServer(); try { const res = await fetch("http://localhost:4507/"); const page = await res.text(); assert(page.includes("mod.ts")); assert(page.includes(`<a href="/testdata/">testdata/</a>`));
// `Deno.FileInfo` is not completely compatible with Windows yet // TODO(bartlomieju): `mode` should work correctly in the future. // Correct this test case accordingly. isWindows === false && assert(/<td class="mode">(\s)*[a-zA-Z- ]{14}(\s)*<\/td>/.test(page)); isWindows && assert(/<td class="mode">(\s)*\(unknown mode\)(\s)*<\/td>/.test(page)); assert(page.includes(`<a href="/mod.ts">mod.ts</a>`)); } finally { await killFileServer(); }});
Deno.test("serveDirIndex with filename including percent symbol", async function () { await startFileServer(); try { const res = await fetch("http://localhost:4507/testdata/"); const page = await res.text(); assertStringIncludes(page, "%2525A.txt"); } finally { await killFileServer(); }});
Deno.test("serveDirIndex with filename including hash symbol", async function () { await startFileServer(); try { const res = await fetch("http://localhost:4507/testdata/"); const page = await res.text(); assertStringIncludes(page, "/testdata/file%232.txt"); } finally { await killFileServer(); }});
Deno.test("serveDirIndex returns a response even if fileinfo is inaccessible", async function () { // Note: Deno.stat for windows system files may be rejected with os error 32. // Mock Deno.stat to test that the dirlisting page can be generated // even if the fileInfo for a particular file cannot be obtained.
// Assuming that fileInfo of `test file.txt` cannot be accessible const denoStatStub = stub(Deno, "stat", (path): Promise<Deno.FileInfo> => { if (path.toString().includes("test file.txt")) { return Promise.reject(new Error("__stubed_error__")); } return denoStatStub.original.call(Deno, path); });
try { const url = "http://localhost:4507/http/testdata/"; const res = await serveDir(new Request(url), { showDirListing: true });
assertEquals(res.status, 200); assertStringIncludes(await res.text(), "/testdata/test%20file.txt"); } finally { denoStatStub.restore(); }});
Deno.test("serveFallback", async function () { await startFileServer(); try { const res = await fetch("http://localhost:4507/badfile.txt"); assertEquals(res.status, 404); const _ = await res.text(); } finally { await killFileServer(); }});
Deno.test("checkPathTraversal", async function () { await startFileServer(); try { const res = await fetch( "http://localhost:4507/../../../../../../../..", );
assertEquals(res.status, 200); const listing = await res.text(); assertStringIncludes(listing, "mod.ts"); } finally { await killFileServer(); }});
Deno.test("checkPathTraversalNoLeadingSlash", async function () { await startFileServer(); try { const res = await fetchExactPath("127.0.0.1", 4507, "../../../.."); assertEquals(res.status, 400); } finally { await killFileServer(); }});
Deno.test("checkPathTraversalAbsoluteURI", async function () { await startFileServer(); try { //allowed per https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html const res = await fetchExactPath( "127.0.0.1", 4507, "http://localhost/../../../..", ); assertEquals(res.status, 200); assertStringIncludes(await res.text(), "mod.ts"); } finally { await killFileServer(); }});
Deno.test("checkURIEncodedPathTraversal", async function () { await startFileServer(); try { const res = await fetch( "http://localhost:4507/%2F..%2F..%2F..%2F..%2F..%2F..%2F..%2F..", );
assertEquals(res.status, 200); assertStringIncludes(await res.text(), "mod.ts"); } finally { await killFileServer(); }});
Deno.test("serveWithUnorthodoxFilename", async function () { await startFileServer(); try { let res = await fetch("http://localhost:4507/testdata/%25"); assert(res.headers.has("access-control-allow-origin")); assert(res.headers.has("access-control-allow-headers")); assertEquals(res.status, 200); const _ = await res.text(); res = await fetch("http://localhost:4507/testdata/test%20file.txt"); assert(res.headers.has("access-control-allow-origin")); assert(res.headers.has("access-control-allow-headers")); assertEquals(res.status, 200); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); }});
Deno.test("CORS support", async function () { await startFileServer(); try { const directoryRes = await fetch("http://localhost:4507/"); assert(directoryRes.headers.has("access-control-allow-origin")); assert(directoryRes.headers.has("access-control-allow-headers")); assertEquals(directoryRes.status, 200); await directoryRes.text(); // Consuming the body so that the test doesn't leak resources
const fileRes = await fetch("http://localhost:4507/testdata/hello.html"); assert(fileRes.headers.has("access-control-allow-origin")); assert(fileRes.headers.has("access-control-allow-headers")); assertEquals(fileRes.status, 200); await fileRes.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); }});
Deno.test("printHelp", async function () { const command = new Deno.Command(Deno.execPath(), { args: [ "run", "--no-check", "--quiet", "file_server.ts", "--help", ], cwd: moduleDir, }); const { stdout } = await command.output(); const output = new TextDecoder().decode(stdout); assert(output.includes(`Deno File Server ${VERSION}`));});
Deno.test("printVersion", async function () { const command = new Deno.Command(Deno.execPath(), { args: [ "run", "--no-check", "--quiet", "file_server.ts", "--version", ], cwd: moduleDir, }); const { stdout } = await command.output(); const output = new TextDecoder().decode(stdout); assert(output.includes(`Deno File Server ${VERSION}`));});
Deno.test("contentType", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/testdata/hello.html"); const contentType = res.headers.get("content-type"); assertEquals(contentType, "text/html; charset=UTF-8"); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); }});
Deno.test("file_server running as library", async function () { await startFileServerAsLibrary(); try { const res = await fetch("http://localhost:8000"); assertEquals(res.status, 200); const _ = await res.text(); } finally { await killFileServer(); }});
Deno.test("file_server should ignore query params", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/mod.ts?key=value"); assertEquals(res.status, 200); const downloadedFile = await res.text(); const localFile = new TextDecoder().decode( await Deno.readFile(join(moduleDir, "mod.ts")), ); assertEquals(downloadedFile, localFile); } finally { await killFileServer(); }});
async function startTlsFileServer({ target = ".", port = "4577",}: FileServerCfg = {}) { const fileServer = new Deno.Command(Deno.execPath(), { args: [ "run", "--no-check", "--quiet", "--allow-read", "--allow-net", "file_server.ts", target, "--host", "localhost", "--cert", "./testdata/tls/localhost.crt", "--key", "./testdata/tls/localhost.key", "--cors", "-p", `${port}`, ], cwd: moduleDir, stdout: "piped", stderr: "null", }); child = fileServer.spawn(); // Once fileServer is ready it will write to its stdout. const r = child.stdout.pipeThrough(new TextDecoderStream()).pipeThrough( new TextLineStream(), ); const reader = r.getReader(); const res = await reader.read(); assert(!res.done && res.value.includes("Listening")); reader.releaseLock();}
Deno.test("serveDirIndex TLS", async function () { await startTlsFileServer(); try { // Valid request after invalid const conn = await Deno.connectTls({ hostname: "localhost", port: 4577, certFile: join(testdataDir, "tls/RootCA.pem"), });
await writeAll( conn, new TextEncoder().encode("GET / HTTP/1.0\r\n\r\n"), ); const res = new Uint8Array(128 * 1024); const nread = await conn.read(res); assert(nread !== null); conn.close(); const page = new TextDecoder().decode(res.subarray(0, nread)); assert(page.includes("<title>Deno File Server</title>")); } finally { await killFileServer(); }});
Deno.test("partial TLS arguments fail", async function () { const fileServer = new Deno.Command(Deno.execPath(), { args: [ "run", "--no-check", "--quiet", "--allow-read", "--allow-net", "file_server.ts", ".", "--host", "localhost", "--cert", "./testdata/tls/localhost.crt", "-p", `4578`, ], cwd: moduleDir, stdout: "piped", stderr: "null", }); child = fileServer.spawn(); try { // Once fileServer is ready it will write to its stdout. const r = child.stdout.pipeThrough(new TextDecoderStream()) .pipeThrough(new TextLineStream()); const reader = r.getReader(); const res = await reader.read(); assert( !res.done && res.value.includes("--key and --cert are required for TLS"), ); reader.releaseLock(); } finally { await killFileServer(); }});
Deno.test("file_server disable dir listings", async function () { await startFileServer({ "dir-listing": false }); try { const res = await fetch("http://localhost:4507/");
assertEquals(res.status, 404); const _ = await res.text(); } finally { await killFileServer(); }});
Deno.test("file_server do not show dotfiles", async function () { await startFileServer({ dotfiles: false }); try { let res = await fetch("http://localhost:4507/testdata/"); assert(!(await res.text()).includes(".dotfile"));
res = await fetch("http://localhost:4507/testdata/.dotfile"); assertEquals(await res.text(), "dotfile"); } finally { await killFileServer(); }});
Deno.test("file_server should show .. if it makes sense", async function (): Promise< void> { await startFileServer(); try { let res = await fetch("http://localhost:4507/"); let page = await res.text(); assert(!page.includes("../")); assert(page.includes("testdata/"));
res = await fetch("http://localhost:4507/testdata/"); page = await res.text(); assert(page.includes("../")); } finally { await killFileServer(); }});
Deno.test( "file_server should download first byte of hello.html file", async () => { await startFileServer(); try { const headers = { "range": "bytes=0-0", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); const text = await res.text(); console.log(text); assertEquals(text, "L"); } finally { await killFileServer(); } },);
Deno.test( "file_server sets `content-range` header for range request responses", async () => { await startFileServer(); try { const headers = { "range": "bytes=0-100", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes 0-100/${contentLength}`, );
await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
const getTestFileSize = async () => { const fileInfo = await getTestFileStat(); return fileInfo.size;};
const getTestFileStat = async (): Promise<Deno.FileInfo> => { const fsPath = join(testdataDir, "test file.txt"); const fileInfo = await Deno.stat(fsPath);
return fileInfo;};
const getTestFileEtag = async () => { const fileInfo = await getTestFileStat(); const etag = await calculate(fileInfo); assert(etag); return etag;};
const getTestFileLastModified = async () => { const fileInfo = await getTestFileStat();
if (fileInfo.mtime instanceof Date) { return new Date(fileInfo.mtime).toUTCString(); } else { return ""; }};
Deno.test( "file_server returns 206 for range request responses", async () => { await startFileServer(); try { const headers = { "range": "bytes=0-100", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, );
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes 0-100/${contentLength}`, );
assertEquals(res.status, 206); assertEquals((await res.arrayBuffer()).byteLength, 101); } finally { await killFileServer(); } },);
Deno.test( "file_server should download from 300 bytes into `hello.html` file until the end", async () => { await startFileServer(); try { const headers = { "range": "bytes=300-", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); const text = await res.text();
const localFile = new TextDecoder().decode( await Deno.readFile(join(testdataDir, "test file.txt")), );
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes 300-${contentLength - 1}/${contentLength}`, ); assertEquals(text, localFile.substring(300)); } finally { await killFileServer(); } },);
Deno.test( "file_server should download last 200 bytes (-200)", async () => { await startFileServer(); try { const headers = { "range": "bytes=-200", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals( await res.text(), (await Deno.readTextFile(join(testdataDir, "test file.txt"))) .slice(-200), );
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes ${contentLength - 200}-${contentLength - 1}/${contentLength}`, );
assertEquals(res.status, 206); assertEquals(res.statusText, "Partial Content"); } finally { await killFileServer(); } },);
Deno.test( "file_server should clamp Ranges that are too large (0-999999999)", async () => { await startFileServer(); try { const headers = { "range": "bytes=0-999999999", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals( await res.text(), await Deno.readTextFile(join(testdataDir, "test file.txt")), );
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes 0-${contentLength - 1}/${contentLength}`, );
assertEquals(res.status, 206); assertEquals(res.statusText, "Partial Content"); } finally { await killFileServer(); } },);
Deno.test( "file_server should clamp Ranges that are too large (-999999999)", async () => { await startFileServer(); try { const headers = { // it means the last 999999999 bytes. It is too big and should be clamped. "range": "bytes=-999999999", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals( await res.text(), await Deno.readTextFile(join(testdataDir, "test file.txt")), );
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes 0-${contentLength - 1}/${contentLength}`, );
assertEquals(res.status, 206); assertEquals(res.statusText, "Partial Content"); } finally { await killFileServer(); } },);
Deno.test( "file_server should return 416 due to a bad range request (500-200)", async () => { await startFileServer(); try { const headers = { "range": "bytes=500-200", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); await res.text();
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes */${contentLength}`, );
assertEquals(res.status, 416); assertEquals(res.statusText, "Range Not Satisfiable"); } finally { await killFileServer(); } },);
Deno.test( "file_server should return 416 due to out of range request (99999-999999)", async () => { await startFileServer(); try { const headers = { "range": "bytes=99999-999999", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); await res.text();
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes */${contentLength}`, );
assertEquals(res.status, 416); assertEquals(res.statusText, "Range Not Satisfiable"); } finally { await killFileServer(); } },);
Deno.test( "file_server should return 416 due to out of range request (99999)", async () => { await startFileServer(); try { const headers = { "range": "bytes=99999-", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); await res.text();
const contentLength = await getTestFileSize(); assertEquals( res.headers.get("content-range"), `bytes */${contentLength}`, );
assertEquals(res.status, 416); assertEquals(res.statusText, "Range Not Satisfiable"); } finally { await killFileServer(); } },);
Deno.test( "file_server should return 200 OK and ignore bad range request (100)", async () => { await startFileServer(); try { const headers = { "range": "bytes=100", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals( await res.text(), await Deno.readTextFile(join(testdataDir, "test file.txt")), ); assertEquals(res.status, 200); assertEquals(res.statusText, "OK"); } finally { await killFileServer(); } },);
Deno.test( "file_server should return 200 OK and ignore bad range request (a-b)", async () => { await startFileServer(); try { const headers = { "range": "bytes=a-b", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals( await res.text(), await Deno.readTextFile(join(testdataDir, "test file.txt")), ); assertEquals(res.status, 200); assertEquals(res.statusText, "OK"); } finally { await killFileServer(); } },);
Deno.test( "file_server should return 200 OK and ignore unsupported multi range request (0-10, 20-30)", async () => { await startFileServer(); try { const headers = { "range": "bytes=0-10, 20-30", }; const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals( await res.text(), await Deno.readTextFile(join(testdataDir, "test file.txt")), ); assertEquals(res.status, 200); assertEquals(res.statusText, "OK"); } finally { await killFileServer(); } },);
Deno.test( "file_server should return 200 OK due to range request for empty file", async () => { await startFileServer(); try { const headers = { "range": "bytes=-100", }; const res = await fetch( "http://localhost:4507/testdata/test_empty_file.txt", { headers }, );
assertEquals(await res.text(), "");
assertEquals(res.status, 200); assertEquals(res.statusText, "OK"); } finally { await killFileServer(); } },);
Deno.test( "file_server returns correct mime-types", async () => { await startFileServer(); try { const txtRes = await fetch( "http://localhost:4507/testdata/test%20file.txt", ); assertEquals( txtRes.headers.get("content-type"), "text/plain; charset=UTF-8", ); await txtRes.text(); // Consuming the body so that the test doesn't leak resources
const htmlRes = await fetch("http://localhost:4507/testdata/hello.html"); assertEquals( htmlRes.headers.get("content-type"), "text/html; charset=UTF-8", ); await htmlRes.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server sets `accept-ranges` header to `bytes` for directory listings", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/"); assertEquals(res.headers.get("accept-ranges"), "bytes"); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server sets `accept-ranges` header to `bytes` for file responses", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/testdata/test%20file.txt"); assertEquals(res.headers.get("accept-ranges"), "bytes"); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test("file_server sets `Last-Modified` header correctly", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/testdata/test%20file.txt");
const lastModifiedHeader = res.headers.get("last-modified") as string; const lastModifiedTime = Date.parse(lastModifiedHeader);
const fileInfo = await getTestFileStat(); const expectedTime = fileInfo.mtime && fileInfo.mtime instanceof Date ? fileInfo.mtime.getTime() : Number.NaN;
const round = (d: number) => Math.floor(d / 1000 / 60 / 30); // Rounds epochs to 2 minute units, to accommodate minor variances in how long the test(s) take to execute assertEquals(round(lastModifiedTime), round(expectedTime)); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); }});
Deno.test("file_server sets `Date` header correctly", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/testdata/test%20file.txt"); const dateHeader = res.headers.get("date") as string; const date = Date.parse(dateHeader); const fileInfo = await getTestFileStat(); const expectedTime = fileInfo.atime && fileInfo.atime instanceof Date ? fileInfo.atime.getTime() : Number.NaN; const round = (d: number) => Math.floor(d / 1000 / 60 / 30); // Rounds epochs to 2 minute units, to accommodate minor variances in how long the test(s) take to execute assertEquals(round(date), round(expectedTime)); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); }});
Deno.test( "file_server sets headers correctly if provided as arguments", async () => { await startFileServer({ headers: ["cache-control:max-age=100", "x-custom-header:hi"], }); try { const res = await fetch("http://localhost:4507/testdata/test%20file.txt"); assertEquals(res.headers.get("cache-control"), "max-age=100"); assertEquals(res.headers.get("x-custom-header"), "hi"); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server file responses includes correct etag", async () => { await startFileServer(); try { const res = await fetch("http://localhost:4507/testdata/test%20file.txt"); const expectedEtag = await getTestFileEtag(); assertEquals(res.headers.get("etag"), expectedEtag); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server returns 304 for requests with if-none-match set with the etag", async () => { await startFileServer(); try { const expectedEtag = await getTestFileEtag(); const headers = new Headers(); headers.set("if-none-match", expectedEtag); const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals(res.status, 304); assertEquals(res.statusText, "Not Modified"); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server returns an empty body for 304 responses from requests with if-none-match set with the etag", async () => { await startFileServer(); try { const expectedEtag = await getTestFileEtag(); const headers = new Headers(); headers.set("if-none-match", expectedEtag); const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals(await res.text(), ""); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server returns 304 for requests with if-modified-since if the requested resource has not been modified after the given date", async () => { await startFileServer(); try { const expectedIfModifiedSince = await getTestFileLastModified(); const headers = new Headers(); headers.set("if-modified-since", expectedIfModifiedSince); const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals(res.status, 304); assertEquals(res.statusText, "Not Modified"); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server if both `if-none-match` and `if-modified-since` headers are provided, use only `if-none-match`", async () => { await startFileServer(); try { // When used in combination with If-None-Match, If-Modified-Since is ignored // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since // -> If etag doesn't match, don't return 304 even if if-modified-since is a valid value.
const expectedIfModifiedSince = await getTestFileLastModified(); const headers = new Headers(); headers.set("if-none-match", "not match etag"); headers.set("if-modified-since", expectedIfModifiedSince); const res = await fetch( "http://localhost:4507/testdata/test%20file.txt", { headers }, ); assertEquals(res.status, 200); assertEquals(res.statusText, "OK"); await res.text(); // Consuming the body so that the test doesn't leak resources } finally { await killFileServer(); } },);
Deno.test( "file_server `serveFile` serve test file", async () => { const req = new Request("http://localhost:4507/testdata/test file.txt"); const testdataPath = join(testdataDir, "test file.txt"); const res = await serveFile(req, testdataPath); const localFile = new TextDecoder().decode( await Deno.readFile(testdataPath), ); assertEquals(res.status, 200); assertEquals(await res.text(), localFile); },);
Deno.test( "file_server `serveFile` returns 404 due to file not found", async () => { const req = new Request("http://localhost:4507/testdata/non_existent.txt"); const testdataPath = join(testdataDir, "non_existent.txt"); const res = await serveFile(req, testdataPath); assertEquals(res.status, 404); assertEquals(res.statusText, "Not Found"); },);
Deno.test( "file_server `serveFile` returns 404 when the given path is a directory", async () => { const req = new Request("http://localhost:4507/testdata/"); const res = await serveFile(req, testdataDir); assertEquals(res.status, 404); assertEquals(res.statusText, "Not Found"); },);
Deno.test( "file_server `serveFile` should return 206 due to a bad range request (200-500)", async () => { const req = new Request("http://localhost:4507/testdata/test file.txt"); req.headers.set("range", "bytes=200-500"); const testdataPath = join(testdataDir, "test file.txt"); const res = await serveFile(req, testdataPath); assertEquals(res.status, 206); assertEquals((await res.arrayBuffer()).byteLength, 301); },);
Deno.test( "file_server `serveFile` should return 416 due to a bad range request (500-200)", async () => { const req = new Request("http://localhost:4507/testdata/test file.txt"); req.headers.set("range", "bytes=500-200"); const testdataPath = join(testdataDir, "test file.txt"); const res = await serveFile(req, testdataPath); assertEquals(res.status, 416); },);
Deno.test( "file_server `serveFile` returns 304 for requests with if-modified-since if the requested resource has not been modified after the given date", async () => { const req = new Request("http://localhost:4507/testdata/test file.txt"); const expectedEtag = await getTestFileEtag(); req.headers.set("if-none-match", expectedEtag); const testdataPath = join(testdataDir, "test file.txt"); const res = await serveFile(req, testdataPath); assertEquals(res.status, 304); assertEquals(res.statusText, "Not Modified"); },);
Deno.test( "file_server `serveFile` if both `if-none-match` and `if-modified-since` headers are provided, use only `if-none-match`", async () => { // When used in combination with If-None-Match, If-Modified-Since is ignored // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since // -> If etag doesn't match, don't return 304 even if if-modified-since is a valid value.
const expectedIfModifiedSince = await getTestFileLastModified(); const req = new Request("http://localhost:4507/testdata/test file.txt"); req.headers.set("if-none-match", "not match etag"); req.headers.set("if-modified-since", expectedIfModifiedSince); const testdataPath = join(testdataDir, "test file.txt"); const res = await serveFile(req, testdataPath); assertEquals(res.status, 200); assertEquals(res.statusText, "OK"); await res.text(); // Consuming the body so that the test doesn't leak resources },);
Deno.test( "file_server `serveFile` etag value falls back to DENO_DEPLOYMENT_ID if fileInfo.mtime is not available", async () => { const testDenoDeploymentId = "__THIS_IS_DENO_DEPLOYMENT_ID__"; const hashedDenoDeploymentId = await calculate(testDenoDeploymentId, { weak: true, }); // deno-fmt-ignore const code = ` import { serveFile } from "${import.meta.resolve("./file_server.ts")}"; import { fromFileUrl } from "${import.meta.resolve("../path/mod.ts")}"; import { assertEquals } from "${import.meta.resolve("../testing/asserts.ts")}"; const testdataPath = "${toFileUrl(join(testdataDir, "test file.txt"))}"; const fileInfo = await Deno.stat(new URL(testdataPath)); fileInfo.mtime = null; const req = new Request("http://localhost:4507/testdata/test file.txt"); const res = await serveFile(req, fromFileUrl(testdataPath), { fileInfo }); assertEquals(res.headers.get("etag"), \`${hashedDenoDeploymentId}\`); `; const command = new Deno.Command(Deno.execPath(), { args: ["eval", code], stdout: "inherit", stderr: "inherit", env: { DENO_DEPLOYMENT_ID: testDenoDeploymentId }, }); const { success } = await command.output(); assert(success); },);
Deno.test( "serveDir (without options) serves files under the current dir", async () => { const req = new Request("http://localhost:4507/http/testdata/hello.html"); const res = await serveDir(req); assertEquals(res.status, 200); assertStringIncludes(await res.text(), "Hello World"); },);
Deno.test( "serveDir (with fsRoot option) serves files under the given dir", async () => { const req = new Request("http://localhost:4507/testdata/hello.html"); const res = await serveDir(req, { fsRoot: "http" }); assertEquals(res.status, 200); assertStringIncludes(await res.text(), "Hello World"); },);
Deno.test( "serveDir (with fsRoot, urlRoot option) serves files under the given dir", async () => { const req = new Request( "http://localhost:4507/my-static-root/testdata/hello.html", ); const res = await serveDir(req, { fsRoot: "http", urlRoot: "my-static-root", }); assertEquals(res.status, 200); assertStringIncludes(await res.text(), "Hello World"); },);
Deno.test( "serveDir serves index.html when showIndex is true", async () => { const url = "http://localhost:4507/http/testdata/subdir-with-index/"; const expectedText = "This is subdir-with-index/index.html"; { const res = await serveDir(new Request(url), { showIndex: true }); assertEquals(res.status, 200); assertStringIncludes(await res.text(), expectedText); }
{ // showIndex is true by default const res = await serveDir(new Request(url)); assertEquals(res.status, 200); assertStringIncludes(await res.text(), expectedText); } },);
Deno.test( "serveDir doesn't serve index.html when showIndex is false", async () => { const url = "http://localhost:4507/http/testdata/subdir-with-index/"; const res = await serveDir(new Request(url), { showIndex: false }); assertEquals(res.status, 404); },);
Deno.test( "serveDir redirects a directory URL not ending with a slash if it has an index", async () => { const url = "http://localhost:4507/http/testdata/subdir-with-index"; const res = await serveDir(new Request(url), { showIndex: true }); assertEquals(res.status, 301); assertEquals( res.headers.get("Location"), "http://localhost:4507/http/testdata/subdir-with-index/", ); },);
Deno.test( "serveDir redirects a directory URL not ending with a slash correctly even with a query string", async () => { const url = "http://localhost:4507/http/testdata/subdir-with-index?test"; const res = await serveDir(new Request(url), { showIndex: true }); assertEquals(res.status, 301); assertEquals( res.headers.get("Location"), "http://localhost:4507/http/testdata/subdir-with-index/?test", ); },);
Deno.test( "serveDir redirects a file URL ending with a slash correctly even with a query string", async () => { const url = "http://localhost:4507/http/testdata/test%20file.txt/?test"; const res = await serveDir(new Request(url), { showIndex: true }); assertEquals(res.status, 301); assertEquals( res.headers.get("Location"), "http://localhost:4507/http/testdata/test%20file.txt?test", ); },);
Deno.test( "serveDir redirects non-canonical URLs", async () => { const url = "http://localhost:4507/http/testdata//////test%20file.txt/////?test"; const res = await serveDir(new Request(url), { showIndex: true }); assertEquals(res.status, 301); assertEquals( res.headers.get("Location"), "http://localhost:4507/http/testdata/test%20file.txt/?test", ); },);
Deno.test( "file_server returns 304 for requests with if-none-match set with the etag but with W/ prefixed etag in request headers.", async () => { await startFileServer(); try { const testurl = "http://localhost:4507/testdata/desktop.ini"; const fileurl = new URL("./testdata/desktop.ini", import.meta.url); let etag: string | undefined | null;
{ const res = await fetch( testurl, { headers: [ ["Accept-Encoding", "gzip, deflate, br"], ], }, ); assertEquals(res.status, 200); assertEquals(res.statusText, "OK");
const data = await Deno.readTextFile( fileurl, ); assertEquals(data, await res.text()); // Consuming the body so that the test doesn't leak resources etag = res.headers.get("etag"); }
assert(typeof etag === "string"); assert(etag.length > 0); assert(etag.startsWith("W/")); { const res = await fetch( testurl, { headers: { "if-none-match": etag, }, }, ); assertEquals(res.status, 304); assertEquals(res.statusText, "Not Modified"); assertEquals("", await res.text()); // Consuming the body so that the test doesn't leak resources assert( etag === res.headers.get("etag") || etag === "W/" + res.headers.get("etag"), ); } } finally { await killFileServer(); } },);