Debian Codegraph @

tests/code/testcases/testcastleuriutils.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestUriUtils" -*-
2 {
3 Copyright 2013-2021 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Test CastleUriUtils unit. }
18 unit TestCastleUriUtils;
19
20 interface
21
22 uses
23 Classes, SysUtils, CastleTester;
24
25 type
26 TTestUriUtils = class(TCastleTestCase)
27 procedure TestUriProtocol;
28 procedure TestAbsoluteUri;
29 procedure TestUriToFilenameSafe;
30 procedure TestPercentEncoding;
31 procedure TestCombineUriEncoding;
32 procedure TestUriDisplay;
33 procedure TestRelativeURLWhitespace;
34 procedure TestUriExists;
35 procedure TestRelativeToCastleDataURL;
36 procedure TestExtractUri;
37 procedure TestDotfile;
38 procedure TestDecodeBase64;
39 procedure TestEncodeBase64;
40 procedure TestMimeTypeHttpQuery;
41 procedure TestUriMimeType;
42 end;
43
44 implementation
45
46 uses Base64, UriParser,
47 CastleUriUtils, CastleUtils, CastleClassUtils;
48
49 procedure TTestUriUtils.TestUriProtocol;
50 var
51 Colon: Integer;
52 begin
53 AssertTrue(UriProtocol('data:blah:foo') = 'data');
54 AssertTrue(UriProtocolIs('data:blah:foo', 'data', Colon));
55 AssertTrue(not UriProtocolIs('data:blah:foo', 'data1', Colon));
56 AssertTrue(not UriProtocolIs('data:blah:foo', 'dat', Colon));
57 AssertTrue(not UriProtocolIs('data', 'data', Colon));
58 AssertTrue(not UriProtocolIs('', 'data', Colon));
59
60 AssertTrue(UriProtocol('ecmascript:xyz') = 'ecmascript');
61 AssertTrue(UriDeleteProtocol('ecmascript:xyz') = 'xyz');
62
63 AssertTrue(UriProtocol(' ' + NL + ' ecmascript:xyz') = 'ecmascript');
64 AssertTrue(UriDeleteProtocol(' ' + NL + ' ecmascript:xyz') = 'xyz');
65
66 AssertTrue(UriProtocol('void main()' + NL + 'ecmascript:xyz') = '');
67 AssertTrue(UriDeleteProtocol('void main()' + NL + 'ecmascript:xyz') = 'void main()' + NL + 'ecmascript:xyz');
68 end;
69
70 procedure TTestUriUtils.TestAbsoluteUri;
71 {$ifdef MSWINDOWS}
72 var
73 Drive: string;
74 {$endif}
75 begin
76 {$ifdef MSWINDOWS}
77 AssertFilenamesEqual('file:///C:/foo.txt', AbsoluteUri('c:\foo.txt'));
78 { Below ExpandFileName will change /foo.txt on Windows to add drive letter }
79 Drive := ExtractFileDrive(GetCurrentDir);
80 AssertFilenamesEqual('file:///' + Drive + '/foo.txt', AbsoluteUri('/foo.txt'));
81 {$endif}
82
83 {$ifdef UNIX}
84 { Below ExpandFileName will add path on Unix, treating "c:"
85 like a normal filename.
86
87 Also, thanks to our AllowDirectorySeparators fix in CastleUtils,
88 the backslash remains intact (with stupid default
89 AllowDirectorySeparators backslash would get converted to slash
90 by ExpandFileName). }
91 AssertEquals(FilenameToUriSafe(InclPathDelim(GetCurrentDir) + 'c:\foo.txt'), AbsoluteUri('c:\foo.txt'));
92 AssertEquals(InclPathDelim(GetCurrentDir) + 'c:\foo.txt', ExpandFileName('c:\foo.txt'));
93
94 AssertEquals('file:///foo.txt', AbsoluteUri('/foo.txt'));
95 {$endif}
96
97 AssertFilenamesEqual(FilenameToUriSafe(InclPathDelim(GetCurrentDir) + 'foo.txt'), AbsoluteUri('foo.txt'));
98 AssertEquals('http://foo', AbsoluteUri('http://foo'));
99 AssertFilenamesEqual(FilenameToUriSafe(InclPathDelim(GetCurrentDir)), AbsoluteUri(''));
100 end;
101
102 procedure TTestUriUtils.TestUriToFilenameSafe;
103 var
104 Temp: string;
105 begin
106 { UriToFilename fails for Windows absolute filenames,
107 but our UriToFilenameSafe works. }
108 AssertTrue(not UriToFilename('c:\foo.txt', Temp));
109 AssertEquals('c:\foo.txt', UriToFilenameSafe('c:\foo.txt'));
110 end;
111
112 procedure TTestUriUtils.TestPercentEncoding;
113 {$ifdef MSWINDOWS}
114 var
115 FilenamePart: String;
116 FilenamePartPercent: String;
117 FilenamePartUnescaped: String;
118 Filename: String;
119 FilenameAsUri: String;
120 FilenameFromUri: String;
121 {$endif}
122 begin
123 { FilenameToUriSafe must percent-encode,
124 UriToFilenameSafe must decode it back. }
125 {$ifdef MSWINDOWS}
126 AssertFilenamesEqual('file:///C:/foo%254d.txt', FilenameToUriSafe('c:\foo%4d.txt'));
127 AssertFilenamesEqual('C:\fooM.txt', UriToFilenameSafe('file:///C:/foo%4d.txt'));
128 AssertFilenamesEqual('C:\foo%.txt', UriToFilenameSafe('file:///C:/foo%25.txt'));
129 {$endif}
130 {$ifdef UNIX}
131 AssertEquals('file:///foo%254d.txt', FilenameToUriSafe('/foo%4d.txt'));
132 AssertEquals('/fooM.txt', UriToFilenameSafe('file:///foo%4d.txt'));
133 AssertEquals('/foo%.txt', UriToFilenameSafe('file:///foo%25.txt'));
134 {$endif}
135
136 { Always UriToFilenameSafe and FilenameToUriSafe should reverse each other. }
137 {$ifdef MSWINDOWS}
138 AssertFilenamesEqual('C:\foo%4d.txt', UriToFilenameSafe(FilenameToUriSafe('c:\foo%4d.txt')));
139 { Actually this would be valid too:
140 AssertEquals('file:///C:/foo%4d.txt', FilenameToUriSafe(UriToFilenameSafe('file:///C:/foo%4d.txt')));
141 But it's Ok that %4d gets converted to M, as char "M" is safe inside Uri. }
142 AssertFilenamesEqual('file:///C:/fooM.txt', FilenameToUriSafe(UriToFilenameSafe('file:///C:/foo%4d.txt')));
143 {$endif}
144 {$ifdef UNIX}
145 AssertEquals('/foo%4d.txt', UriToFilenameSafe(FilenameToUriSafe('/foo%4d.txt')));
146 { Actually this would be valid too:
147 AssertEquals('file:///foo%25.txt', FilenameToUriSafe(UriToFilenameSafe('file:///foo%25.txt')));
148 But it's Ok that %4d gets converted to M, as char "M" is safe inside Uri. }
149 AssertEquals('file:///fooM.txt', FilenameToUriSafe(UriToFilenameSafe('file:///foo%4d.txt')));
150 {$endif}
151
152 {$ifdef MSWINDOWS}
153 Filename := 'C:\Users\cge\AppData\Local\test_local_filename_chars\config with Polish chars ćma źrebak żmija wąż królik.txt';
154 FilenameAsUri := FilenameToUriSafe(Filename);
155 AssertEquals('file:///C:/Users/cge/AppData/Local/test_local_filename_chars/config%20with%20Polish%20chars%20%C4%87ma%20%C5%BArebak%20%C5%BCmija%20w%C4%85%C5%BC%20kr%C3%B3lik.txt', FilenameAsUri);
156 FilenameFromUri := UriToFilenameSafe(FilenameAsUri);
157 AssertEquals(Filename, FilenameFromUri);
158
159 FilenamePart := 'C:/Users/cge/AppData/Local/test_local_filename_chars/config with Polish chars ćma źrebak żmija wąż królik.txt';
160 FilenamePartPercent := InternalUriEscape(FilenamePart);
161 AssertEquals('C:/Users/cge/AppData/Local/test_local_filename_chars/config%20with%20Polish%20chars%20%C4%87ma%20%C5%BArebak%20%C5%BCmija%20w%C4%85%C5%BC%20kr%C3%B3lik.txt', FilenamePartPercent);
162 FilenamePartUnescaped := InternalUriUnescape(FilenamePartPercent);
163 AssertEquals(FilenamePart, FilenamePartUnescaped);
164 {$endif}
165 end;
166
167 procedure TTestUriUtils.TestCombineUriEncoding;
168 begin
169 { Simple test without any % inside }
170 AssertEquals('http:///game/player_sudden_pain.wav', CombineUri('http:///game/sounds.xml', 'player_sudden_pain.wav'));
171
172 { Special % inside must not be destroyed by CombineUri }
173 AssertEquals('http:///game/player_sudden_pain.wav', CombineUri('http:///game/sounds%25.xml', 'player_sudden_pain.wav'));
174 AssertEquals('http:///game%25/player_sudden_pain.wav', CombineUri('http:///game%25/sounds.xml', 'player_sudden_pain.wav'));
175 AssertEquals('http:///game/player%25_sudden_pain.wav', CombineUri('http:///game/sounds.xml', 'player%25_sudden_pain.wav'));
176 AssertEquals('http:///game%25/player%25_sudden_pain.wav', CombineUri('http:///game%25/sounds.xml', 'player%25_sudden_pain.wav'));
177
178 { It is Ok to convert non-special %xx sequences }
179 AssertEquals('http:///game/player_sudden_pain.wav', CombineUri('http:///game/sounds%4d.xml', 'player_sudden_pain.wav'));
180 AssertEquals('http:///gameM/player_sudden_pain.wav', CombineUri('http:///game%4d/sounds.xml', 'player_sudden_pain.wav'));
181 AssertEquals('http:///game/playerM_sudden_pain.wav', CombineUri('http:///game/sounds.xml', 'player%4d_sudden_pain.wav'));
182 AssertEquals('http:///gameM/playerM_sudden_pain.wav', CombineUri('http:///game%4d/sounds.xml', 'player%4d_sudden_pain.wav'));
183 end;
184
185 procedure TTestUriUtils.TestUriDisplay;
186 const
187 DataUriX3D =
188 'data:model/x3d+xml,<?xml version="1.0" encoding="UTF-8"?>' + LineEnding +
189 '<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d-3.0.dtd">' + LineEnding +
190 '<X3D version="3.0" profile="Immersive" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xsd:noNamespaceSchemaLocation="http://www.web3d.org/specifications/x3d-3.0.xsd">' + LineEnding +
191 '<head>' + LineEnding +
192 '</head>' + LineEnding +
193 '<Scene>' + LineEnding +
194 ' <Transform translation="0 -8 0">' + LineEnding +
195 ' <Shape>' + LineEnding +
196 ' <Text string=''"X3D (XML) model inlined using data Uri"'' />' + LineEnding +
197 ' </Shape>' + LineEnding +
198 ' </Transform>' + LineEnding +
199 '</Scene>' + LineEnding +
200 '</X3D>' + LineEnding +
201 '';
202 ScriptUri =
203 '' + LineEnding +
204 'ecmascript:' + LineEnding +
205 'blah blah' + LineEnding +
206 '';
207 ScriptUri2 =
208 '' + LineEnding +
209 'ecmascript:' + LineEnding +
210 '';
211
212 begin
213 AssertEquals('http:///blah/player_sudden_pain.wav#blabla', UriDisplay('http:///blah/player_sudden_pain.wav#blabla'));
214 AssertEquals('file:///blah/player_sudden_pain.wav#blabla', UriDisplay('file:///blah/player_sudden_pain.wav#blabla'));
215 AssertEquals('blah/player_sudden_pain.wav#blabla', UriDisplay('blah/player_sudden_pain.wav#blabla'));
216 AssertEquals('data:model/x3d+xml,...', UriDisplay(DataUriX3D));
217 AssertEquals('ecmascript:...', UriDisplay(ScriptUri));
218 AssertEquals('ecmascript:', UriDisplay(ScriptUri2));
219
220 AssertEquals('', UriDisplay('', false));
221 AssertEquals('', UriDisplay('', true));
222 AssertEquals('', UriCaption(''));
223 end;
224
225 procedure TTestUriUtils.TestRelativeURLWhitespace;
226 begin
227 AssertEquals(' castlescript: blablah', CombineUri('http:///foo/bar', ' castlescript: blablah'));
228 AssertEquals(NL + 'castlescript: blablah', CombineUri('http:///foo/bar', NL + 'castlescript: blablah'));
229
230 AssertEquals(' ecmascript: blablah', CombineUri('http:///foo/bar', ' ecmascript: blablah'));
231 AssertEquals(NL + 'ecmascript: blablah', CombineUri('http:///foo/bar', NL + 'ecmascript: blablah'));
232 AssertEquals(NL + ' ecmascript: blablah', CombineUri('http:///foo/bar', NL + ' ecmascript: blablah'));
233 end;
234
235 procedure TTestUriUtils.TestUriExists;
236 begin
237 AssertTrue(ueUnknown = UriExists('http:/whatever'));
238 AssertTrue(ueUnknown = UriExists('unknown-protocol:/whatecer'));
239
240 AssertTrue(ueNotExists = UriExists('castle-data:/not_existing'));
241 AssertTrue(ueFile = UriExists('castle-data:/game/level.xml'));
242 AssertTrue(ueDirectory = UriExists('castle-data:/game/'));
243 AssertTrue(ueDirectory = UriExists('castle-data:/game'));
244 AssertTrue(ueDirectory = UriExists('castle-data:/'));
245
246 AssertTrue(ueNotExists = UriExists(ResolveCastleDataURL('castle-data:/not_existing')));
247 AssertTrue(ueFile = UriExists(ResolveCastleDataURL('castle-data:/game/level.xml')));
248 AssertTrue(ueDirectory = UriExists(ResolveCastleDataURL('castle-data:/game/')));
249 AssertTrue(ueDirectory = UriExists(ResolveCastleDataURL('castle-data:/game')));
250 AssertTrue(ueDirectory = UriExists(ResolveCastleDataURL('castle-data:/')));
251
252 AssertTrue(ueFile = UriExists('data:model/vrml,#VRML V2.0 utf8' + NL +
253 'Transform {' + NL +
254 ' translation 0 -2 0' + NL +
255 ' children Shape {' + NL +
256 ' geometry Text { string "VRML 2.0 model inlined using data URI" }' + NL +
257 ' }' + NL +
258 '}'));
259 end;
260
261 procedure TTestUriUtils.TestRelativeToCastleDataURL;
262 var
263 WasInsideData: Boolean;
264 begin
265 AssertEquals('foo/bar.txt', RelativeToCastleDataURL('castle-data:/foo/bar.txt', WasInsideData));
266 AssertTrue(WasInsideData);
267 AssertEquals('bar.txt', RelativeToCastleDataURL('castle-data:/bar.txt', WasInsideData));
268 AssertTrue(WasInsideData);
269 AssertEquals('foo/bar.txt', RelativeToCastleDataURL(ResolveCastleDataURL('castle-data:/foo/bar.txt'), WasInsideData));
270 AssertTrue(WasInsideData);
271 AssertEquals('bar.txt', RelativeToCastleDataURL(ResolveCastleDataURL('castle-data:/bar.txt'), WasInsideData));
272 AssertTrue(WasInsideData);
273 AssertEquals('http://example.com/bar.txt', RelativeToCastleDataURL('http://example.com/bar.txt', WasInsideData));
274 AssertFalse(WasInsideData);
275 AssertEquals('/something-not-in-root/bar.txt', RelativeToCastleDataURL('/something-not-in-root/bar.txt', WasInsideData));
276 AssertFalse(WasInsideData);
277 end;
278
279 procedure TTestUriUtils.TestExtractUri;
280 begin
281 // Test with valid Spine URL, ending with anim-naming:strict-underscore
282 AssertEquals('castle-data:/starling/', ExtractUriPath('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:strict-underscore'));
283 AssertEquals('character_zombie_atlas.starling-xml', ExtractUriName('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:strict-underscore'));
284 AssertEquals('castle-data:/starling/character_zombie_atlas.starling-xml', UriDeleteAnchor('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:strict-underscore'));
285 AssertEquals('castle-data:/starling/character_zombie_atlas.new-extension#fps:8,anim-naming:strict-underscore', ChangeUriExt('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:strict-underscore', '.new-extension'));
286 AssertEquals('castle-data:/starling/character_zombie_atlas#fps:8,anim-naming:strict-underscore', DeleteUriExt('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:strict-underscore'));
287
288 // Test also invalid Spine URL, ending with anim-naming:character_zombie_atlas.png
289 AssertEquals('castle-data:/starling/', ExtractUriPath('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:character_zombie_atlas.png'));
290 AssertEquals('character_zombie_atlas.starling-xml', ExtractUriName('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:character_zombie_atlas.png'));
291 AssertEquals('castle-data:/starling/character_zombie_atlas.starling-xml', UriDeleteAnchor('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:character_zombie_atlas.png'));
292 AssertEquals('castle-data:/starling/character_zombie_atlas.new-extension#fps:8,anim-naming:character_zombie_atlas.png', ChangeUriExt('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:character_zombie_atlas.png', '.new-extension'));
293 AssertEquals('castle-data:/starling/character_zombie_atlas#fps:8,anim-naming:character_zombie_atlas.png', DeleteUriExt('castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:character_zombie_atlas.png'));
294
295 AssertEquals('castle-data:/walking/npcs/hotel_room/hero/', ExtractUriPath('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo.json#skin:default'));
296 AssertEquals('hawaii_exo.json', ExtractUriName('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo.json#skin:default'));
297 AssertEquals('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo.json', UriDeleteAnchor('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo.json#skin:default'));
298 AssertEquals('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo.new-extension#skin:default', ChangeUriExt('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo.json#skin:default', '.new-extension'));
299 AssertEquals('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo#skin:default', DeleteUriExt('castle-data:/walking/npcs/hotel_room/hero/hawaii_exo.json#skin:default'));
300
301 AssertEquals('armor.tga', ExtractUriName('armor.tga'));
302 AssertEquals('armor.tga', ExtractUriName('/armor.tga'));
303 AssertEquals('armor.tga', ExtractUriName('blabla/armor.tga'));
304 end;
305
306 procedure TTestUriUtils.TestDotfile;
307 begin
308 { Test that dot on 1st place is not treated as extension start. }
309
310 AssertEquals('.hidden', ExtractUriName('https://blah/.hidden'));
311 AssertEquals('.hidden', ExtractUriName('castle-data:/.hidden'));
312 AssertEquals('.hidden.x3d', ExtractUriName('https://blah/.hidden.x3d'));
313 AssertEquals('.hidden.x3d', ExtractUriName('castle-data:/.hidden.x3d'));
314
315 AssertEquals('https://blah/.hidden', DeleteUriExt('https://blah/.hidden'));
316 AssertEquals('castle-data:/.hidden', DeleteUriExt('castle-data:/.hidden'));
317 AssertEquals('https://blah/.hidden', DeleteUriExt('https://blah/.hidden.x3d'));
318 AssertEquals('castle-data:/.hidden', DeleteUriExt('castle-data:/.hidden.x3d'));
319 AssertEquals('https://blah/.hidden.x3d', DeleteUriExt('https://blah/.hidden.x3d.gz'));
320 AssertEquals('castle-data:/.hidden.x3d', DeleteUriExt('castle-data:/.hidden.x3d.gz'));
321 end;
322
323 procedure TTestUriUtils.TestDecodeBase64;
324 var
325 Source: TStream;
326 Decode: TBase64DecodingStream;
327 DecodeStr: String;
328 begin
329 // sample from https://en.wikipedia.org/wiki/Base64
330
331 { TODO: Why both versions with TStringStream fail on Delphi?
332 TStringStream (esp 2nd variant) should encode in UTF-16, which should
333 be what our TBase64DecodingStream expects (as shown by the fact that
334 MemoryStreamLoadFromDefaultString is OK). }
335 //Source := TStringStream.Create('TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu');
336 //Source := TStringStream.Create('TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu', TEncoding.Unicode, false);
337
338 Source := MemoryStreamLoadFromDefaultString('TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu');
339 try
340 Assert(Source.Size = SizeOf(Char) * Length('TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu'));
341 Decode := TBase64DecodingStream.Create(Source, bdmMIME);
342 try
343 { The input from https://en.wikipedia.org/wiki/Base64 contains 8-bit
344 (ASCII) string encoded, which is why ReadGrowingStreamToString (AnsiString)
345 makes sense.
346 See TBase64DecodingStream about encodings. }
347 DecodeStr :=
348 //ReadGrowingStreamToDefaultString
349 ReadGrowingStreamToString
350 (Decode);
351 AssertEquals('Many hands make light work.', DecodeStr);
352 finally FreeAndNil(Decode) end;
353 finally FreeAndNil(Source) end;
354 end;
355
356 procedure TTestUriUtils.TestEncodeBase64;
357 var
358 S: TStringStream;
359 Encode: TBase64EncodingStream;
360 EncodeStr: String;
361 begin
362 // sample from https://en.wikipedia.org/wiki/Base64
363 S := TStringStream.Create;
364 try
365 Encode := TBase64EncodingStream.Create(S);
366 try
367 { The sample from https://en.wikipedia.org/wiki/Base64 contains 8-bit
368 (ASCII) string encoded, which is why feeding here using WriteStr
369 (this always converts chars to 8-bit, i.e. AnsiString)
370 makes sense.
371 See TBase64EncodingStream about encodings. }
372 WriteStr(Encode, 'Many hands make light work.');
373 finally FreeAndNil(Encode) end;
374 EncodeStr := S.DataString;
375 AssertEquals('TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu', EncodeStr);
376 finally FreeAndNil(S) end;
377 end;
378
379 procedure TTestUriUtils.TestMimeTypeHttpQuery;
380 begin
381 { See https://github.com/castle-engine/castle-engine/issues/547
382 and TUrlFile.MimeTypeStripHttpQuery }
383
384 // no protocol
385 AssertEquals('text/plain', UriMimeType('foo.pas'));
386 AssertEquals('text/plain', UriMimeType('foo.pas?query-is-stripped-for-all-protocols'));
387
388 // file protocol
389 AssertEquals('text/plain', UriMimeType('file:///foo.pas'));
390 AssertEquals('text/plain', UriMimeType('file:///foo.pas?query-is-stripped-for-all-protocols'));
391
392 // http protocol
393 AssertEquals('text/plain', UriMimeType('http://example.com/foo.pas'));
394 AssertEquals('text/plain', UriMimeType('http://example.com/foo.pas?some-query'));
395 AssertEquals('text/plain', UriMimeType('http://example.com/foo.pas?some-query=123'));
396
397 // https protocol
398 AssertEquals('text/plain', UriMimeType('https://example.com/foo.pas'));
399 AssertEquals('text/plain', UriMimeType('https://example.com/foo.pas?some-query'));
400 AssertEquals('text/plain', UriMimeType('https://example.com/foo.pas?some-query=123'));
401 AssertEquals('text/plain', UriMimeType('https://example.com/foo.pas?some-query=123#anchor'));
402
403 // glTF mime type
404
405 // no protocol
406 AssertEquals('model/gltf+json', UriMimeType('foo.gltf'));
407 AssertEquals('model/gltf+json', UriMimeType('foo.gltf?query-is-stripped-for-all-protocols'));
408 AssertEquals('model/gltf+json', UriMimeType('foo.gltf?query-is-stripped-for-all-protocols#anchor'));
409 AssertEquals('model/gltf+json', UriMimeType('foo.gltf#anchor'));
410
411 // file protocol
412 AssertEquals('model/gltf+json', UriMimeType('file:///foo.gltf'));
413 AssertEquals('model/gltf+json', UriMimeType('file:///foo.gltf?query-is-stripped-for-all-protocols'));
414
415 // http protocol
416 AssertEquals('model/gltf+json', UriMimeType('http://example.com/foo.gltf'));
417 AssertEquals('model/gltf+json', UriMimeType('http://example.com/foo.gltf?some-query'));
418 AssertEquals('model/gltf+json', UriMimeType('http://example.com/foo.gltf?some-query=123'));
419
420 // https protocol
421 AssertEquals('model/gltf+json', UriMimeType('https://example.com/foo.gltf'));
422 AssertEquals('model/gltf+json', UriMimeType('https://example.com/foo.gltf?some-query'));
423 AssertEquals('model/gltf+json', UriMimeType('https://example.com/foo.gltf?some-query=123'));
424 AssertEquals('model/gltf+json', UriMimeType('https://example.com/foo.gltf?some-query=123#anchor'));
425
426 // test exactly from https://github.com/castle-engine/castle-engine/issues/547
427 AssertEquals('image/jpeg', UriMimeType('https://cards.scryfall.io/large/front/0/9/092c48bd-b648-4c9e-aa99-cac3c407911d.jpg?1692936576'));
428 end;
429
430 procedure TTestUriUtils.TestUriMimeType;
431 begin
432 AssertEquals('image/png', UriMimeType('aaa.png'));
433 AssertEquals('image/png', UriMimeType('aaa.png#some-anchor'));
434 AssertEquals('image/png', UriMimeType('castle-data:/aaa.png'));
435 AssertEquals('image/png', UriMimeType('castle-data:/aaa.png#some-anchor'));
436
437 AssertEquals('image/png', UriMimeType('%23/aaa.png'));
438 AssertEquals('image/png', UriMimeType('%23/aaa.png#some-anchor'));
439 AssertEquals('image/png', UriMimeType('castle-data:/%23/aaa.png'));
440 AssertEquals('image/png', UriMimeType('castle-data:/%23/aaa.png#some-anchor'));
441
442 AssertEquals('application/x-md3', UriMimeType('head.md3'));
443 AssertEquals('application/x-md3', UriMimeType('head.md3#skin:light'));
444 AssertEquals('application/x-md3', UriMimeType('castle-data:/head.md3'));
445 AssertEquals('application/x-md3', UriMimeType('castle-data:/head.md3#skin:light'));
446
447 AssertEquals('application/json', UriMimeType('dragon.json'));
448 AssertEquals('application/json', UriMimeType('dragon.json#skin:dark'));
449 AssertEquals('application/json', UriMimeType('castle-data:/dragon.json'));
450 AssertEquals('application/json', UriMimeType('castle-data:/dragon.json#skin:dark'));
451 end;
452
453 initialization
454 RegisterTest(TTestUriUtils);
455 end.