Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(media_types): improve test coverage #4554

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion media_types/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function isTokenChar(r: string): boolean {
return code > 0x20 && code < 0x7f && !isTSpecial(r);
}

function isTSpecial(r: string): boolean {
export function isTSpecial(r: string): boolean {
return r[0] ? `()<>@,;:\\"/[]?=`.includes(r[0]) : false;
}

Expand Down
102 changes: 101 additions & 1 deletion media_types/_util_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../assert/mod.ts";
import { consumeMediaParam, consumeToken, consumeValue } from "./_util.ts";
import {
consumeMediaParam,
consumeToken,
consumeValue,
decode2331Encoding,
isIterator,
isToken,
isTSpecial,
} from "./_util.ts";

Deno.test({
name: "consumeToken()",
Expand All @@ -22,6 +30,10 @@ Deno.test({
name: "consumeValue()",
fn() {
const fixtures = [
["", "", ""],
[`"\n"foo`, "", `"\n"foo`],
[`"\r"foo`, "", `"\r"foo`],
[`"\\\\;"`, "\\;", ""],
["foo bar", "foo", " bar"],
["bar", "bar", ""],
[" bar ", "", " bar "],
Expand All @@ -47,6 +59,10 @@ Deno.test({
name: "consumeMediaParam()",
fn() {
const fixtures = [
["", "", "", ""],
["foo=bar", "", "", "foo=bar"],
[";", "", "", ";"],
[";foo", "", "", ";foo"],
[" ; foo=bar", "foo", "bar", ""],
["; foo=bar", "foo", "bar", ""],
[";foo=bar", "foo", "bar", ""],
Expand All @@ -69,3 +85,87 @@ Deno.test({
}
},
});

Deno.test({
name: "decode2331Encoding()",
fn() {
const fixtures = [
["", undefined],
["foo", undefined],
[`foo'bar'baz`, undefined],
[`us-ascii'en-us'`, undefined],
[`us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A`, "This is ***fun***"],
[`UTF-8''foo-a%cc%88.html`, "foo-ä.html"],
] as const;
for (const [fixture, expected] of fixtures) {
assertEquals(decode2331Encoding(fixture), expected);
}
},
});

Deno.test({
name: "isIterator()",
fn() {
const fixtures = [
[null, false],
[undefined, false],
[{}, false],
["", true],
[[], true],
[Object.entries({}), true],
] as const;
for (const [fixture, expected] of fixtures) {
assertEquals(isIterator(fixture), expected);
}
},
});

Deno.test({
name: "isToken()",
fn() {
const fixtures = [
["", false],
[";", false],
["\\", false],
["foo", true],
] as const;
for (const [fixture, expected] of fixtures) {
assertEquals(isToken(fixture), expected);
}
},
});

Deno.test({
name: "isTSpecial()",
fn() {
const fixtues = [
["", false],
[` ()<>@,;:\\"/[]?=`, false],
["(", true],
[")", true],
["<", true],
[">", true],
["@", true],
[",", true],
[";", true],
[":", true],
["\\", true],
['"', true],
["/", true],
["[", true],
["]", true],
["?", true],
["=", true],
[" ", false],
["\t", false],
["\n", false],
["\r", false],
["\f", false],
["\v", false],
["foo", false],
] as const;
for (const [fixture, expected] of fixtues) {
assertEquals(isTSpecial(fixture), expected);
}
},
});
2 changes: 2 additions & 0 deletions media_types/content_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ Deno.test({
name: "contentType()",
fn() {
const fixtures = [
[" ; charset=UTF-8", undefined],
[".json", "application/json; charset=UTF-8"],
["text/html", "text/html; charset=UTF-8"],
["txt", "text/plain; charset=UTF-8"],
["text/plain; charset=ISO-8859-1", "text/plain; charset=ISO-8859-1"],
["text/plan; charset", undefined],
["foo", undefined],
["file.json", undefined],
["application/foo", "application/foo"],
Expand Down
1 change: 1 addition & 0 deletions media_types/extensions_by_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Deno.test({
name: "extensionsByType()",
fn() {
const fixtures: [string, string[] | undefined][] = [
["text/plain; charset", undefined],
["image/gif", ["gif"]],
["application/javascript", ["js", "mjs"]],
["text/html; charset=UTF-8", ["html", "htm", "shtml"]],
Expand Down
2 changes: 2 additions & 0 deletions media_types/format_media_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Deno.test({
name: "formatMediaType()",
fn() {
const fixtures = [
["/", undefined, ""],
["noslash", { X: "Y" }, "noslash; x=Y"],
["foo bar/baz", undefined, ""],
["foo/bar baz", undefined, ""],
Expand Down Expand Up @@ -58,6 +59,7 @@ Deno.test({
],
["foo/bar", { "0": "'", "9": "'" }, "foo/bar; 0='; 9='"],
["foo", { "bar": "" }, `foo; bar=""`],
["foo/bar", [], "foo/bar"],
] as const;
for (const [type, param, expected] of fixtures) {
assertEquals(formatMediaType(type, param), expected);
Expand Down
2 changes: 2 additions & 0 deletions media_types/get_charset_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Deno.test({
name: "getCharset()",
fn() {
const fixtures = [
[";", undefined],
["text/plain; charset", undefined],
["text/plain", "UTF-8"],
["text/html", "UTF-8"],
["application/foo", undefined],
Expand Down
39 changes: 38 additions & 1 deletion media_types/parse_media_type_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals } from "../assert/mod.ts";
import { assertEquals, assertThrows } from "../assert/mod.ts";
import { parseMediaType } from "./mod.ts";

Deno.test({
Expand Down Expand Up @@ -91,3 +91,40 @@ Deno.test({
}
},
});

Deno.test({
name: "parseMediaType() throws on invalid media type",
fn() {
const fixtures = [
`form-data; foo`,
`form-data; foo="bar"; baz`,
] as const;
for (const fixture of fixtures) {
assertThrows(
() => {
parseMediaType(fixture);
},
TypeError,
"Invalid media parameter.",
);
}
},
});

Deno.test({
name: "parseMediaType() throws on duplicate keys",
fn() {
const fixtures = [
`form-data; foo="bar"; foo="baz"`,
] as const;
for (const fixture of fixtures) {
assertThrows(
() => {
parseMediaType(fixture);
},
TypeError,
"Duplicate key parsed.",
);
}
},
});
Loading