diff options
Diffstat (limited to 'std/http')
-rw-r--r-- | std/http/_io_test.ts | 125 | ||||
-rw-r--r-- | std/http/cookie_test.ts | 7 | ||||
-rwxr-xr-x | std/http/file_server.ts | 22 | ||||
-rw-r--r-- | std/http/file_server_test.ts | 81 | ||||
-rw-r--r-- | std/http/racing_server_test.ts | 7 | ||||
-rw-r--r-- | std/http/server.ts | 24 | ||||
-rw-r--r-- | std/http/server_test.ts | 169 |
7 files changed, 229 insertions, 206 deletions
diff --git a/std/http/_io_test.ts b/std/http/_io_test.ts index 3b385d013..473c40637 100644 --- a/std/http/_io_test.ts +++ b/std/http/_io_test.ts @@ -18,11 +18,13 @@ import { BufReader, ReadLineResult } from "../io/bufio.ts"; import { ServerRequest, Response } from "./server.ts"; import { StringReader } from "../io/readers.ts"; import { mockConn } from "./_mock_conn.ts"; -const { Buffer, test, readAll } = Deno; -test("bodyReader", async () => { +Deno.test("bodyReader", async () => { const text = "Hello, Deno"; - const r = bodyReader(text.length, new BufReader(new Buffer(encode(text)))); + const r = bodyReader( + text.length, + new BufReader(new Deno.Buffer(encode(text))) + ); assertEquals(decode(await Deno.readAll(r)), text); }); function chunkify(n: number, char: string): string { @@ -31,7 +33,7 @@ function chunkify(n: number, char: string): string { .join(""); return `${n.toString(16)}\r\n${v}\r\n`; } -test("chunkedBodyReader", async () => { +Deno.test("chunkedBodyReader", async () => { const body = [ chunkify(3, "a"), chunkify(5, "b"), @@ -40,11 +42,11 @@ test("chunkedBodyReader", async () => { chunkify(0, ""), ].join(""); const h = new Headers(); - const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); + const r = chunkedBodyReader(h, new BufReader(new Deno.Buffer(encode(body)))); let result: number | null; // Use small buffer as some chunks exceed buffer size const buf = new Uint8Array(5); - const dest = new Buffer(); + const dest = new Deno.Buffer(); while ((result = await r.read(buf)) !== null) { const len = Math.min(buf.byteLength, result); await dest.write(buf.subarray(0, len)); @@ -53,7 +55,7 @@ test("chunkedBodyReader", async () => { assertEquals(new TextDecoder().decode(dest.bytes()), exp); }); -test("chunkedBodyReader with trailers", async () => { +Deno.test("chunkedBodyReader with trailers", async () => { const body = [ chunkify(3, "a"), chunkify(5, "b"), @@ -67,7 +69,7 @@ test("chunkedBodyReader with trailers", async () => { const h = new Headers({ trailer: "deno,node", }); - const r = chunkedBodyReader(h, new BufReader(new Buffer(encode(body)))); + const r = chunkedBodyReader(h, new BufReader(new Deno.Buffer(encode(body)))); assertEquals(h.has("trailer"), true); assertEquals(h.has("deno"), false); assertEquals(h.has("node"), false); @@ -79,54 +81,63 @@ test("chunkedBodyReader with trailers", async () => { assertEquals(h.get("node"), "js"); }); -test("readTrailers", async () => { +Deno.test("readTrailers", async () => { const h = new Headers({ trailer: "Deno, Node", }); const trailer = ["deno: land", "node: js", "", ""].join("\r\n"); - await readTrailers(h, new BufReader(new Buffer(encode(trailer)))); + await readTrailers(h, new BufReader(new Deno.Buffer(encode(trailer)))); assertEquals(h.has("trailer"), false); assertEquals(h.get("deno"), "land"); assertEquals(h.get("node"), "js"); }); -test("readTrailer should throw if undeclared headers found in trailer", async () => { - const patterns = [ - ["deno,node", "deno: land\r\nnode: js\r\ngo: lang\r\n\r\n"], - ["deno", "node: js\r\n\r\n"], - ["deno", "node:js\r\ngo: lang\r\n\r\n"], - ]; - for (const [header, trailer] of patterns) { - const h = new Headers({ - trailer: header, - }); - await assertThrowsAsync( - async () => { - await readTrailers(h, new BufReader(new Buffer(encode(trailer)))); - }, - Deno.errors.InvalidData, - `Undeclared trailers: [ "` - ); +Deno.test( + "readTrailer should throw if undeclared headers found in trailer", + async () => { + const patterns = [ + ["deno,node", "deno: land\r\nnode: js\r\ngo: lang\r\n\r\n"], + ["deno", "node: js\r\n\r\n"], + ["deno", "node:js\r\ngo: lang\r\n\r\n"], + ]; + for (const [header, trailer] of patterns) { + const h = new Headers({ + trailer: header, + }); + await assertThrowsAsync( + async () => { + await readTrailers( + h, + new BufReader(new Deno.Buffer(encode(trailer))) + ); + }, + Deno.errors.InvalidData, + `Undeclared trailers: [ "` + ); + } } -}); - -test("readTrailer should throw if trailer contains prohibited fields", async () => { - for (const f of ["Content-Length", "Trailer", "Transfer-Encoding"]) { - const h = new Headers({ - trailer: f, - }); - await assertThrowsAsync( - async () => { - await readTrailers(h, new BufReader(new Buffer())); - }, - Deno.errors.InvalidData, - `Prohibited trailer names: [ "` - ); +); + +Deno.test( + "readTrailer should throw if trailer contains prohibited fields", + async () => { + for (const f of ["Content-Length", "Trailer", "Transfer-Encoding"]) { + const h = new Headers({ + trailer: f, + }); + await assertThrowsAsync( + async () => { + await readTrailers(h, new BufReader(new Deno.Buffer())); + }, + Deno.errors.InvalidData, + `Prohibited trailer names: [ "` + ); + } } -}); +); -test("writeTrailer", async () => { - const w = new Buffer(); +Deno.test("writeTrailer", async () => { + const w = new Deno.Buffer(); await writeTrailers( w, new Headers({ "transfer-encoding": "chunked", trailer: "deno,node" }), @@ -138,8 +149,8 @@ test("writeTrailer", async () => { ); }); -test("writeTrailer should throw", async () => { - const w = new Buffer(); +Deno.test("writeTrailer should throw", async () => { + const w = new Deno.Buffer(); await assertThrowsAsync( () => { return writeTrailers(w, new Headers(), new Headers()); @@ -181,7 +192,7 @@ test("writeTrailer should throw", async () => { }); // Ported from https://github.com/golang/go/blob/f5c43b9/src/net/http/request_test.go#L535-L565 -test("parseHttpVersion", (): void => { +Deno.test("parseHttpVersion", (): void => { const testCases = [ { in: "HTTP/0.9", want: [0, 9] }, { in: "HTTP/1.0", want: [1, 0] }, @@ -212,7 +223,7 @@ test("parseHttpVersion", (): void => { } }); -test("writeUint8ArrayResponse", async function (): Promise<void> { +Deno.test("writeUint8ArrayResponse", async function (): Promise<void> { const shortText = "Hello"; const body = new TextEncoder().encode(shortText); @@ -248,7 +259,7 @@ test("writeUint8ArrayResponse", async function (): Promise<void> { assertEquals(eof, null); }); -test("writeStringResponse", async function (): Promise<void> { +Deno.test("writeStringResponse", async function (): Promise<void> { const body = "Hello"; const res: Response = { body }; @@ -283,7 +294,7 @@ test("writeStringResponse", async function (): Promise<void> { assertEquals(eof, null); }); -test("writeStringReaderResponse", async function (): Promise<void> { +Deno.test("writeStringReaderResponse", async function (): Promise<void> { const shortText = "Hello"; const body = new StringReader(shortText); @@ -326,8 +337,8 @@ test("writeStringReaderResponse", async function (): Promise<void> { assertEquals(r.more, false); }); -test("writeResponse with trailer", async () => { - const w = new Buffer(); +Deno.test("writeResponse with trailer", async () => { + const w = new Deno.Buffer(); const body = new StringReader("Hello"); await writeResponse(w, { status: 200, @@ -356,18 +367,18 @@ test("writeResponse with trailer", async () => { assertEquals(ret, exp); }); -test("writeResponseShouldNotModifyOriginHeaders", async () => { +Deno.test("writeResponseShouldNotModifyOriginHeaders", async () => { const headers = new Headers(); const buf = new Deno.Buffer(); await writeResponse(buf, { body: "foo", headers }); - assert(decode(await readAll(buf)).includes("content-length: 3")); + assert(decode(await Deno.readAll(buf)).includes("content-length: 3")); await writeResponse(buf, { body: "hello", headers }); - assert(decode(await readAll(buf)).includes("content-length: 5")); + assert(decode(await Deno.readAll(buf)).includes("content-length: 5")); }); -test("readRequestError", async function (): Promise<void> { +Deno.test("readRequestError", async function (): Promise<void> { const input = `GET / HTTP/1.1 malformedHeader `; @@ -385,7 +396,7 @@ malformedHeader // Ported from Go // https://github.com/golang/go/blob/go1.12.5/src/net/http/request_test.go#L377-L443 // TODO(zekth) fix tests -test("testReadRequestError", async function (): Promise<void> { +Deno.test("testReadRequestError", async function (): Promise<void> { const testCases = [ { in: "GET / HTTP/1.1\r\nheader: foo\r\n\r\n", diff --git a/std/http/cookie_test.ts b/std/http/cookie_test.ts index e221b3363..0b412d8e4 100644 --- a/std/http/cookie_test.ts +++ b/std/http/cookie_test.ts @@ -2,9 +2,8 @@ import { ServerRequest, Response } from "./server.ts"; import { getCookies, deleteCookie, setCookie } from "./cookie.ts"; import { assert, assertEquals } from "../testing/asserts.ts"; -const { test } = Deno; -test({ +Deno.test({ name: "Cookie parser", fn(): void { const req = new ServerRequest(); @@ -32,7 +31,7 @@ test({ }, }); -test({ +Deno.test({ name: "Cookie Delete", fn(): void { const res: Response = {}; @@ -44,7 +43,7 @@ test({ }, }); -test({ +Deno.test({ name: "Cookie Set", fn(): void { const res: Response = {}; diff --git a/std/http/file_server.ts b/std/http/file_server.ts index d9ed56236..8a14e95be 100755 --- a/std/http/file_server.ts +++ b/std/http/file_server.ts @@ -6,7 +6,6 @@ // TODO Add tests like these: // https://github.com/indexzero/http-server/blob/master/test/http-server-test.js -const { args, stat, readDir, open, exit } = Deno; import { posix, extname } from "../path/mod.ts"; import { listenAndServe, ServerRequest, Response } from "./server.ts"; import { parse } from "../flags/mod.ts"; @@ -33,7 +32,7 @@ interface FileServerArgs { const encoder = new TextEncoder(); -const serverArgs = parse(args) as FileServerArgs; +const serverArgs = parse(Deno.args) as FileServerArgs; const target = posix.resolve(serverArgs._[0] ?? ""); const MEDIA_TYPES: Record<string, string> = { @@ -100,7 +99,10 @@ export async function serveFile( req: ServerRequest, filePath: string ): Promise<Response> { - const [file, fileInfo] = await Promise.all([open(filePath), stat(filePath)]); + const [file, fileInfo] = await Promise.all([ + Deno.open(filePath), + Deno.stat(filePath), + ]); const headers = new Headers(); headers.set("content-length", fileInfo.size.toString()); const contentTypeValue = contentType(filePath); @@ -124,7 +126,7 @@ async function serveDir( ): Promise<Response> { const dirUrl = `/${posix.relative(target, dirPath)}`; const listEntry: EntryInfo[] = []; - for await (const entry of readDir(dirPath)) { + for await (const entry of Deno.readDir(dirPath)) { const filePath = posix.join(dirPath, entry.name); const fileUrl = posix.join(dirUrl, entry.name); if (entry.name === "index.html" && entry.isFile) { @@ -134,7 +136,7 @@ async function serveDir( // Yuck! let fileInfo = null; try { - fileInfo = await stat(filePath); + fileInfo = await Deno.stat(filePath); } catch (e) { // Pass } @@ -307,18 +309,18 @@ function main(): void { if (serverArgs.h ?? serverArgs.help) { console.log(`Deno File Server Serves a local directory in HTTP. - + INSTALL: deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts - + USAGE: file_server [path] [options] - + OPTIONS: -h, --help Prints help information -p, --port <PORT> Set port --cors Enable CORS via the "Access-Control-Allow-Origin" header`); - exit(); + Deno.exit(); } listenAndServe( @@ -336,7 +338,7 @@ function main(): void { let response: Response | undefined; try { - const fileInfo = await stat(fsPath); + const fileInfo = await Deno.stat(fsPath); if (fileInfo.isDirectory) { response = await serveDir(req, fsPath); } else { diff --git a/std/http/file_server_test.ts b/std/http/file_server_test.ts index ceea566fa..66c1d7d04 100644 --- a/std/http/file_server_test.ts +++ b/std/http/file_server_test.ts @@ -4,7 +4,6 @@ import { BufReader } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { ServerRequest } from "./server.ts"; import { serveFile } from "./file_server.ts"; -const { test } = Deno; let fileServer: Deno.Process<Deno.RunOptions & { stdout: "piped" }>; type FileServerCfg = { @@ -68,42 +67,48 @@ async function killFileServer(): Promise<void> { fileServer.stdout!.close(); } -test("file_server serveFile in ./", async (): Promise<void> => { - await startFileServer(); - try { - const res = await fetch("http://localhost:4507/README.md"); - assert(res.headers.has("access-control-allow-origin")); - assert(res.headers.has("access-control-allow-headers")); - assertEquals(res.headers.get("content-type"), "text/markdown"); - const downloadedFile = await res.text(); - const localFile = new TextDecoder().decode( - await Deno.readFile("README.md") - ); - assertEquals(downloadedFile, localFile); - } finally { - await killFileServer(); +Deno.test( + "file_server serveFile in ./", + async (): Promise<void> => { + await startFileServer(); + try { + const res = await fetch("http://localhost:4507/README.md"); + assert(res.headers.has("access-control-allow-origin")); + assert(res.headers.has("access-control-allow-headers")); + assertEquals(res.headers.get("content-type"), "text/markdown"); + const downloadedFile = await res.text(); + const localFile = new TextDecoder().decode( + await Deno.readFile("README.md") + ); + assertEquals(downloadedFile, localFile); + } finally { + await killFileServer(); + } } -}); +); -test("file_server serveFile in ./http", async (): Promise<void> => { - await startFileServer({ target: "./http" }); - try { - const res = await fetch("http://localhost:4507/README.md"); - assert(res.headers.has("access-control-allow-origin")); - assert(res.headers.has("access-control-allow-headers")); - assertEquals(res.headers.get("content-type"), "text/markdown"); - const downloadedFile = await res.text(); - const localFile = new TextDecoder().decode( - await Deno.readFile("./http/README.md") - ); - console.log(downloadedFile, localFile); - assertEquals(downloadedFile, localFile); - } finally { - await killFileServer(); +Deno.test( + "file_server serveFile in ./http", + async (): Promise<void> => { + await startFileServer({ target: "./http" }); + try { + const res = await fetch("http://localhost:4507/README.md"); + assert(res.headers.has("access-control-allow-origin")); + assert(res.headers.has("access-control-allow-headers")); + assertEquals(res.headers.get("content-type"), "text/markdown"); + const downloadedFile = await res.text(); + const localFile = new TextDecoder().decode( + await Deno.readFile("./http/README.md") + ); + console.log(downloadedFile, localFile); + assertEquals(downloadedFile, localFile); + } finally { + await killFileServer(); + } } -}); +); -test("serveDirectory", async function (): Promise<void> { +Deno.test("serveDirectory", async function (): Promise<void> { await startFileServer(); try { const res = await fetch("http://localhost:4507/"); @@ -125,7 +130,7 @@ test("serveDirectory", async function (): Promise<void> { } }); -test("serveFallback", async function (): Promise<void> { +Deno.test("serveFallback", async function (): Promise<void> { await startFileServer(); try { const res = await fetch("http://localhost:4507/badfile.txt"); @@ -138,7 +143,7 @@ test("serveFallback", async function (): Promise<void> { } }); -test("serveWithUnorthodoxFilename", async function (): Promise<void> { +Deno.test("serveWithUnorthodoxFilename", async function (): Promise<void> { await startFileServer(); try { let res = await fetch("http://localhost:4507/http/testdata/%"); @@ -156,7 +161,7 @@ test("serveWithUnorthodoxFilename", async function (): Promise<void> { } }); -test("printHelp", async function (): Promise<void> { +Deno.test("printHelp", async function (): Promise<void> { const helpProcess = Deno.run({ cmd: [ Deno.execPath(), @@ -177,7 +182,7 @@ test("printHelp", async function (): Promise<void> { helpProcess.stdout.close(); }); -test("contentType", async () => { +Deno.test("contentType", async () => { const request = new ServerRequest(); const response = await serveFile(request, "http/testdata/hello.html"); const contentType = response.headers!.get("content-type"); @@ -185,7 +190,7 @@ test("contentType", async () => { (response.body as Deno.File).close(); }); -test("file_server running as library", async function (): Promise<void> { +Deno.test("file_server running as library", async function (): Promise<void> { await startFileServerAsLibrary(); try { const res = await fetch("http://localhost:8000"); diff --git a/std/http/racing_server_test.ts b/std/http/racing_server_test.ts index 054dfc385..7e510f13d 100644 --- a/std/http/racing_server_test.ts +++ b/std/http/racing_server_test.ts @@ -1,11 +1,10 @@ import { assert, assertEquals } from "../testing/asserts.ts"; import { BufReader, BufWriter } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; -const { connect, run, test } = Deno; let server: Deno.Process<Deno.RunOptions & { stdout: "piped" }>; async function startServer(): Promise<void> { - server = run({ + server = Deno.run({ // TODO(lucacasonato): remove unstable when stabilized cmd: [Deno.execPath(), "run", "--unstable", "-A", "http/racing_server.ts"], stdout: "piped", @@ -59,10 +58,10 @@ content-length: 6 Step7 `; -test("serverPipelineRace", async function (): Promise<void> { +Deno.test("serverPipelineRace", async function (): Promise<void> { await startServer(); - const conn = await connect({ port: 4501 }); + const conn = await Deno.connect({ port: 4501 }); const r = new TextProtoReader(new BufReader(conn)); const w = new BufWriter(conn); await w.write(new TextEncoder().encode(input)); diff --git a/std/http/server.ts b/std/http/server.ts index d2736cb43..effa7b4b9 100644 --- a/std/http/server.ts +++ b/std/http/server.ts @@ -10,10 +10,6 @@ import { writeResponse, readRequest, } from "./_io.ts"; -import Listener = Deno.Listener; -import Conn = Deno.Conn; -import Reader = Deno.Reader; -const { listen, listenTls } = Deno; export class ServerRequest { url!: string; @@ -22,7 +18,7 @@ export class ServerRequest { protoMinor!: number; protoMajor!: number; headers!: Headers; - conn!: Conn; + conn!: Deno.Conn; r!: BufReader; w!: BufWriter; done: Deferred<Error | undefined> = deferred(); @@ -119,9 +115,9 @@ export class ServerRequest { export class Server implements AsyncIterable<ServerRequest> { private closing = false; - private connections: Conn[] = []; + private connections: Deno.Conn[] = []; - constructor(public listener: Listener) {} + constructor(public listener: Deno.Listener) {} close(): void { this.closing = true; @@ -140,7 +136,7 @@ export class Server implements AsyncIterable<ServerRequest> { // Yields all HTTP requests on a single TCP connection. private async *iterateHttpRequests( - conn: Conn + conn: Deno.Conn ): AsyncIterableIterator<ServerRequest> { const reader = new BufReader(conn); const writer = new BufWriter(conn); @@ -191,11 +187,11 @@ export class Server implements AsyncIterable<ServerRequest> { } } - private trackConnection(conn: Conn): void { + private trackConnection(conn: Deno.Conn): void { this.connections.push(conn); } - private untrackConnection(conn: Conn): void { + private untrackConnection(conn: Deno.Conn): void { const index = this.connections.indexOf(conn); if (index !== -1) { this.connections.splice(index, 1); @@ -211,7 +207,7 @@ export class Server implements AsyncIterable<ServerRequest> { ): AsyncIterableIterator<ServerRequest> { if (this.closing) return; // Wait for a new connection. - let conn: Conn; + let conn: Deno.Conn; try { conn = await this.listener.accept(); } catch (error) { @@ -257,7 +253,7 @@ export function serve(addr: string | HTTPOptions): Server { addr = { hostname, port: Number(port) }; } - const listener = listen(addr); + const listener = Deno.listen(addr); return new Server(listener); } @@ -309,7 +305,7 @@ export function serveTLS(options: HTTPSOptions): Server { ...options, transport: "tcp", }; - const listener = listenTls(tlsOptions); + const listener = Deno.listenTls(tlsOptions); return new Server(listener); } @@ -349,6 +345,6 @@ export async function listenAndServeTLS( export interface Response { status?: number; headers?: Headers; - body?: Uint8Array | Reader | string; + body?: Uint8Array | Deno.Reader | string; trailers?: () => Promise<Headers> | Headers; } diff --git a/std/http/server_test.ts b/std/http/server_test.ts index 2d911c450..340d9fa73 100644 --- a/std/http/server_test.ts +++ b/std/http/server_test.ts @@ -19,8 +19,6 @@ import { delay } from "../async/delay.ts"; import { encode, decode } from "../encoding/utf8.ts"; import { mockConn } from "./_mock_conn.ts"; -const { Buffer, test } = Deno; - interface ResponseTest { response: Response; raw: string; @@ -43,7 +41,7 @@ const responseTests: ResponseTest[] = [ { response: { status: 200, - body: new Buffer(new TextEncoder().encode("abcdef")), + body: new Deno.Buffer(new TextEncoder().encode("abcdef")), }, raw: @@ -53,9 +51,9 @@ const responseTests: ResponseTest[] = [ }, ]; -test("responseWrite", async function (): Promise<void> { +Deno.test("responseWrite", async function (): Promise<void> { for (const testCase of responseTests) { - const buf = new Buffer(); + const buf = new Deno.Buffer(); const bufw = new BufWriter(buf); const request = new ServerRequest(); request.w = bufw; @@ -68,13 +66,13 @@ test("responseWrite", async function (): Promise<void> { } }); -test("requestContentLength", function (): void { +Deno.test("requestContentLength", function (): void { // Has content length { const req = new ServerRequest(); req.headers = new Headers(); req.headers.set("content-length", "5"); - const buf = new Buffer(encode("Hello")); + const buf = new Deno.Buffer(encode("Hello")); req.r = new BufReader(buf); assertEquals(req.contentLength, 5); } @@ -96,7 +94,7 @@ test("requestContentLength", function (): void { chunkOffset += chunkSize; } chunksData += "0\r\n\r\n"; - const buf = new Buffer(encode(chunksData)); + const buf = new Deno.Buffer(encode(chunksData)); req.r = new BufReader(buf); assertEquals(req.contentLength, null); } @@ -121,12 +119,12 @@ function totalReader(r: Deno.Reader): TotalReader { }, }; } -test("requestBodyWithContentLength", async function (): Promise<void> { +Deno.test("requestBodyWithContentLength", async function (): Promise<void> { { const req = new ServerRequest(); req.headers = new Headers(); req.headers.set("content-length", "5"); - const buf = new Buffer(encode("Hello")); + const buf = new Deno.Buffer(encode("Hello")); req.r = new BufReader(buf); const body = decode(await Deno.readAll(req.body)); assertEquals(body, "Hello"); @@ -138,59 +136,65 @@ test("requestBodyWithContentLength", async function (): Promise<void> { const req = new ServerRequest(); req.headers = new Headers(); req.headers.set("Content-Length", "5000"); - const buf = new Buffer(encode(longText)); + const buf = new Deno.Buffer(encode(longText)); req.r = new BufReader(buf); const body = decode(await Deno.readAll(req.body)); assertEquals(body, longText); } // Handler ignored to consume body }); -test("ServerRequest.finalize() should consume unread body / content-length", async () => { - const text = "deno.land"; - const req = new ServerRequest(); - req.headers = new Headers(); - req.headers.set("content-length", "" + text.length); - const tr = totalReader(new Buffer(encode(text))); - req.r = new BufReader(tr); - req.w = new BufWriter(new Buffer()); - await req.respond({ status: 200, body: "ok" }); - assertEquals(tr.total, 0); - await req.finalize(); - assertEquals(tr.total, text.length); -}); -test("ServerRequest.finalize() should consume unread body / chunked, trailers", async () => { - const text = [ - "5", - "Hello", - "4", - "Deno", - "0", - "", - "deno: land", - "node: js", - "", - "", - ].join("\r\n"); - const req = new ServerRequest(); - req.headers = new Headers(); - req.headers.set("transfer-encoding", "chunked"); - req.headers.set("trailer", "deno,node"); - const body = encode(text); - const tr = totalReader(new Buffer(body)); - req.r = new BufReader(tr); - req.w = new BufWriter(new Buffer()); - await req.respond({ status: 200, body: "ok" }); - assertEquals(tr.total, 0); - assertEquals(req.headers.has("trailer"), true); - assertEquals(req.headers.has("deno"), false); - assertEquals(req.headers.has("node"), false); - await req.finalize(); - assertEquals(tr.total, body.byteLength); - assertEquals(req.headers.has("trailer"), false); - assertEquals(req.headers.get("deno"), "land"); - assertEquals(req.headers.get("node"), "js"); -}); -test("requestBodyWithTransferEncoding", async function (): Promise<void> { +Deno.test( + "ServerRequest.finalize() should consume unread body / content-length", + async () => { + const text = "deno.land"; + const req = new ServerRequest(); + req.headers = new Headers(); + req.headers.set("content-length", "" + text.length); + const tr = totalReader(new Deno.Buffer(encode(text))); + req.r = new BufReader(tr); + req.w = new BufWriter(new Deno.Buffer()); + await req.respond({ status: 200, body: "ok" }); + assertEquals(tr.total, 0); + await req.finalize(); + assertEquals(tr.total, text.length); + } +); +Deno.test( + "ServerRequest.finalize() should consume unread body / chunked, trailers", + async () => { + const text = [ + "5", + "Hello", + "4", + "Deno", + "0", + "", + "deno: land", + "node: js", + "", + "", + ].join("\r\n"); + const req = new ServerRequest(); + req.headers = new Headers(); + req.headers.set("transfer-encoding", "chunked"); + req.headers.set("trailer", "deno,node"); + const body = encode(text); + const tr = totalReader(new Deno.Buffer(body)); + req.r = new BufReader(tr); + req.w = new BufWriter(new Deno.Buffer()); + await req.respond({ status: 200, body: "ok" }); + assertEquals(tr.total, 0); + assertEquals(req.headers.has("trailer"), true); + assertEquals(req.headers.has("deno"), false); + assertEquals(req.headers.has("node"), false); + await req.finalize(); + assertEquals(tr.total, body.byteLength); + assertEquals(req.headers.has("trailer"), false); + assertEquals(req.headers.get("deno"), "land"); + assertEquals(req.headers.get("node"), "js"); + } +); +Deno.test("requestBodyWithTransferEncoding", async function (): Promise<void> { { const shortText = "Hello"; const req = new ServerRequest(); @@ -208,7 +212,7 @@ test("requestBodyWithTransferEncoding", async function (): Promise<void> { chunkOffset += chunkSize; } chunksData += "0\r\n\r\n"; - const buf = new Buffer(encode(chunksData)); + const buf = new Deno.Buffer(encode(chunksData)); req.r = new BufReader(buf); const body = decode(await Deno.readAll(req.body)); assertEquals(body, shortText); @@ -232,20 +236,22 @@ test("requestBodyWithTransferEncoding", async function (): Promise<void> { chunkOffset += chunkSize; } chunksData += "0\r\n\r\n"; - const buf = new Buffer(encode(chunksData)); + const buf = new Deno.Buffer(encode(chunksData)); req.r = new BufReader(buf); const body = decode(await Deno.readAll(req.body)); assertEquals(body, longText); } }); -test("requestBodyReaderWithContentLength", async function (): Promise<void> { +Deno.test("requestBodyReaderWithContentLength", async function (): Promise< + void +> { { const shortText = "Hello"; const req = new ServerRequest(); req.headers = new Headers(); req.headers.set("content-length", "" + shortText.length); - const buf = new Buffer(encode(shortText)); + const buf = new Deno.Buffer(encode(shortText)); req.r = new BufReader(buf); const readBuf = new Uint8Array(6); let offset = 0; @@ -266,7 +272,7 @@ test("requestBodyReaderWithContentLength", async function (): Promise<void> { const req = new ServerRequest(); req.headers = new Headers(); req.headers.set("Content-Length", "5000"); - const buf = new Buffer(encode(longText)); + const buf = new Deno.Buffer(encode(longText)); req.r = new BufReader(buf); const readBuf = new Uint8Array(1000); let offset = 0; @@ -282,7 +288,9 @@ test("requestBodyReaderWithContentLength", async function (): Promise<void> { } }); -test("requestBodyReaderWithTransferEncoding", async function (): Promise<void> { +Deno.test("requestBodyReaderWithTransferEncoding", async function (): Promise< + void +> { { const shortText = "Hello"; const req = new ServerRequest(); @@ -300,7 +308,7 @@ test("requestBodyReaderWithTransferEncoding", async function (): Promise<void> { chunkOffset += chunkSize; } chunksData += "0\r\n\r\n"; - const buf = new Buffer(encode(chunksData)); + const buf = new Deno.Buffer(encode(chunksData)); req.r = new BufReader(buf); const readBuf = new Uint8Array(6); let offset = 0; @@ -333,7 +341,7 @@ test("requestBodyReaderWithTransferEncoding", async function (): Promise<void> { chunkOffset += chunkSize; } chunksData += "0\r\n\r\n"; - const buf = new Buffer(encode(chunksData)); + const buf = new Deno.Buffer(encode(chunksData)); req.r = new BufReader(buf); const readBuf = new Uint8Array(1000); let offset = 0; @@ -349,7 +357,7 @@ test("requestBodyReaderWithTransferEncoding", async function (): Promise<void> { } }); -test({ +Deno.test({ name: "destroyed connection", fn: async (): Promise<void> => { // Runs a simple server as another process @@ -393,7 +401,7 @@ test({ }, }); -test({ +Deno.test({ name: "serveTLS", fn: async (): Promise<void> => { // Runs a simple server as another process @@ -450,17 +458,20 @@ test({ }, }); -test("close server while iterating", async (): Promise<void> => { - const server = serve(":8123"); - const nextWhileClosing = server[Symbol.asyncIterator]().next(); - server.close(); - assertEquals(await nextWhileClosing, { value: undefined, done: true }); +Deno.test( + "close server while iterating", + async (): Promise<void> => { + const server = serve(":8123"); + const nextWhileClosing = server[Symbol.asyncIterator]().next(); + server.close(); + assertEquals(await nextWhileClosing, { value: undefined, done: true }); - const nextAfterClosing = server[Symbol.asyncIterator]().next(); - assertEquals(await nextAfterClosing, { value: undefined, done: true }); -}); + const nextAfterClosing = server[Symbol.asyncIterator]().next(); + assertEquals(await nextAfterClosing, { value: undefined, done: true }); + } +); -test({ +Deno.test({ name: "[http] close server while connection is open", async fn(): Promise<void> { async function iteratorReq(server: Server): Promise<void> { @@ -491,7 +502,7 @@ test({ }, }); -test({ +Deno.test({ name: "respond error closes connection", async fn(): Promise<void> { const serverRoutine = async (): Promise<void> => { @@ -522,7 +533,7 @@ test({ }, }); -test({ +Deno.test({ name: "[http] request error gets 400 response", async fn(): Promise<void> { const server = serve(":8124"); @@ -546,7 +557,7 @@ test({ }, }); -test({ +Deno.test({ name: "serveTLS Invalid Cert", fn: async (): Promise<void> => { async function iteratorReq(server: Server): Promise<void> { |