Skip to content

Commit

Permalink
Test for send
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jan 20, 2019
1 parent a41727c commit 6ac00f8
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 3 deletions.
6 changes: 6 additions & 0 deletions fixtures/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<html>
<body>
Hello World!
</body>
</html>
3 changes: 3 additions & 0 deletions fixtures/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"hello": "world"
}
Binary file added fixtures/test.json.br
Binary file not shown.
Binary file added fixtures/test.json.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function send(
if (!hidden && isHidden(root, path)) {
return;
}
Headers;

let encodingExt = "";
if (
brotli &&
Expand Down
135 changes: 133 additions & 2 deletions send_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,138 @@
import { assert, test } from "https://deno.land/x/std/testing/mod.ts";

import { Application } from "./application.ts";
import { Context } from "./context.ts";
import * as deno from "deno";
import { Status } from "./deps.ts";
import httpErrors from "./httpError.ts";
import { Request } from "./request.ts";
import { Response } from "./response.ts";
import { send } from "./send.ts";

test(function basicSend() {
assert(send != null);
let encodingsAccepted = "identity";

function createMockApp<S extends object = { [key: string]: any }>(
state = {} as S
): Application<S> {
return {
state
} as any;
}

function createMockContext<S extends object = { [key: string]: any }>(
app: Application<S>,
path = "/",
method = "GET"
) {
return {
app,
request: {
acceptsEncodings() {
return encodingsAccepted;
},
headers: new Headers(),
method,
path,
search: undefined,
searchParams: new URLSearchParams(),
url: path
} as Request,
response: {
status: Status.OK,
body: undefined,
headers: new Headers()
} as Response,
state: app.state
} as Context<S>;
}

function setup<S extends object = { [key: string]: any }>(
path = "/",
method = "GET"
): {
app: Application<S>;
context: Context<S>;
} {
encodingsAccepted = "identity";
const app = createMockApp<S>();
const context = createMockContext<S>(app, path, method);
return { app, context };
}

test(async function sendHtml() {
const { context } = setup("/test.html");
const fixture = await deno.readFile("./fixtures/test.html");
await send(context, context.request.path, {
root: "./fixtures"
});
assert.equal(context.response.body, fixture);
assert.equal(context.response.type, ".html");
assert.equal(
context.response.headers.get("content-length"),
String(fixture.length)
);
assert(context.response.headers.get("last-modified") != null);
assert.equal(context.response.headers.get("cache-control"), "max-age=0");
});

test(async function sendGzip() {
const { context } = setup("/test.json");
const fixture = await deno.readFile("./fixtures/test.json.gz");
encodingsAccepted = "gzip";
await send(context, context.request.path, {
root: "./fixtures"
});
assert.equal(context.response.body, fixture);
assert.equal(context.response.type, ".json");
assert.equal(context.response.headers.get("content-encoding"), "gzip");
assert.equal(
context.response.headers.get("content-length"),
String(fixture.length)
);
});

test(async function sendBrotli() {
const { context } = setup("/test.json");
const fixture = await deno.readFile("./fixtures/test.json.br");
encodingsAccepted = "br";
await send(context, context.request.path, {
root: "./fixtures"
});
assert.equal(context.response.body, fixture);
assert.equal(context.response.type, ".json");
assert.equal(context.response.headers.get("content-encoding"), "br");
assert.equal(
context.response.headers.get("content-length"),
String(fixture.length)
);
});

test(async function sendIdentity() {
const { context } = setup("/test.json");
const fixture = await deno.readFile("./fixtures/test.json");
await send(context, context.request.path, {
root: "./fixtures"
});
assert.equal(context.response.body, fixture);
assert.equal(context.response.type, ".json");
assert.strictEqual(context.response.headers.get("content-encoding"), null);
assert.equal(
context.response.headers.get("content-length"),
String(fixture.length)
);
});

test(async function send404() {
const { context } = setup("/foo.txt");
encodingsAccepted = "identity";
let didThrow = false;
try {
await send(context, context.request.path, {
root: "./fixtures"
});
} catch (e) {
assert(e instanceof httpErrors.NotFound);
didThrow = true;
}
assert(didThrow);
});

0 comments on commit 6ac00f8

Please sign in to comment.