forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediaTyper_test.ts
116 lines (105 loc) · 2.09 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2018-2020 the oak authors. All rights reserved. MIT license.
import {
assertEquals,
assertStrictEq,
assertThrows,
test,
} from "./test_deps.ts";
import { format, parse } from "./mediaTyper.ts";
test({
name: "format basic type",
fn() {
const actual = format({ type: "text", subtype: "html" });
assertStrictEq(actual, "text/html");
},
});
test({
name: "format type with suffic",
fn() { // formatWithSuffix() {
const actual = format({ type: "image", subtype: "svg", suffix: "xml" });
assertStrictEq(actual, "image/svg+xml");
},
});
test({
name: "format invalid type",
fn() {
assertThrows(
() => {
format({ type: "text/", subtype: "html" });
},
TypeError,
"Invalid type",
);
},
});
test({
name: "format invalid sub type",
fn() {
assertThrows(
() => {
format({ type: "text", subtype: "html/" });
},
TypeError,
"Invalid subtype",
);
},
});
test({
name: "format invalid suffix",
fn() {
assertThrows(
() => {
format({ type: "image", subtype: "svg", suffix: "xml\\" });
},
TypeError,
"Invalid suffix",
);
},
});
test({
name: "parse basic type",
fn() {
const actual = parse("text/html");
assertEquals(actual, { type: "text", subtype: "html", suffix: undefined });
},
});
test({
name: "parse with suffix",
fn() {
const actual = parse("image/svg+xml");
assertEquals(actual, { type: "image", subtype: "svg", suffix: "xml" });
},
});
test({
name: "parse is case insensitive",
fn() {
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: `parse invalidType: "${type}"`,
fn() {
assertThrows(
() => {
parse(type);
},
TypeError,
"Invalid media type",
);
},
});
}