deno.land / x / opine@2.3.4 / test / units / req.accepts.test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import { opine } from "../../mod.ts";import { superdeno } from "../deps.ts";import { describe, it } from "../utils.ts";
describe("req", function () { describe(".accepts(type)", function () { it("should return true when Accept is not present", function (done) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts("json") ? "yes" : "no"); });
superdeno(app) .get("/") .expect("yes", done); });
it("should return true when present", function (done) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts("json") ? "yes" : "no"); });
superdeno(app) .get("/") .set("Accept", "application/json") .expect("yes", done); });
it("should return false otherwise", function (done) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts("json") ? "yes" : "no"); });
superdeno(app) .get("/") .set("Accept", "text/html") .expect("no", done); }); });
it("should accept an argument list of type names", function (done) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts("json", "html") as string); });
superdeno(app) .get("/") .set("Accept", "application/json") .expect("json", done); });
describe(".accepts(types)", function () { it("should return the first when Accept is not present", function (done) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts(["json", "html"]) as string); });
superdeno(app) .get("/") .expect("json", done); });
it("should return the first acceptable type", function (done) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts(["json", "html"]) as string); });
superdeno(app) .get("/") .set("Accept", "text/html") .expect("html", done); });
it("should return false when no match is made", function (done) { const app = opine();
app.use(function (req, res, next) { res.end( req.accepts(["text/html", "application/json"]) ? "yup" : "nope", ); });
superdeno(app) .get("/") .set("Accept", "foo/bar, bar/baz") .expect("nope", done); });
it("should take quality into account", function (done) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts(["text/html", "application/json"]) as string); });
superdeno(app) .get("/") .set("Accept", "*/html; q=.5, application/json") .expect("application/json", done); });
it( "should return the first acceptable type with canonical mime types", function ( done, ) { const app = opine();
app.use(function (req, res, next) { res.end(req.accepts(["application/json", "text/html"]) as string); });
superdeno(app) .get("/") .set("Accept", "*/html") .expect("text/html", done); }, ); });});
Version Info