deno.land / x / opine@2.3.4 / test / units / res.send.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547// deno-lint-ignore-file no-explicit-anyimport { opine } from "../../mod.ts";import { describe, it, shouldHaveBody, shouldNotHaveBody, shouldNotHaveHeader,} from "../utils.ts";import { expect, superdeno } from "../deps.ts";import { methods } from "../../src/methods.ts";import type { OpineRequest, OpineResponse } from "../../src/types.ts";
describe("res", function () { describe(".send()", function () { it('should set body to ""', function (done) { const app = opine();
app.use(function (_req, res) { res.send(); });
superdeno(app) .get("/") .expect(200, "", done); }); });
describe(".send(null)", function () { it('should set body to ""', function (done) { const app = opine();
app.use(function (_req, res) { res.send(null); });
superdeno(app) .get("/") .expect("Content-Length", "0") .expect(200, "", done); }); });
describe(".send(undefined)", function () { it('should set body to ""', function (done) { const app = opine();
app.use(function (_req, res) { res.send(undefined); });
superdeno(app) .get("/") .expect(200, "", done); }); });
describe(".send(String)", function () { it("should send as html", function (done) { const app = opine();
app.use(function (_req, res) { res.send("<p>hey</p>"); });
superdeno(app) .get("/") .expect("Content-Type", "text/html; charset=utf-8") .expect(200, "<p>hey</p>", done); });
it("should set ETag", function (done) { const app = opine();
app.use(function (_req, res) { const str = Array(1000).join("-"); res.send(str); });
superdeno(app) .get("/") .expect("ETag", 'W/"3e7-a8f9e4277095755845250bd405f"') .expect(200, done); });
it("should not override Content-Type", function (done) { const app = opine();
app.use(function (_req, res) { res.set("Content-Type", "text/plain").send("hey"); });
superdeno(app) .get("/") .expect("Content-Type", "text/plain; charset=utf-8") .expect(200, "hey", done); });
it("should not override charset in Content-Type", function (done) { const app = opine();
app.use(function (_req, res) { res.set("Content-Type", "text/plain; charset=iso-8859-1").send("hey"); });
superdeno(app) .get("/") .expect("Content-Type", "text/plain; charset=iso-8859-1") .expect(200, "hey", done); });
it("should keep charset in Content-Type for Buffers", function (done) { const app = opine();
app.use(function (_req, res) { res.set("Content-Type", "text/plain; charset=iso-8859-1").send( (new TextEncoder()).encode("hi"), ); });
superdeno(app) .get("/") .expect("Content-Type", "text/plain; charset=iso-8859-1") .expect(200, "hi", done); }); });
describe(".send(Buffer)", function () { it("should send as octet-stream", function (done) { const app = opine();
app.use(function (_req, res) { res.send((new TextEncoder()).encode("hello")); });
superdeno(app) .get("/") .expect(200) .expect("Content-Type", "application/octet-stream") .expect(shouldHaveBody("hello")) .end(done); });
it("should set ETag", function (done) { const app = opine();
app.use(function (_req, res) { res.send((new TextEncoder()).encode("hello")); });
superdeno(app) .get("/") .expect("ETag", 'W/"5-aaf4c61ddcc5e8a2dabede0f3b4"') .expect(200, done); });
it("should not override Content-Type", function (done) { const app = opine();
app.use(function (_req, res) { res.set("Content-Type", "text/plain").send( (new TextEncoder()).encode("hey"), ); });
superdeno(app) .get("/") .expect("Content-Type", "text/plain; charset=utf-8") .expect(200, "hey", done); });
it("should not override ETag", function (done) { const app = opine();
app.use(function (_req, res) { res.type("text/plain").set("ETag", '"foo"').send( (new TextEncoder()).encode("hey"), ); });
superdeno(app) .get("/") .expect("ETag", '"foo"') .expect(200, "hey", done); }); });
describe(".send(Object)", function () { it("should send as application/json", function (done) { const app = opine();
app.use(function (_req, res) { res.send({ name: "deno" }); });
superdeno(app) .get("/") .expect("Content-Type", "application/json; charset=utf-8") .expect(200, '{"name":"deno"}', done); }); });
describe("when the request method is HEAD", function () { it("should ignore the body", function (done) { const app = opine();
app.use(function (_req, res) { res.send("yay"); });
superdeno(app) .head("/") .expect(200) .expect(shouldNotHaveBody()) .end(done); }); });
describe("when .statusCode is 204", function () { it( "should strip Content-* fields, Transfer-Encoding field, and body", function ( done, ) { const app = opine();
app.use(function (_req, res) { res.setStatus(204).set("Transfer-Encoding", "chunked").send("foo"); });
superdeno(app) .get("/") .expect(shouldNotHaveHeader("Content-Type")) .expect(shouldNotHaveHeader("Transfer-Encoding")) .expect(204, null, done); }, ); });
describe("when .statusCode is 304", function () { it( "should strip Content-* fields, Transfer-Encoding field, and body", function ( done, ) { const app = opine();
app.use(function (_req, res) { res.setStatus(304).set("Transfer-Encoding", "chunked").send("foo"); });
superdeno(app) .get("/") .expect(shouldNotHaveHeader("Content-Type")) .expect(shouldNotHaveHeader("Transfer-Encoding")) .expect(304, null, done); }, ); });
it("should always check regardless of length", function (done) { const app = opine(); const etag = '"asdf"';
app.use(function (_req, res) { res.set("ETag", etag); res.send("hey"); });
superdeno(app) .get("/") .set("If-None-Match", etag) .expect(304, done); });
it("should respond with 304 Not Modified when fresh", function (done) { const app = opine(); const etag = '"asdf"';
app.use(function (_req, res) { const str = Array(1000).join("-"); res.set("ETag", etag); res.send(str); });
superdeno(app) .get("/") .set("If-None-Match", etag) .expect(304, done); });
it("should not perform freshness check unless 2xx or 304", function (done) { const app = opine(); const etag = '"asdf"';
app.use(function (_req, res) { res.setStatus(500); res.set("ETag", etag); res.send("hey"); });
superdeno(app) .get("/") .set("If-None-Match", etag) .expect("hey") .expect(500, done); });
it("should not support jsonp callbacks", function (done) { const app = opine();
app.use(function (_req, res) { res.send({ foo: "bar" }); });
superdeno(app) .get("/?callback=foo") .expect('{"foo":"bar"}', done); });
it("should be chainable", function (done) { const app = opine();
app.use(function (_req, res) { expect(res.send("hey")).toEqual(res); });
superdeno(app) .get("/") .expect(200, "hey", done); });
describe('"etag" setting', function () { describe("when enabled", function () { it("should send ETag", function (done) { const app = opine();
app.use(function (_req, res) { res.send("kajdslfkasdf"); });
app.enable("etag");
superdeno(app) .get("/") .expect("ETag", 'W/"c-22047f2f9485ec22507dfe30c4a"') .expect(200, done); });
methods.forEach(function (method) { if (method === "connect") return;
it( "should send ETag in response to " + method.toUpperCase() + " request", function (done) { const app = opine();
(app as any)[method]( "/", function (_req: OpineRequest, res: OpineResponse) { res.send("kajdslfkasdf"); }, );
(superdeno(app) as any)[method]("/") .expect("ETag", 'W/"c-22047f2f9485ec22507dfe30c4a"') .expect(200, done); }, ); });
it("should send ETag for empty string response", function (done) { const app = opine();
app.use(function (_req, res) { res.send(""); });
app.enable("etag");
superdeno(app) .get("/") .expect("ETag", 'W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"') .expect(200, done); });
it("should send ETag for long response", function (done) { const app = opine();
app.use(function (_req, res) { const str = Array(1000).join("-"); res.send(str); });
app.enable("etag");
superdeno(app) .get("/") .expect("ETag", 'W/"3e7-a8f9e4277095755845250bd405f"') .expect(200, done); });
it("should not override ETag when manually set", function (done) { const app = opine();
app.use(function (_req, res) { res.set("etag", '"asdf"'); res.send(200); });
app.enable("etag");
superdeno(app) .get("/") .expect("ETag", '"asdf"') .expect(200, done); });
it("should not send ETag for res.send()", function (done) { const app = opine();
app.use(function (_req, res) { res.send(); });
app.enable("etag");
superdeno(app) .get("/") .expect(shouldNotHaveHeader("ETag")) .expect(200, done); }); });
describe("when disabled", function () { it("should send no ETag", function (done) { const app = opine();
app.use(function (_req, res) { const str = Array(1000).join("-"); res.send(str); });
app.disable("etag");
superdeno(app) .get("/") .expect(shouldNotHaveHeader("ETag")) .expect(200, done); });
it("should send ETag when manually set", function (done) { const app = opine();
app.disable("etag");
app.use(function (_req, res) { res.set("etag", '"asdf"'); res.send(200); });
superdeno(app) .get("/") .expect("ETag", '"asdf"') .expect(200, done); }); });
describe('when "strong"', function () { it("should send strong ETag", function (done) { const app = opine();
app.set("etag", "strong");
app.use(function (_req, res) { res.send("hello, world!"); });
superdeno(app) .get("/") .expect("ETag", '"d-1f09d30c707d53f3d16c530dd73"') .expect(200, done); }); });
describe('when "weak"', function () { it("should send weak ETag", function (done) { const app = opine();
app.set("etag", "weak");
app.use(function (_req, res) { res.send("hello, world!"); });
superdeno(app) .get("/") .expect("ETag", 'W/"d-1f09d30c707d53f3d16c530dd73"') .expect(200, done); }); });
describe("when a function", function () { it("should send custom ETag", function (done) { const app = opine();
app.set("etag", function (body: any) { const chunk = body instanceof Uint8Array ? (new TextDecoder()).decode(body) : body;
expect(chunk.toString()).toEqual("hello, world!");
return '"custom"'; });
app.use(function (_req, res) { res.send("hello, world!"); });
superdeno(app) .get("/") .expect("ETag", '"custom"') .expect(200, done); });
it("should not send falsy ETag", function (done) { const app = opine();
app.set("etag", function (_body: any) { return undefined; });
app.use(function (_req, res) { res.send("hello, world!"); });
superdeno(app) .get("/") .expect(shouldNotHaveHeader("ETag")) .expect(200, done); }); }); });});
Version Info