| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088 |
- (* _ _
- * | |__ _ __ ___ ___ | | __
- * | '_ \| '__/ _ \ / _ \| |/ /
- * | |_) | | | (_) | (_) | <
- * |_.__/|_| \___/ \___/|_|\_\
- *
- * Microframework which helps to develop web Pascal applications.
- *
- * Copyright (c) 2012-2020 Silvio Clecio <[email protected]>
- *
- * Brook framework is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * Brook framework is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Brook framework; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *)
- program Test_MediaTypes;
- {$I Tests.inc}
- uses
- RTLConsts,
- SysUtils,
- {$IFDEF FPC}
- FileUtil,
- {$ELSE}
- IOUtils,
- {$ENDIF}
- Classes,
- BrookExtra,
- BrookLibraryLoader,
- BrookReader,
- BrookMediaTypes,
- Test;
- const
- MIME_TYPES_FILE = '../Examples/Common/mime.types';
- FAKE_MIME_TYPES_FILE = 'fakemime.types';
- var
- MIMEFileName: TFileName;
- type
- { TFakeMediaTypes }
- TFakeMediaTypes = class(TBrookMediaTypes)
- private
- FPrepared: Boolean;
- protected
- function IsPrepared: Boolean; override;
- public
- class function GetDescription: string; override;
- procedure Prepare; override;
- property Cache;
- end;
- { TFakeMediaTypes }
- class function TFakeMediaTypes.GetDescription: string;
- begin
- Result := 'Fake';
- end;
- procedure TFakeMediaTypes.Prepare;
- begin
- FPrepared := True;
- end;
- function TFakeMediaTypes.IsPrepared: Boolean;
- begin
- Result := FPrepared;
- end;
- { MediaTypes }
- procedure Test_MediaTypesCreate;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(Assigned(MT.Cache));
- Assert(MT.DefaultType = BROOK_CT_OCTET_STREAM);
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesGetRegisterAlias;
- begin
- Assert(TFakeMediaTypes.GetRegisterAlias = 'BrookMIME_Fake');
- end;
- procedure Test_MediaTypesGetDescription;
- begin
- Assert(TFakeMediaTypes.GetDescription = 'Fake');
- end;
- procedure Test_MediaTypesIsValid;
- begin
- Assert(not TBrookMediaTypes.IsValid(''));
- Assert(not TBrookMediaTypes.IsValid('abc'));
- Assert(TBrookMediaTypes.IsValid('abc/'));
- Assert(TBrookMediaTypes.IsValid('/abc'));
- Assert(TBrookMediaTypes.IsValid('abc/def'));
- Assert(TBrookMediaTypes.IsValid('/abc/def/def'));
- end;
- procedure Test_MediaTypesIsText;
- begin
- Assert(not TBrookMediaTypes.IsText(''));
- Assert(not TBrookMediaTypes.IsText('abc'));
- Assert(not TBrookMediaTypes.IsText('abc/'));
- Assert(not TBrookMediaTypes.IsText('/abc'));
- Assert(not TBrookMediaTypes.IsText('/abc/def/def'));
- Assert(not TBrookMediaTypes.IsText('abc/def'));
- Assert(not TBrookMediaTypes.IsText('application/xml'));
- Assert(TBrookMediaTypes.IsText('text/html'));
- end;
- procedure Test_MediaTypesIsExt;
- begin
- Assert(not TBrookMediaTypes.IsExt(''));
- Assert(not TBrookMediaTypes.IsExt('.'));
- Assert(not TBrookMediaTypes.IsExt('..'));
- Assert(TBrookMediaTypes.IsExt('js'));
- Assert(TBrookMediaTypes.IsExt('.js'));
- end;
- procedure Test_MediaTypesNormalizeExt;
- begin
- Assert(TBrookMediaTypes.NormalizeExt('') = '');
- Assert(TBrookMediaTypes.NormalizeExt('.') = '.');
- Assert(TBrookMediaTypes.NormalizeExt('a') = '.a');
- Assert(TBrookMediaTypes.NormalizeExt('.a') = '.a');
- Assert(TBrookMediaTypes.NormalizeExt('a.b') = '.a.b');
- Assert(TBrookMediaTypes.NormalizeExt('.abc') = '.abc');
- end;
- procedure Test_MediaTypesPrepare;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(not MT.IsPrepared);
- MT.Prepare;
- Assert(MT.IsPrepared);
- finally
- MT.Free;
- end;
- end;
- procedure DoMediaTypesAddEmptyMediaExt(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Add('', 'bar/foo');
- end;
- procedure DoMediaTypesAddInvalidMediaExt(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Add('.', 'bar/foo');
- end;
- procedure DoMediaTypesAddEmptyMediaType(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Add('.foo', '');
- end;
- procedure DoMediaTypesAddInvalidMediaType(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Add('.foo', 'bar');
- end;
- procedure Test_MediaTypesAdd;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(not MT.Prepared);
- Assert(MT.Count = 0);
- MT.Add('.foo', 'bar/foo');
- MT.Add('.bar', 'foo/bar');
- Assert(MT.Count = 2);
- MT.Clear;
- AssertExcept(DoMediaTypesAddEmptyMediaExt,
- EArgumentException, SBrookEmptyMediaExt, [MT]);
- AssertExcept(DoMediaTypesAddInvalidMediaExt,
- EBrookMediaTypes, Format(SBrookInvalidMediaExt, ['.']), [MT]);
- AssertExcept(DoMediaTypesAddEmptyMediaType,
- EArgumentException, SBrookEmptyMediaType, [MT]);
- AssertExcept(DoMediaTypesAddInvalidMediaType,
- EBrookMediaTypes, Format(SBrookInvalidMediaType, ['bar']), [MT]);
- finally
- MT.Free;
- end;
- end;
- procedure DoMediaTypesRemoveEmptyMediaExt(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Remove('');
- end;
- procedure DoMediaTypesRemoveInvalidMediaExt(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Remove('.');
- end;
- procedure Test_MediaTypesRemove;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- MT.Add('.foo', 'bar/foo');
- MT.Add('.bar', 'foo/bar');
- Assert(MT.Count = 2);
- MT.Remove('.foo');
- Assert(MT.Count = 1);
- MT.Remove('.bar');
- Assert(MT.Count = 0);
- AssertExcept(DoMediaTypesRemoveEmptyMediaExt,
- EArgumentException, SBrookEmptyMediaExt, [MT]);
- AssertExcept(DoMediaTypesRemoveInvalidMediaExt,
- EBrookMediaTypes, Format(SBrookInvalidMediaExt, ['.']), [MT]);
- finally
- MT.Free;
- end;
- end;
- procedure DoMediaTypesTryTypeEmptyMediaExt(const AArgs: array of const);
- var
- D: string;
- begin
- TFakeMediaTypes(AArgs[0].VObject).TryType('', D);
- end;
- procedure DoMediaTypesTryTypeInvalidMediaExt(const AArgs: array of const);
- var
- D: string;
- begin
- TFakeMediaTypes(AArgs[0].VObject).TryType('.', D);
- end;
- procedure Test_MediaTypesTryType;
- var
- MT: TFakeMediaTypes;
- T: string;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(not MT.Prepared);
- Assert(not MT.TryType('.foo', T));
- Assert(not MT.TryType('.bar', T));
- MT.Add('.foo', 'bar/foo');
- MT.Add('.bar', 'foo/bar');
- Assert(MT.TryType('.foo', T));
- Assert(T = 'bar/foo');
- Assert(MT.TryType('.bar', T));
- Assert(T = 'foo/bar');
- Assert(MT.Prepared);
- AssertExcept(DoMediaTypesTryTypeEmptyMediaExt,
- EArgumentException, SBrookEmptyMediaExt, [MT]);
- AssertExcept(DoMediaTypesTryTypeInvalidMediaExt,
- EBrookMediaTypes, Format(SBrookInvalidMediaExt, ['.']), [MT]);
- finally
- MT.Free;
- end;
- end;
- procedure DoMediaTypesFindEmptyMediaExt(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Find('');
- end;
- procedure DoMediaTypesFindInvalidMediaExt(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Find('.');
- end;
- procedure DoMediaTypesFindEmptyMediaType(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Find('.foo', '');
- end;
- procedure DoMediaTypesFindInvalidMediaType(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).Find('.foo', 'bar');
- end;
- procedure Test_MediaTypesFind;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(not MT.Prepared);
- Assert(MT.Find('.foo') = MT.DefaultType);
- Assert(MT.Find('.foo', 'bar/foo') = 'bar/foo');
- Assert(MT.Find('.bar') = MT.DefaultType);
- Assert(MT.Find('.bar', 'bar/foo') = 'bar/foo');
- MT.Add('.foo', 'bar/foo');
- MT.Add('.bar', 'foo/bar');
- Assert(MT.Find('.foo') = 'bar/foo');
- Assert(MT.Find('.bar') = 'foo/bar');
- Assert(MT.Prepared);
- AssertExcept(DoMediaTypesFindEmptyMediaExt,
- EArgumentException, SBrookEmptyMediaExt, [MT]);
- AssertExcept(DoMediaTypesFindInvalidMediaExt,
- EBrookMediaTypes, Format(SBrookInvalidMediaExt, ['.']), [MT]);
- AssertExcept(DoMediaTypesFindEmptyMediaType,
- EArgumentException, SBrookEmptyMediaType, [MT]);
- AssertExcept(DoMediaTypesFindInvalidMediaType,
- EBrookMediaTypes, Format(SBrookInvalidMediaType, ['bar']), [MT]);
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesCount;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(MT.Count = 0);
- MT.Add('.foo', 'bar/foo');
- MT.Add('.bar', 'foo/bar');
- Assert(MT.Count = 2);
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesClear;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(MT.Count = 0);
- MT.Add('.foo', 'bar/foo');
- MT.Add('.bar', 'foo/bar');
- Assert(MT.Count = 2);
- MT.Clear;
- Assert(MT.Count = 0);
- finally
- MT.Free;
- end;
- end;
- procedure DoMediaTypesDefaultTypeEmptyMediaType(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).DefaultType := '';
- end;
- procedure DoMediaTypesDefaultTypeInvalidMediaType(const AArgs: array of const);
- begin
- TFakeMediaTypes(AArgs[0].VObject).DefaultType := 'bar';
- end;
- procedure Test_MediaTypesDefaultType;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(MT.DefaultType = BROOK_CT_OCTET_STREAM);
- MT.DefaultType := 'foo/bar';
- Assert(MT.DefaultType = 'foo/bar');
- AssertExcept(DoMediaTypesDefaultTypeEmptyMediaType,
- EArgumentException, SBrookEmptyMediaType, [MT]);
- AssertExcept(DoMediaTypesDefaultTypeInvalidMediaType,
- EBrookMediaTypes, Format(SBrookInvalidMediaType, ['bar']), [MT]);
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesPrepared;
- var
- MT: TFakeMediaTypes;
- begin
- MT := TFakeMediaTypes.Create;
- try
- Assert(not MT.Prepared);
- MT.Prepare;
- Assert(MT.Prepared);
- finally
- MT.Free;
- end
- end;
- procedure DoMediaTypesParserCreateParamIsNilReader(const AArgs: array of const);
- begin
- TBrookMediaTypesParser.Create(nil, TBrookMediaTypes(AArgs[0].VObject));
- end;
- procedure DoMediaTypesParserCreateParamIsNilTypes(const AArgs: array of const);
- begin
- TBrookMediaTypesParser.Create(TBrookTextReader(AArgs[0].VObject), nil);
- end;
- procedure Test_MediaTypesParserCreate;
- var
- R: TBrookTextReader;
- T: TBrookMediaTypes;
- P: TBrookMediaTypesParser;
- begin
- R := TBrookStringReader.Create('');
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(Assigned(P.Reader) and (P.Reader = R));
- Assert(Assigned(P.Types) and (P.Types = T));
- AssertExcept(DoMediaTypesParserCreateParamIsNilReader,
- EArgumentNilException, Format(SParamIsNil, ['AReader']), [T]);
- AssertExcept(DoMediaTypesParserCreateParamIsNilTypes,
- EArgumentNilException, Format(SParamIsNil, ['ATypes']), [R]);
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- end;
- procedure DoMediaTypesParserParseInvalidMediaType(const AArgs: array of const);
- begin
- TBrookMediaTypesParser(AArgs[0].VObject).Parse;
- end;
- procedure DoMediaTypesParserParseInvalidMediaExt(const AArgs: array of const);
- begin
- TBrookMediaTypesParser(AArgs[0].VObject).Parse;
- end;
- procedure Test_MediaTypesParserParse;
- var
- R: TBrookTextReader;
- T: TBrookMediaTypes;
- P: TBrookMediaTypesParser;
- begin
- R := TBrookFileReader.Create(FAKE_MIME_TYPES_FILE);
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- P.Parse;
- Assert(P.Types.Count = 7);
- Assert(P.Types.Find('.json') = 'application/json');
- Assert(P.Types.Find('.html') = 'text/html');
- Assert(P.Types.Find('.htm') = 'text/html');
- Assert(P.Types.Find('.pas') = 'text/x-pascal');
- Assert(P.Types.Find('.p') = 'text/x-pascal');
- Assert(P.Types.Find('.jpgv') = 'video/jpeg');
- Assert(P.Types.Find('.foobar') = P.Types.DefaultType);
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- R := TBrookFileReader.Create(MIME_TYPES_FILE);
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- P.Parse;
- Assert(P.Types.Count = 1239);
- Assert(P.Types.Find('.txt') = 'text/plain');
- Assert(P.Types.Find('.html') = 'text/html');
- Assert(P.Types.Find('.htm') = 'text/html');
- Assert(P.Types.Find('.pas') = 'text/x-pascal');
- Assert(P.Types.Find('.p') = 'text/x-pascal');
- Assert(P.Types.Find('.xhtml') = 'application/xhtml+xml');
- Assert(P.Types.Find('.json') = 'application/json');
- Assert(P.Types.Find('.pdf') = 'application/pdf');
- Assert(P.Types.Find('.foobar') = P.Types.DefaultType);
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- R := TBrookStringReader.Create(Concat(
- 'text/plain', #9, #9, #9,'txt', sLineBreak,
- 'text/html', #9, #9, #9,'html htm xhtml', sLineBreak,
- 'application/json', #9, #9, #9,'json', sLineBreak,
- '#application/docbook+xml', #9, #9, #9,'docbook dbk', sLineBreak,
- 'application/pdf', #9, #9, #9,'pdf', sLineBreak,
- 'foo bar', sLineBreak,
- 'foo=bar'
- ));
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- P.Parse;
- Assert(P.Types.Count = 6);
- Assert(P.Types.Find('.txt') = 'text/plain');
- Assert(P.Types.Find('.html') = 'text/html');
- Assert(P.Types.Find('.htm') = 'text/html');
- Assert(P.Types.Find('.xhtml') = 'text/html');
- Assert(P.Types.Find('.json') = 'application/json');
- Assert(P.Types.Find('.docbook') = P.Types.DefaultType);
- Assert(P.Types.Find('.pdf') = 'application/pdf');
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- R := TBrookStringReader.Create(Concat(
- 'text/plain', #9, 'txt', sLineBreak,
- 'application/json', #9, 'json', sLineBreak,
- 'foo', #9, 'bar'
- ));
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- AssertExcept(DoMediaTypesParserParseInvalidMediaType,
- EBrookMediaTypes, Format(SBrookInvalidMediaType, ['foo']), [P]);
- Assert(P.Types.Count = 2);
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- R := TBrookStringReader.Create(Concat(
- 'text/plain', #9, 'txt', sLineBreak,
- 'application/json', #9, 'json', sLineBreak,
- 'foo/bar', #9, '.'
- ));
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- AssertExcept(DoMediaTypesParserParseInvalidMediaExt,
- EBrookMediaTypes, Format(SBrookInvalidMediaExt, ['.']), [P]);
- Assert(P.Types.Count = 2);
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- end;
- procedure Test_MediaTypesParserReader;
- var
- R: TBrookTextReader;
- T: TBrookMediaTypes;
- P: TBrookMediaTypesParser;
- begin
- R := TBrookStringReader.Create('');
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(Assigned(P.Reader) and (P.Reader = R));
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- end;
- procedure Test_MediaTypesParserTypes;
- var
- R: TBrookTextReader;
- T: TBrookMediaTypes;
- P: TBrookMediaTypesParser;
- begin
- R := TBrookStringReader.Create('');
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParser.Create(R, T);
- try
- Assert(Assigned(P.Types) and (P.Types = T));
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- end;
- procedure DoMediaTypesParserNginxParseInvalidMediaType(const AArgs: array of const);
- begin
- TBrookMediaTypesParserNginx(AArgs[0].VObject).Parse;
- end;
- procedure DoMediaTypesParserNginxParseInvalidMediaExt(const AArgs: array of const);
- begin
- TBrookMediaTypesParserNginx(AArgs[0].VObject).Parse;
- end;
- procedure Test_MediaTypesParserNginxParse;
- var
- R: TBrookTextReader;
- T: TBrookMediaTypes;
- P: TBrookMediaTypesParser;
- begin
- R := TBrookStringReader.Create(Concat(
- sLineBreak,
- 'types {', sLineBreak,
- ' text/html html htm shtml;', sLineBreak,
- sLineBreak,
- ' text/css css;', sLineBreak,
- '# text/xml xml;', sLineBreak,
- ' application/javascript js;', sLineBreak,
- '# application/atom+xml atom;', sLineBreak,
- ' application/rss+xml rss;', sLineBreak,
- '}', sLineBreak
- ));
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParserNginx.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- P.Parse;
- Assert(P.Types.Count = 6);
- Assert(P.Types.Find('.html') = 'text/html');
- Assert(P.Types.Find('.htm') = 'text/html');
- Assert(P.Types.Find('.shtml') = 'text/html');
- Assert(P.Types.Find('.js') = 'application/javascript');
- Assert(P.Types.Find('.xml') = P.Types.DefaultType);
- Assert(P.Types.Find('.rss') = 'application/rss+xml');
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- R := TBrookStringReader.Create(Concat(
- 'types {', sLineBreak,
- ' text/html html;', sLineBreak,
- ' text/css css;', sLineBreak,
- ' foo bar;', sLineBreak,
- '}'
- ));
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParserNginx.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- AssertExcept(DoMediaTypesParserNginxParseInvalidMediaType,
- EBrookMediaTypes, Format(SBrookInvalidMediaType, ['foo']), [P]);
- Assert(P.Types.Count = 2);
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- R := TBrookStringReader.Create(Concat(
- 'types {', sLineBreak,
- ' text/html html;', sLineBreak,
- ' text/css css;', sLineBreak,
- ' foo .;', sLineBreak,
- '}'
- ));
- T := TFakeMediaTypes.Create;
- P := TBrookMediaTypesParserNginx.Create(R, T);
- try
- Assert(P.Types.Count = 0);
- AssertExcept(DoMediaTypesParserNginxParseInvalidMediaExt,
- EBrookMediaTypes, Format(SBrookInvalidMediaExt, ['.']), [P]);
- Assert(P.Types.Count = 2);
- finally
- T.Free;
- R.Free;
- P.Free;
- end;
- end;
- procedure DoMediaTypesPathCreateFOpenError(const AArgs: array of const);
- begin
- TBrookMediaTypesPath.Create(string(AArgs[0].VPChar));
- end;
- procedure Test_MediaTypesPathCreate;
- var
- MT: TBrookMediaTypesPath;
- begin
- MT := TBrookMediaTypesPath.Create(MIME_TYPES_FILE);
- try
- Assert(MT.FileName = MIME_TYPES_FILE);
- Assert(Assigned(MT.Reader));
- Assert(Assigned(MT.Parser));
- finally
- MT.Free;
- end;
- MT := TBrookMediaTypesPath.Create;
- try
- Assert(MT.FileName = MIMEFileName);
- Assert(Assigned(MT.Reader));
- Assert(Assigned(MT.Parser));
- finally
- MT.Free;
- end;
- AssertExcept(DoMediaTypesPathCreateFOpenError, EFOpenError, [PChar('')]);
- DeleteFile('foo');
- AssertExcept(DoMediaTypesPathCreateFOpenError, EFOpenError, [PChar('foo')]);
- end;
- procedure Test_MediaTypesPathGetDescription;
- begin
- Assert(TBrookMediaTypesPath.GetDescription = 'Path');
- end;
- procedure Test_MediaTypesPathGetFileName;
- begin
- Assert(TBrookMediaTypesPath.GetFileName = MIMEFileName);
- end;
- procedure Test_MediaTypesPathPrepare;
- var
- MT: TBrookMediaTypesPath;
- begin
- MT := TBrookMediaTypesPath.Create(MIME_TYPES_FILE);
- try
- Assert(not MT.Prepared);
- MT.Prepare;
- Assert(MT.Prepared);
- Assert(MT.Find('.txt') = 'text/plain');
- Assert(MT.Find('.html') = 'text/html');
- Assert(MT.Find('.json') = 'application/json');
- Assert(MT.Find('.pdf') = 'application/pdf');
- Assert(MT.Find('.xxx') = MT.DefaultType);
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesPathClear;
- var
- MT: TBrookMediaTypesPath;
- begin
- MT := TBrookMediaTypesPath.Create(MIME_TYPES_FILE);
- try
- Assert(MT.Count = 0);
- MT.Prepare;
- Assert(MT.Count > 0);
- MT.Clear;
- Assert(MT.Count = 0);
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesPathReader;
- var
- MT: TBrookMediaTypesPath;
- begin
- MT := TBrookMediaTypesPath.Create(MIME_TYPES_FILE);
- try
- Assert(Assigned(MT.Reader));
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesPathParser;
- var
- MT: TBrookMediaTypesPath;
- begin
- MT := TBrookMediaTypesPath.Create(MIME_TYPES_FILE);
- try
- Assert(Assigned(MT.Parser));
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesPathFileName;
- var
- MT: TBrookMediaTypesPath;
- begin
- MT := TBrookMediaTypesPath.Create(MIMEFileName);
- try
- Assert(MT.FileName = MIMEFileName);
- finally
- MT.Free;
- end;
- end;
- procedure Test_MediaTypesApacheGetDescription;
- begin
- Assert(TBrookMediaTypesApache.GetDescription = 'Apache');
- end;
- procedure Test_MediaTypesNginxGetDescription;
- begin
- Assert(TBrookMediaTypesNginx.GetDescription = 'Nginx');
- end;
- procedure Test_MediaTypesNginxGetFileName;
- begin
- Assert(TBrookMediaTypesNginx.GetFileName = Concat(
- {$IFDEF UNIX}'/etc/nginx/'{$ELSE}ExtractFilePath(ParamStr(0)){$ENDIF},
- BROOK_MIME_FILE));
- end;
- procedure Test_MediaTypesWindowsGetDescription;
- begin
- Assert(TBrookMediaTypesWindows.GetDescription = 'Windows');
- end;
- procedure Test_MediaTypesUnixGetDescription;
- begin
- Assert(TBrookMediaTypesUnix.GetDescription = 'Unix');
- end;
- function LocalMediaTypesUnixGetFileName: TFileName;
- var
- FNs: TArray<TFileName>;
- FN: TFileName;
- begin
- FNs := TArray<TFileName>.Create(
- Concat('/etc/', BROOK_MIME_FILE)
- // Put other 'mime.types' paths here...
- );
- for FN in FNs do
- if FileExists(FN) then
- Exit(FN);
- Result := BROOK_MIME_FILE;
- end;
- procedure Test_MediaTypesUnixGetFileName;
- begin
- Assert(TBrookMediaTypesUnix.GetFileName = LocalMediaTypesUnixGetFileName);
- end;
- procedure Test_MIMECreate;
- var
- M: TBrookMIME;
- C: TComponent;
- begin
- C := TComponent.Create(nil);
- M := TBrookMIME.Create(C);
- try
- Assert(M.Owner = C);
- M.Open;
- Assert(M.Active);
- TBrookLibraryLoader.Unload;
- Assert(not M.Active);
- TBrookLibraryLoader.Load;
- Assert(M.DefaultType = BROOK_CT_OCTET_STREAM);
- Assert(M.FileName = MIMEFileName);
- Assert(M.Provider = BROOK_MIME_PROVIDER);
- finally
- M.Free;
- C.Free;
- end;
- end;
- procedure Test_MIMEGetProviderClass;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- M.Provider := 'Nginx';
- M.Open;
- Assert(M.GetProviderClass = TBrookMediaTypesNginx);
- M.Close;
- M.Provider := 'Apache';
- M.Open;
- Assert(M.GetProviderClass = TBrookMediaTypesApache);
- finally
- M.Free;
- end;
- end;
- procedure Test_MIMEOpen;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- Assert(not M.Active);
- M.Open;
- Assert(M.Active);
- finally
- M.Free;
- end;
- end;
- procedure Test_MIMEClose;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- Assert(not M.Active);
- M.Open;
- Assert(M.Active);
- M.Close;
- Assert(not M.Active);
- finally
- M.Free;
- end;
- end;
- procedure Test_MIMETypes;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- M.Provider := BROOK_MIME_PROVIDER;
- M.Open;
- {$IF DEFINED(MSWINDOWS)}
- Assert(M.Types is TBrookMediaTypesWindows);
- {$ELSEIF DEFINED(UNIX)}
- Assert(M.Types is TBrookMediaTypesUnix);
- {$ELSE}
- Assert(M.Types is TBrookMediaTypesPath);
- {$ENDIF}
- finally
- M.Free;
- end;
- end;
- procedure Test_MIMEActive;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- Assert(not M.Active);
- M.Active := not M.Active;
- Assert(M.Active);
- M.Active := False;
- M.Provider := 'Fake';
- RegisterClassAlias(TFakeMediaTypes, TFakeMediaTypes.GetRegisterAlias);
- M.Active := True;
- Assert(M.Types is TFakeMediaTypes);
- UnRegisterClass(TFakeMediaTypes);
- M.Active := False;
- M.Provider := 'Unix';
- M.Active := True;
- Assert(M.Types is TBrookMediaTypesUnix);
- Assert(M.DefaultType = BROOK_CT_OCTET_STREAM);
- finally
- M.Free;
- end;
- end;
- procedure Test_MIMEDefaultType;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- Assert(M.DefaultType = BROOK_CT_OCTET_STREAM);
- M.DefaultType := '';
- Assert(M.DefaultType = BROOK_CT_OCTET_STREAM);
- M.DefaultType := 'abc';
- Assert(M.DefaultType = BROOK_CT_OCTET_STREAM);
- M.DefaultType := 'text/plain';
- Assert(M.DefaultType = 'text/plain');
- finally
- M.Free;
- end;
- end;
- procedure Test_MIMEFileName;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- Assert(M.FileName = MIMEFileName);
- M.FileName := '';
- Assert(M.FileName = MIMEFileName);
- M.FileName := 'abc';
- Assert(M.FileName = 'abc');
- M.FileName := MIME_TYPES_FILE;
- Assert(M.FileName = MIME_TYPES_FILE);
- finally
- M.Free;
- end;
- end;
- procedure Test_MIMEProvider;
- var
- M: TBrookMIME;
- begin
- M := TBrookMIME.Create(nil);
- try
- Assert(M.Provider = BROOK_MIME_PROVIDER);
- M.Provider := '';
- Assert(M.Provider = BROOK_MIME_PROVIDER);
- M.Provider := 'abc';
- Assert(M.Provider = 'abc');
- M.Provider := 'Unix';
- Assert(M.Provider = 'Unix');
- finally
- M.Free;
- end;
- end;
- var
- F: TFileName;
- begin
- {$IF (NOT DEFINED(FPC)) AND DEFINED(DEBUG)}
- ReportMemoryLeaksOnShutdown := True;
- {$ENDIF}
- MIMEFileName := Concat(
- {$IFDEF UNIX}'/etc/'{$ELSE}ExtractFilePath(ParamStr(0)){$ENDIF},
- BROOK_MIME_FILE);
- TBrookLibraryLoader.Load;
- F := Concat(ExtractFilePath(ParamStr(0)), BROOK_MIME_FILE);
- {$IFDEF FPC}
- CopyFile(MIME_TYPES_FILE, F);
- {$ELSE}
- TFile.Copy(MIME_TYPES_FILE, F, True);
- {$ENDIF}
- try
- Test_MediaTypesCreate;
- // Test_MediaTypesDestroy - not required
- Test_MediaTypesGetRegisterAlias;
- Test_MediaTypesGetDescription;
- Test_MediaTypesIsValid;
- Test_MediaTypesIsText;
- Test_MediaTypesIsExt;
- Test_MediaTypesNormalizeExt;
- Test_MediaTypesPrepare;
- Test_MediaTypesAdd;
- Test_MediaTypesRemove;
- Test_MediaTypesTryType;
- Test_MediaTypesFind;
- Test_MediaTypesCount;
- Test_MediaTypesClear;
- Test_MediaTypesDefaultType;
- Test_MediaTypesPrepared;
- Test_MediaTypesParserCreate;
- Test_MediaTypesParserParse;
- Test_MediaTypesParserReader;
- Test_MediaTypesParserTypes;
- Test_MediaTypesParserNginxParse;
- Test_MediaTypesPathCreate;
- // Test_MediaTypesPathDestroy - not required
- Test_MediaTypesPathGetDescription;
- Test_MediaTypesPathGetFileName;
- Test_MediaTypesPathPrepare;
- Test_MediaTypesPathClear;
- Test_MediaTypesPathReader;
- Test_MediaTypesPathParser;
- Test_MediaTypesPathFileName;
- Test_MediaTypesApacheGetDescription;
- Test_MediaTypesNginxGetDescription;
- Test_MediaTypesNginxGetFileName;
- Test_MediaTypesWindowsGetDescription;
- Test_MediaTypesUnixGetDescription;
- Test_MediaTypesUnixGetFileName;
- Test_MIMECreate;
- // Test_MIMEDestroy - not required
- Test_MIMEGetProviderClass;
- Test_MIMEOpen;
- Test_MIMEClose;
- Test_MIMETypes;
- Test_MIMEActive;
- Test_MIMEDefaultType;
- Test_MIMEFileName;
- Test_MIMEProvider;
- finally
- DeleteFile(F);
- end;
- end.
|