forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isMediaType_test.ts
130 lines (112 loc) · 4.22 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
// Copyright 2018-2019 the oak authors. All rights reserved. MIT license.
import { test, assert, assertEquals, assertStrictEq } from "./test_deps.ts";
import { isMediaType } from "./isMediaType.ts";
test(function shouldIgnoreParams() {
const actual = isMediaType("text/html; charset=utf-8", ["text/*"]);
assertEquals(actual, "text/html");
});
test(function shouldIgnoreParamsLWS() {
const actual = isMediaType("text/html ; charset=utf-8", ["text/*"]);
assertEquals(actual, "text/html");
});
test(function shouldIgnoreCasing() {
const actual = isMediaType("text/HTML", ["text/*"]);
assertEquals(actual, "text/html");
});
test(function shouldFailWithInvalidType() {
const actual = isMediaType("text/html**", ["text/*"]);
assertEquals(actual, false);
});
test(function returnsFalseWithInvalidTypes() {
assertEquals(isMediaType("text/html", ["text/html/"]), false);
});
test(function noTypesGiven() {
assertEquals(isMediaType("image/png", []), "image/png");
});
test(function typeOrFalse() {
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(function firstTypeOrFalse() {
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");
assertStrictEq(isMediaType("image/png", ["jpeg"]), false);
assertStrictEq(isMediaType("image/png", [".jpeg"]), false);
assertStrictEq(isMediaType("image/png", ["text/*", "application/*"]), false);
assertStrictEq(
isMediaType("image/png", ["text/html", "text/plain", "application/json"]),
false
);
});
test(function matchSuffix() {
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"
);
assertStrictEq(
isMediaType("application/vnd+json", ["application/json"]),
false
);
assertStrictEq(isMediaType("application/vnd+json", ["text/*+json"]), false);
});
test(function starStarMatchesContentType() {
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(function starStarInvalidMTReturnsFalse() {
assertStrictEq(isMediaType("bogus", ["*/*"]), false);
});
test(function 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(function matchMultipartStar() {
assertEquals(
isMediaType("multipart/form-data", ["multipart/*"]),
"multipart/form-data"
);
});
test(function matchMultipart() {
assertEquals(isMediaType("multipart/form-data", ["multipart"]), "multipart");
});