deno.land / std@0.224.0 / http / negotiation_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../assert/mod.ts";import { accepts, acceptsEncodings, acceptsLanguages } from "./negotiation.ts";
Deno.test({ name: "accepts() handles no args", fn() { const req = new Request("https://example.com/", { headers: { "accept": "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8", }, }); assertEquals(accepts(req), [ "text/html", "application/xhtml+xml", "image/webp", "application/xml", "*/*", ]); },});
Deno.test({ name: "accepts() handles args", fn() { const req = new Request("https://example.com/", { headers: { "accept": "text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8", }, }); assertEquals(accepts(req, "text/html", "image/webp"), "text/html"); },});
Deno.test({ name: "accepts() handles no match", fn() { const req = new Request("https://example.com/", { headers: { "accept": "text/html, application/xhtml+xml, application/xml", }, }); assertEquals(accepts(req, "application/json"), undefined); },});
Deno.test({ name: "accepts() handles args and no header", fn() { const req = new Request("https://example.com/"); assertEquals(accepts(req, "text/html", "image/webp"), "text/html"); },});
Deno.test({ name: "accepts() handles no args and no header", fn() { const req = new Request("https://example.com/"); assertEquals(accepts(req), ["*/*"]); },});
Deno.test({ name: "acceptsEncodings() handles no args", fn() { const req = new Request("https://example.com/", { headers: { "accept-encoding": "deflate, gzip;q=1.0, *;q=0.5" }, }); assertEquals(acceptsEncodings(req), ["deflate", "gzip", "*"]); },});
Deno.test({ name: "acceptsEncodings() handles args", fn() { const req = new Request("https://example.com/", { headers: { "accept-encoding": "deflate, gzip;q=1.0, *;q=0.5" }, }); assertEquals(acceptsEncodings(req, "gzip", "identity"), "gzip"); },});
Deno.test({ name: "acceptsEncodings() handles no match", fn() { const req = new Request("https://example.com/", { headers: { "accept-encoding": "deflate, gzip" }, }); assertEquals(acceptsEncodings(req, "brotli"), undefined); },});
Deno.test({ name: "acceptsEncodings() handles args and no header", fn() { const req = new Request("https://example.com/"); assertEquals(acceptsEncodings(req, "gzip", "identity"), "gzip"); },});
Deno.test({ name: "acceptsEncodings() handles no args and no header", fn() { const req = new Request("https://example.com/"); assertEquals(acceptsEncodings(req), ["*"]); },});
Deno.test({ name: "acceptsLanguages() handles no args", fn() { const req = new Request("https://example.com/", { headers: { "accept-language": "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5", }, }); assertEquals(acceptsLanguages(req), ["fr-CH", "fr", "en", "de", "*"]); },});
Deno.test({ name: "acceptsLanguages() handles args", fn() { const req = new Request("https://example.com/", { headers: { "accept-language": "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5", }, }); assertEquals(acceptsLanguages(req, "en-gb", "en-us", "en"), "en"); },});
Deno.test({ name: "acceptsLanguages() handles no match", fn() { const req = new Request("https://example.com/", { headers: { "accept-language": "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7" }, }); assertEquals(acceptsLanguages(req, "zh"), undefined); },});
Deno.test({ name: "acceptsLanguages() handles args and no header", fn() { const req = new Request("https://example.com/"); assertEquals(acceptsLanguages(req, "en-gb", "en-us", "en"), "en-gb"); },});
Deno.test({ name: "acceptsLanguages() handles no args and no header", fn() { const req = new Request("https://example.com/"); assertEquals(acceptsLanguages(req), ["*"]); },});
Version Info