forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isMediaType_test.ts
189 lines (170 loc) · 4.99 KB
/
isMediaType_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2018-2021 the oak authors. All rights reserved. MIT license.
import { assertEquals, assertStrictEquals } from "./test_deps.ts";
import { isMediaType } from "./isMediaType.ts";
const { test } = Deno;
test({
name: "isMediaType should ignore params",
fn() {
const actual = isMediaType("text/html; charset=utf-8", ["text/*"]);
assertEquals(actual, "text/html");
},
});
test({
name: "isMediaType should ignore params LWS",
fn() {
const actual = isMediaType("text/html ; charset=utf-8", ["text/*"]);
assertEquals(actual, "text/html");
},
});
test({
name: "isMediaType should ignore casing",
fn() {
const actual = isMediaType("text/HTML", ["text/*"]);
assertEquals(actual, "text/html");
},
});
test({
name: "isMediaType should fail with invalid type",
fn() {
const actual = isMediaType("text/html**", ["text/*"]);
assertEquals(actual, false);
},
});
test({
name: "isMediaType returns false with invalid types",
fn() {
assertEquals(isMediaType("text/html", ["text/html/"]), false);
},
});
test({
name: "isMediaType no types given",
fn() {
assertEquals(isMediaType("image/png", []), "image/png");
},
});
test({
name: "isMediaType type or false",
fn() {
assertEquals(isMediaType("image/png", ["png"]), "png");
assertEquals(isMediaType("image/png", [".png"]), ".png");
assertEquals(isMediaType("image/png", ["image/png"]), "image/png");
assertEquals(isMediaType("image/png", ["image/*"]), "image/png");
assertEquals(isMediaType("image/png", ["*/png"]), "image/png");
assertEquals(isMediaType("image/png", ["jpeg"]), false);
assertEquals(isMediaType("image/png", [".jpeg"]), false);
assertEquals(isMediaType("image/png", ["image/jpeg"]), false);
assertEquals(isMediaType("image/png", ["text/*"]), false);
assertEquals(isMediaType("image/png", ["*/jpeg"]), false);
assertEquals(isMediaType("image/png", ["bogus"]), false);
assertEquals(isMediaType("image/png", ["something/bogus*"]), false);
},
});
test({
name: "isMediaType first type or false",
fn() {
assertEquals(isMediaType("image/png", ["png"]), "png");
assertEquals(isMediaType("image/png", [".png"]), ".png");
assertEquals(isMediaType("image/png", ["text/*", "image/*"]), "image/png");
assertEquals(isMediaType("image/png", ["image/*", "text/*"]), "image/png");
assertEquals(
isMediaType("image/png", ["image/*", "image/png"]),
"image/png",
);
assertEquals(
isMediaType("image/png", ["image/png", "image/*"]),
"image/png",
);
assertStrictEquals(isMediaType("image/png", ["jpeg"]), false);
assertStrictEquals(isMediaType("image/png", [".jpeg"]), false);
assertStrictEquals(
isMediaType("image/png", ["text/*", "application/*"]),
false,
);
assertStrictEquals(
isMediaType("image/png", ["text/html", "text/plain", "application/json"]),
false,
);
},
});
test({
name: "isMediaType match suffix",
fn() {
assertEquals(
isMediaType("application/vnd+json", ["+json"]),
"application/vnd+json",
);
assertEquals(
isMediaType("application/vnd+json", ["application/vnd+json"]),
"application/vnd+json",
);
assertEquals(
isMediaType("application/vnd+json", ["application/*+json"]),
"application/vnd+json",
);
assertEquals(
isMediaType("application/vnd+json", ["*/vnd+json"]),
"application/vnd+json",
);
assertStrictEquals(
isMediaType("application/vnd+json", ["application/json"]),
false,
);
assertStrictEquals(
isMediaType("application/vnd+json", ["text/*+json"]),
false,
);
},
});
test({
name: "isMediaType start matches content type",
fn() {
assertEquals(isMediaType("text/html", ["*/*"]), "text/html");
assertEquals(isMediaType("text/xml", ["*/*"]), "text/xml");
assertEquals(isMediaType("application/json", ["*/*"]), "application/json");
assertEquals(
isMediaType("application/vnd+json", ["*/*"]),
"application/vnd+json",
);
},
});
test({
name: "isMediaType start with invalid media type returns false",
fn() {
assertStrictEquals(isMediaType("bogus", ["*/*"]), false);
},
});
test({
name: "isMediaType matching url encoded",
fn() { //matchUrlEncoded() {
assertEquals(
isMediaType("application/x-www-form-urlencoded", ["urlencoded"]),
"urlencoded",
);
assertEquals(
isMediaType("application/x-www-form-urlencoded", ["json", "urlencoded"]),
"urlencoded",
);
assertEquals(
isMediaType("application/x-www-form-urlencoded", ["urlencoded", "json"]),
"urlencoded",
);
},
});
test({
name: "isMediaType matching multipart star",
fn() { //matchMultipartStar() {
assertEquals(
isMediaType("multipart/form-data", ["multipart/*"]),
"multipart/form-data",
);
},
});
test({
name: "isMediaType matching multipart",
fn() { //matchMultipart() {
assertEquals(
isMediaType("multipart/form-data", ["multipart"]),
"multipart",
);
},
});