-
Notifications
You must be signed in to change notification settings - Fork 230
/
mediaTyper_test.ts
92 lines (81 loc) · 1.84 KB
/
mediaTyper_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import {
assertEquals,
assertStrictEq,
assertThrows,
test
} from "./test_deps.ts";
import { format, parse } from "./mediaTyper.ts";
test(function formatBasicType() {
const actual = format({ type: "text", subtype: "html" });
assertStrictEq(actual, "text/html");
});
test(function formatWithSuffix() {
const actual = format({ type: "image", subtype: "svg", suffix: "xml" });
assertStrictEq(actual, "image/svg+xml");
});
test(function invalidType() {
assertThrows(
() => {
format({ type: "text/", subtype: "html" });
},
TypeError,
"Invalid type"
);
});
test(function invalidSubType() {
assertThrows(
() => {
format({ type: "text", subtype: "html/" });
},
TypeError,
"Invalid subtype"
);
});
test(function invalidSubType() {
assertThrows(
() => {
format({ type: "image", subtype: "svg", suffix: "xml\\" });
},
TypeError,
"Invalid suffix"
);
});
test(function parseBasicType() {
const actual = parse("text/html");
assertEquals(actual, { type: "text", subtype: "html", suffix: undefined });
});
test(function parseWithSuffix() {
const actual = parse("image/svg+xml");
assertEquals(actual, { type: "image", subtype: "svg", suffix: "xml" });
});
test(function parseLowerCase() {
const actual = parse("IMAGE/SVG+XML");
assertEquals(actual, { type: "image", subtype: "svg", suffix: "xml" });
});
const invalidTypes = [
" ",
"null",
"undefined",
"/",
"text/;plain",
'text/"plain"',
"text/p£ain",
"text/(plain)",
"text/@plain",
"text/plain,wrong"
];
for (const type of invalidTypes) {
test({
name: `invalidType: "${type}"`,
fn() {
assertThrows(
() => {
parse(type);
},
TypeError,
"Invalid media type"
);
}
});
}