-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
Hello World! | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"hello": "world" | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |