deno.land / std@0.166.0 / media_types / 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "../testing/asserts.ts";import { contentType, extension, extensionsByType, formatMediaType, getCharset, parseMediaType, typeByExtension,} from "./mod.ts";
Deno.test({ name: "media_types - contentType()", fn() { const fixtures = [ [".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"], ["foo", undefined], ["file.json", undefined], ["application/foo", "application/foo"], ] as const; for (const [fixture, expected] of fixtures) { assertEquals(contentType(fixture), expected); } },});
Deno.test({ name: "media_types - contentType()", fn() { let _str: string; // For well-known content types, the return type is a string. // string is assignable to string _str = contentType(".json"); _str = contentType("text/html"); _str = contentType("txt");
// @ts-expect-error: string | undefined is not assignable to string _str = contentType("text/plain; charset=ISO-8859-1"); // @ts-expect-error: string | undefined is not assignable to string _str = contentType("foo"); // @ts-expect-error: string | undefined is not assignable to string _str = contentType("file.json"); // @ts-expect-error: string | undefined is not assignable to string _str = contentType("application/foo"); },});
Deno.test({ name: "media_types - extension()", fn() { const fixtures: [string, string | undefined][] = [ ["image/gif", "gif"], ["application/javascript", "js"], ["text/html; charset=UTF-8", "html"], ["application/foo", undefined], ]; for (const [fixture, expected] of fixtures) { assertEquals(extension(fixture), expected); } },});
Deno.test({ name: "media_types - extensionsByType()", fn() { const fixtures: [string, string[] | undefined][] = [ ["image/gif", ["gif"]], ["application/javascript", ["js", "mjs"]], ["text/html; charset=UTF-8", ["html", "htm", "shtml"]], ["application/foo", undefined], ]; for (const [fixture, expected] of fixtures) { assertEquals(extensionsByType(fixture), expected); } },});
Deno.test({ name: "media_types - formatMediaType", fn() { const fixtures = [ ["noslash", { X: "Y" }, "noslash; x=Y"], ["foo bar/baz", undefined, ""], ["foo/bar baz", undefined, ""], [ "attachment", { filename: "ĄĄŽŽČČŠŠ" }, "attachment; filename*=utf-8''%C4%84%C4%84%C5%BD%C5%BD%C4%8C%C4%8C%C5%A0%C5%A0", ], [ "attachment", { filename: "ÁÁÊÊÇÇÎÎ" }, "attachment; filename*=utf-8''%C3%81%C3%81%C3%8A%C3%8A%C3%87%C3%87%C3%8E%C3%8E", ], [ "attachment", { filename: "数据统计.png" }, "attachment; filename*=utf-8''%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1.png", ], ["foo/BAR", undefined, "foo/bar"], ["foo/BAR", { "X": "Y" }, "foo/bar; x=Y"], ["foo/BAR", { "space": "With space" }, `foo/bar; space="With space"`], ["foo/BAR", { "quote": `With "quote` }, `foo/bar; quote="With \\"quote"`], [ "foo/BAR", { "bslash": `With \\backslash` }, `foo/bar; bslash="With \\\\backslash"`, ], [ "foo/BAR", { "both": `With \\backslash and "quote` }, `foo/bar; both="With \\\\backslash and \\"quote"`, ], ["foo/BAR", { "": "empty attribute" }, ""], ["foo/BAR", { "bad attribute": "baz" }, ""], [ "foo/BAR", { "nonascii": "not an ascii character: ä" }, "foo/bar; nonascii*=utf-8''not%20an%20ascii%20character%3A%20%C3%A4", ], [ "foo/BAR", { "ctl": "newline: \n nil: \0" }, "foo/bar; ctl*=utf-8''newline%3A%20%0A%20nil%3A%20%00", ], [ "foo/bar", { "a": "av", "b": "bv", "c": "cv" }, "foo/bar; a=av; b=bv; c=cv", ], ["foo/bar", { "0": "'", "9": "'" }, "foo/bar; 0='; 9='"], ["foo", { "bar": "" }, `foo; bar=""`], ] as const; for (const [type, param, expected] of fixtures) { assertEquals(formatMediaType(type, param), expected); } },});
Deno.test({ name: "media-types - getCharset()", fn() { const fixtures = [ ["text/plain", "UTF-8"], ["text/html", "UTF-8"], ["application/foo", undefined], ["application/news-checkgroups", "US-ASCII"], ["application/news-checkgroups; charset=UTF-8", "UTF-8"], ] as const; for (const [fixture, expected] of fixtures) { assertEquals(getCharset(fixture), expected); } },});
Deno.test({ name: "media_types - parseMediaType()", fn() { const nameFoo = { "name": "foo" }; const fixtures: [string, string, Record<string, string> | undefined][] = [ [`form-data; name="foo"`, "form-data", nameFoo], [` form-data ; name=foo`, "form-data", nameFoo], [`FORM-DATA;name="foo"`, "form-data", nameFoo], [` FORM-DATA ; name="foo"`, "form-data", nameFoo], [` FORM-DATA ; name="foo"`, "form-data", nameFoo], [`form-data; key=value; blah="value";name="foo" `, "form-data", { key: "value", blah: "value", name: "foo", }], [ `application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A`, "application/x-stuff", { title: "This is ***fun***", }, ], [ `message/external-body; access-type=URL; ` + `URL*0="ftp://";` + `URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"`, "message/external-body", { "access-type": "URL", url: "ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar", }, ], [ `application/x-stuff; ` + `title*0*=us-ascii'en'This%20is%20even%20more%20; ` + `title*1*=%2A%2A%2Afun%2A%2A%2A%20; ` + `title*2="isn't it!"`, `application/x-stuff`, { title: "This is even more ***fun*** isn't it!", }, ], [`attachment`, "attachment", undefined], [`ATTACHMENT`, "attachment", undefined], [`attachment; filename="foo.html"`, "attachment", { filename: "foo.html", }], [`attachment; filename="f\\oo.html"`, "attachment", { filename: "f\\oo.html", }], [`attachment; filename="Here's a semicolon;.html"`, "attachment", { filename: "Here's a semicolon;.html", }], [`attachment; filename="foo-%c3%a4-%e2%82%ac.html"`, "attachment", { filename: "foo-%c3%a4-%e2%82%ac.html", }], [ `attachment; filename*=''foo-%c3%a4-%e2%82%ac.html`, "attachment", undefined, ], [`attachment; filename*=UTF-8''foo-a%cc%88.html`, "attachment", { filename: "foo-ä.html", }], [`attachment; filename*0="foo."; filename*1="html"`, "attachment", { filename: "foo.html", }], [`form-data; firstname="Брэд"; lastname="Фицпатрик"`, "form-data", { firstname: "Брэд", lastname: "Фицпатрик", }], [ `form-data; name="file"; filename="C:\\dev\\go\\robots.txt"`, "form-data", { name: "file", filename: `C:\\dev\\go\\robots.txt` }, ], [ `form-data; name="file"; filename="C:\\新建文件夹\\中文第二次测试.mp4"`, "form-data", { name: "file", filename: `C:\\新建文件夹\\中文第二次测试.mp4` }, ], ];
for (const [fixture, mediaType, params] of fixtures) { assertEquals(parseMediaType(fixture), [mediaType, params]); } },});
Deno.test({ name: "media_types - typeByExtension", fn() { const fixtures = [ ["js", "application/javascript"], [".js", "application/javascript"], ["Js", "application/javascript"], ["html", "text/html"], [".html", "text/html"], [".HTML", "text/html"], ["file.json", undefined], ["foo", undefined], [".foo", undefined], ] as const; for (const [fixture, expected] of fixtures) { assertEquals(typeByExtension(fixture), expected); } },});
std

Version Info

Tagged at
a year ago