Extremely Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import { test, assertEquals } from "../test_deps.ts";import { preferredMediaTypes } from "./mediaType.ts";
test({ name: "preferredMediaTypes accepts undefined", fn() { assertEquals(preferredMediaTypes(), ["*/*"]); },});
test({ name: "preferredMediaTypes accepts */*", fn() { assertEquals(preferredMediaTypes("*/*"), ["*/*"]); },});
test({ name: "preferredMediaTypes basic", fn() { assertEquals(preferredMediaTypes("application/json"), ["application/json"]); },});
test({ name: "preferredMediaTypes with a q=0", fn() { assertEquals(preferredMediaTypes("application/json;q=0"), []); },});
test({ name: "preferredMediaTypes with a low q", fn() { assertEquals(preferredMediaTypes("application/json;q=0.2, text/html"), [ "text/html", "application/json", ]); },});
test({ name: "preferredMediaTypes with a text/*", fn() { assertEquals(preferredMediaTypes("text/*"), ["text/*"]); },});
test({ name: "preferredMediaTypes with a complex q", fn() { assertEquals( preferredMediaTypes( "text/plain, application/json;q=0.5, text/html, */*;q=0.1", ), ["text/plain", "text/html", "application/json", "*/*"], ); },});
test({ name: "preferredMediaTypes that is super long", fn() { assertEquals( preferredMediaTypes( "text/plain, application/json;q=0.5, text/html, text/xml, text/yaml, text/javascript, text/csv, text/css, text/rtf, text/markdown, application/octet-stream;q=0.2, */*;q=0.1", ), [ "text/plain", "text/html", "text/xml", "text/yaml", "text/javascript", "text/csv", "text/css", "text/rtf", "text/markdown", "application/json", "application/octet-stream", "*/*", ], ); },});
test({ name: "preferredMediaTypes provided is undefined", fn() { assertEquals(preferredMediaTypes(undefined, ["text/html"]), ["text/html"]); assertEquals( preferredMediaTypes(undefined, ["text/html", "application/json"]), ["text/html", "application/json"], ); assertEquals( preferredMediaTypes(undefined, ["application/json", "text/html"]), ["application/json", "text/html"], ); },});
test({ name: "preferredMediaTypes provides accepts", fn() { assertEquals(preferredMediaTypes("*/*", ["text/html"]), ["text/html"]); assertEquals( preferredMediaTypes("*/*", ["text/html", "application/json"]), [ "text/html", "application/json", ], ); assertEquals( preferredMediaTypes("*/*", ["application/json", "text/html"]), [ "application/json", "text/html", ], ); },});
test({ name: "preferredMediaTypes is case insensitive", fn() { assertEquals( preferredMediaTypes("application/json", ["application/JSON"]), [ "application/JSON", ], ); },});
test({ name: "preferredMediaTypes only returns an accepted value", fn() { assertEquals(preferredMediaTypes("application/json", ["text/html"]), []); assertEquals( preferredMediaTypes( "application/json", ["text/html", "application/json"], ), ["application/json"], ); },});
test({ name: "preferredMediaType provided but with q=0", fn() { assertEquals( preferredMediaTypes("application/json;q=0", ["application/json"]), [], ); },});
test({ name: "preferredMediaType accepts low q", fn() { assertEquals( preferredMediaTypes("application/json;q=0.2, text/html", [ "application/json", ]), ["application/json"], ); assertEquals( preferredMediaTypes("application/json;q=0.2, text/html", [ "application/json", "text/html", ]), ["text/html", "application/json"], ); assertEquals( preferredMediaTypes("application/json;q=0.2, text/html", [ "text/html", "application/json", ]), ["text/html", "application/json"], ); },});
test({ name: "preferredMediaTypes against text/*", fn() { assertEquals(preferredMediaTypes("text/*", ["application/json"]), []); assertEquals( preferredMediaTypes("text/*", ["application/json", "text/html"]), ["text/html"], ); assertEquals( preferredMediaTypes("text/*", ["text/html", "application/json"]), ["text/html"], ); },});
test({ name: "preferredMediaTypes provided respects ordering", fn() { assertEquals( preferredMediaTypes( "text/plain, application/json;q=0.5, text/html, */*;q=0.1", ["application/json", "text/plain", "text/html"], ), ["text/plain", "text/html", "application/json"], ); assertEquals( preferredMediaTypes( "text/plain, application/json;q=0.5, text/html, */*;q=0.1", ["image/jpeg", "text/html"], ), ["text/html", "image/jpeg"], ); assertEquals( preferredMediaTypes( "text/plain, application/json;q=0.5, text/html, */*;q=0.1", ["image/jpeg", "image/gif"], ), ["image/jpeg", "image/gif"], ); },});
test({ name: "preferredMediaTypes with complex preferred order", fn() { assertEquals( preferredMediaTypes( "text/plain, application/json;q=0.5, text/html, text/xml, text/yaml, text/javascript, text/csv, text/css, text/rtf, text/markdown, application/octet-stream;q=0.2, */*;q=0.1", [ "text/plain", "text/html", "text/xml", "text/yaml", "text/javascript", "text/csv", "text/css", "text/rtf", "text/markdown", "application/json", "application/octet-stream", ], ), [ "text/plain", "text/html", "text/xml", "text/yaml", "text/javascript", "text/csv", "text/css", "text/rtf", "text/markdown", "application/json", "application/octet-stream", ], ); },});