| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- unit utOpenAPIWriter;
- {$mode objfpc}{$H+}
- interface
- uses
- Classes, SysUtils, fpcunit, testregistry, fpjson, fpjson.schema.schema, fpopenapi.objects, fpopenapi.writer;
- type
- { TTestOpenApiWriterBase }
- TTestOpenApiWriterBase = class(TTestCase)
- private
- FDocument: TJSONObject;
- FOpenAPI: TOpenAPI;
- procedure AddSource(Const aFmt : string; aArgs : Array of const);
- Procedure AddSource(Const aLine : string);
- procedure AddDecl(Const aFmt : string; aArgs : Array of const);
- protected
- procedure SetUp; override;
- procedure TearDown; override;
- Procedure WriteOpenAPI;
- procedure CheckJSON(const Msg: String; aJSON: TJSONStringType);
- procedure TestWrite(const Msg: String; aJSON: TJSONStringType);
- Public
- Property OpenApi : TOpenAPI Read FOpenAPI;
- Property Document : TJSONObject Read FDocument;
- published
- procedure TestHookUp;
- end;
- { TTestOpenApiWriter }
- TTestOpenApiWriterOpenAPI = class(TTestOpenApiWriterBase)
- Published
- procedure TestEmpty;
- Procedure TestOpenAPI;
- Procedure TestJSONSchemaDialect;
- end;
- TTestOpenApiWriterInfo = class(TTestOpenApiWriterBase)
- Published
- procedure TestVersion;
- procedure TestTitle;
- procedure TestSummary;
- procedure TestDescription;
- procedure TestTermsOfService;
- procedure TestContactEmail;
- procedure TestContactName;
- procedure TestContactURL;
- procedure TestLicenseUrl;
- procedure TestLicenseIdentifier;
- procedure TestLicenseName;
- end;
- TTestOpenApiWriterTags = class(TTestOpenApiWriterBase)
- Published
- Procedure TestName;
- Procedure TestDescription;
- Procedure TestExternalDocs;
- Procedure Test2Tags;
- end;
- TTestOpenApiWriterExternalDocs = class(TTestOpenApiWriterBase)
- Published
- procedure TestUrl;
- procedure TestDescription;
- end;
- { TTestOpenApiWriterServers }
- TTestOpenApiWriterServers = class(TTestOpenApiWriterBase)
- Published
- Procedure TestUrl;
- Procedure TestDescription;
- procedure TestVariablesDefault;
- procedure TestVariablesDescription;
- procedure TestVariablesEnum;
- Procedure Test2Url;
- end;
- { TTestOpenApiWriterPaths }
- TTestOpenApiWriterPathBase = class(TTestOpenApiWriterBase)
- Public
- Function AddPath(aPath : String) : TPathItem;
- end;
- TTestOpenApiWriterPaths = class(TTestOpenApiWriterPathBase)
- Published
- Procedure TestEmpty;
- Procedure TestRef;
- Procedure TestSummary;
- Procedure TestDescription;
- Procedure TestGetEmpty;
- Procedure TestGetSummary;
- Procedure TestGetDescription;
- Procedure TestGetOperationId;
- Procedure TestGetDeprecated;
- Procedure TestGetExternalDocsEmpty;
- Procedure TestGetExternalDocsURL;
- Procedure TestGetServers;
- end;
- { TTestOpenApiWriterPathRequestBody }
- TTestOpenApiWriterPathRequestBody = class(TTestOpenApiWriterPathBase)
- Public
- Function AddBody(aPath : String) : TRequestBody;
- Published
- procedure TestEmpty;
- procedure TestDescription;
- procedure TestRequired;
- procedure TestContent;
- procedure TestContentSchema;
- procedure TestContentExample;
- procedure TestContentExamples;
- procedure TestContentEncodingContentType;
- procedure TestContentEncodingExplode;
- procedure TestContentEncodingStyle;
- procedure TestContentEncodingAllowReserved;
- procedure TestContentEncodingHeaders;
- end;
- { TTestOpenApiWriterPathParameters }
- TTestOpenApiWriterPathParameters = class(TTestOpenApiWriterPathBase)
- Public
- Function AddParam(aPath : String) : TParameter;
- Published
- Procedure TestGetParametersEmpty;
- end;
- { TTestOpenApiWriterPathResponses }
- TTestOpenApiWriterPathResponses = class(TTestOpenApiWriterPathBase)
- Public
- Function AddResponse(aPath : String) : TResponse;
- Published
- procedure TestEmpty;
- procedure TestHeaders;
- procedure TestDescription;
- procedure TestContentEmpty;
- procedure TestContentSchema;
- procedure TestContentExample;
- procedure TestContentExamples;
- procedure TestContentEncodingEmpty;
- procedure TestContentEncodingContentType;
- end;
- { TTestOpenApiWriterPathResponsesLinks }
- TTestOpenApiWriterPathResponsesLinks = class(TTestOpenApiWriterPathBase)
- Public
- Function AddLink(aPath : String) : TLink;
- Published
- procedure TestEmpty;
- Procedure TestOperatonRef;
- Procedure TestOperatonId;
- Procedure TestParameters;
- Procedure TestRequestBody;
- Procedure TestDescription;
- Procedure TestServer;
- end;
- { TTestOpenApiWriterPathSecurity }
- TTestOpenApiWriterPathSecurity = class(TTestOpenApiWriterPathBase)
- Published
- Procedure TestEmpty;
- Procedure TestAPIKey;
- end;
- { TTestOpenApiWriterWebHooks }
- TTestOpenApiWriterWebHooks = class(TTestOpenApiWriterBase)
- Public
- function AddWebHook(const aname : string) : TPathItem;
- Published
- procedure TestEmpty;
- procedure TestSummary;
- // At the moment, no more tests needed: the items are identical to path items.
- end;
- { TTestOpenApiWriterComponents }
- TTestOpenApiWriterComponents = class(TTestOpenApiWriterBase)
- Public
- function AddComponentSchema(const aname: string): TJSONSchema;
- Published
- procedure TestSchemaEmpty;
- procedure TestSchemaContentEncoding;
- end;
- { TTestOpenApiWriterOAuth2Scopes }
- TTestOpenApiWriterOAuth2Scopes = class(TTestOpenApiWriterBase)
- Published
- procedure TestOAuth2ScopesAsObject;
- procedure TestOAuth2ScopesEmpty;
- end;
- { TTestOpenApiWriterSecurity }
- TTestOpenApiWriterSecurity = class(TTestOpenApiWriterBase)
- Published
- procedure TestOne;
- end;
- (*
- property Components: TComponents Read Get_Components write Set_Components;
- property Security: TSecurityRequirementList Read Get_Security write Set_Security;
- *)
- implementation
- uses jsoncomparer;
- var
- _Sources : TStrings;
- _Decl : TStrings;
- _LastClass : String;
- function ChangeRead(S : String) : String;
- begin
- Result:=StringReplace(S,'Write','Reade',[rfReplaceAll,rfIgnoreCase]);
- end;
- procedure StartSources; forward;
- Procedure AddMethod(aClassName,aTestName : string; const aJSON : String);
- var
- lClass,lTest : String;
- J,S : TJSONObject;
- D : TJSONData;
- P,N : String;
- begin
- lClass:=ChangeRead(aClassName);
- lTest:=ChangeRead(aTestName);
- if lClass<>_LastClass then
- begin
- if _LastClass<>'' then
- _Decl.Add(' end;');
- _Decl.Add('');
- _Decl.Add(' %s = Class(TOpenAPITestRead)',[lClass]);
- _LastClass:=lClass;
- end;
- _Decl.Add(' Procedure %s;',[lTest]);
- _Sources.Add('');
- _Sources.Add('procedure %s.%s;',[lClass,lTest]);
- _Sources.Add('');
- _Sources.Add('begin');
- _Sources.Add(' TestRead(''%s'');',[aJSON]);
- J:=GetJSON(aJSON) as TJSONObject;
- P:='OpenAPI';
- S:=Nil;
- try
- if J.Count>0 then
- begin
- D:=J.Items[0];
- if D is TJSONObject then
- S:=TJSONObject(D);
- N:=J.Names[0];
- While S is TJSONObject do
- begin
- P:=P+'.'+N;
- _Sources.Add(' AssertNotNull(''%s'',%s);',[P,P]);
- if S.Count=0 then
- begin
- D:=Nil;
- S:=Nil;
- Continue;
- end;
- D:=S.Items[0];
- N:=S.Names[0];
- if (D is TJSONArray) and (TJSONArray(D).Count>0) then
- begin
- D:=TJSONArray(D)[0];
- N:=N+'[0]';
- end;
- if D is TJSONObject then
- S:=TJSONObject(D)
- else
- S:=Nil;
- end;
- P:=P+'.'+N;
- if assigned(D) then
- Case D.JSONType of
- jtString : _Sources.Add(' AssertEquals(''%s'',''%s'',%s);',[P,D.AsString,P]);
- jtBoolean : _Sources.Add(' AssertEquals(''%s'',%s,%s);',[P,D.AsString,P]);
- jtNumber : _Sources.Add(' AssertEquals(''%s'',%s,%s);',[P,D.AsString,P]);
- end;
- end;
- finally
- J.Free;
- end;
- _Sources.Add('end;');
- _Sources.Add('');
- end;
- procedure TTestOpenApiWriterBase.AddSource(const aFmt: string; aArgs: array of const);
- begin
- AddSource(Format(aFmt,aArgs));
- end;
- procedure TTestOpenApiWriterBase.AddSource(const aLine: string);
- begin
- _Sources.Add(aLine);
- end;
- procedure TTestOpenApiWriterBase.AddDecl(const aFmt: string; aArgs: array of const);
- begin
- _Decl.Add(aFmt,aArgs);
- end;
- procedure TTestOpenApiWriterBase.SetUp;
- begin
- FOpenAPI:=TOpenAPI.Create;
- if _Decl=Nil then
- StartSources;
- end;
- procedure TTestOpenApiWriterBase.TearDown;
- begin
- FreeAndNil(FOpenAPI);
- FreeAndNil(FDocument);
- end;
- procedure TTestOpenApiWriterBase.WriteOpenAPI;
- var
- W : TOpenAPIWriter;
- begin
- W:=TOpenAPIWriter.Create(Nil);
- try
- FDocument:=W.WriteToJSON(OpenAPI);
- finally
- W.Free;
- end;
- end;
- procedure TTestOpenApiWriterBase.CheckJSON(const Msg: String; aJSON: TJSONStringType);
- var
- Data : TJSONData;
- Obj : TJSONObject absolute data;
- Errors : TStrings;
- begin
- Errors:=Nil;
- Data:=GetJSON(aJSON,true) as TJSONObject;
- try
- AssertEquals('Json object',TJSONObject,Data.ClassType);
- Errors:=TStringList.Create;
- // Writeln('Comparing ',Obj.AsJSON,' === ',Document.AsJSON);
- TJSONComparer.Compare(Obj,Document,Errors);
- if (Errors.Count<>0) then
- Fail(Msg+':'#10+Errors.Text);
- finally
- Errors.Free;
- Data.Free;
- end;
- end;
- procedure TTestOpenApiWriterBase.TestWrite(const Msg: String; aJSON: TJSONStringType);
- begin
- AddMethod(ClassName,TestName,aJSON);
- WriteOpenAPI;
- CheckJSON(msg,aJSON);
- end;
- procedure TTestOpenApiWriterBase.TestHookUp;
- begin
- AssertNotNull('have openapi',OpenAPI);
- AssertNull('have no document',Document);
- end;
- procedure TTestopenApiWriterOpenApi.TestEmpty;
- begin
- WriteOpenApi;
- CheckJSON('Empty','{}');
- end;
- procedure TTestopenApiWriterOpenAPI.TestOpenAPI;
- begin
- OpenAPI.OpenAPI:='123';
- TestWrite('openapi version','{ "openapi" : "123" }');
- end;
- procedure TTestopenApiWriterOpenAPI.TestJSONSchemaDialect;
- begin
- OpenAPI.JSONSchemaDialect:='123';
- TestWrite('json schema dialect','{ "jsonSchemaDialect" : "123" }');
- end;
- procedure TTestopenApiWriterInfo.TestVersion;
- begin
- OpenAPI.Info.Version:='123';
- TestWrite('Version','{ "info" : { "version" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestTitle;
- begin
- OpenAPI.Info.title:='123';
- TestWrite('Title','{ "info" : { "title" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestSummary;
- begin
- OpenAPI.Info.summary:='123';
- TestWrite('Summary','{ "info" : { "summary" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestDescription;
- begin
- OpenAPI.Info.Description:='123';
- TestWrite('Description','{ "info" : { "description" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestTermsOfService;
- begin
- OpenAPI.Info.TermsOfService:='123';
- TestWrite('TermsOfService','{ "info" : { "termsOfService" : "123" } }');
- end;
- procedure TTestopenApiWriterInfo.TestContactEmail;
- begin
- OpenAPI.Info.Contact.Email:='123';
- TestWrite('Contact mail','{ "info" : { "contact" : { "email" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestContactName;
- begin
- OpenAPI.Info.Contact.Name:='123';
- TestWrite('Contact name','{ "info" : { "contact" : { "name" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestContactURL;
- begin
- OpenAPI.Info.Contact.URL:='123';
- TestWrite('Contact url','{ "info" : { "contact" : { "url" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestLicenseUrl;
- begin
- OpenAPI.Info.License.Url:='123';
- TestWrite('license url','{ "info" : { "license" : { "url" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestLicenseName;
- begin
- OpenAPI.Info.License.Name:='123';
- TestWrite('license name','{ "info" : { "license" : { "name" : "123" } } }');
- end;
- procedure TTestopenApiWriterInfo.TestLicenseIdentifier;
- begin
- OpenAPI.Info.License.Identifier:='123';
- TestWrite('license name','{ "info" : { "license" : { "identifier" : "123" } } }');
- end;
- procedure TTestopenApiWriterTags.TestName;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[0]');
- T.Name:='123';
- OpenAPI.Tags.Add(T);
- TestWrite('name','{ "tags" : [ { "name" : "123" } ] }');
- end;
- procedure TTestopenApiWriterTags.TestDescription;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[1]');
- T.Description:='123';
- OpenAPI.Tags.Add(T);
- TestWrite('description','{ "tags" : [ { "description" : "123" } ] }');
- end;
- procedure TTestopenApiWriterTags.TestExternalDocs;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[1]');
- T.ExternalDocs.Url:='123';
- OpenAPI.Tags.Add(T);
- TestWrite('externaldocs','{ "tags" : [ { "externalDocs" : { "url" :"123" } } ] }');
- end;
- procedure TTestopenApiWriterTags.Test2Tags;
- var
- T : TTag;
- begin
- T:=TTag.Create(OpenAPI,'tags[0]');
- T.Name:='123';
- OpenAPI.Tags.Add(T);
- T:=TTag.Create(OpenAPI,'tags[1]');
- T.Name:='456';
- OpenAPI.Tags.Add(T);
- TestWrite('multi','{ "tags" : [ { "name" : "123" }, { "name" : "456" } ] }');
- end;
- procedure TTestopenApiWriterExternalDocs.TestUrl;
- begin
- OpenAPI.ExternalDocs.Url:='123';
- TestWrite('Url','{ "externalDocs" : { "url" :"123" } }');
- end;
- procedure TTestopenApiWriterExternalDocs.TestDescription;
- begin
- OpenAPI.ExternalDocs.Description:='123';
- TestWrite('description','{ "externalDocs" : { "description" :"123" } }');
- end;
- { TTestOpenApiWriterServers }
- procedure TTestOpenApiWriterServers.TestUrl;
- var
- Server : TServer;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.Url:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('url','{ "servers" : [ { "url" :"123" } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestDescription;
- var
- Server : TServer;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.description:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('url','{ "servers" : [ { "description" :"123" } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestVariablesDefault;
- var
- Server : TServer;
- ServerVar : TServerVariable;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- ServerVar:=Server.Variables.AddItem('user');
- ServerVar.Default:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('var.default','{ "servers" : [ { "variables" : { "user": { "default": "123" } } } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestVariablesDescription;
- var
- Server : TServer;
- ServerVar : TServerVariable;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- ServerVar:=Server.Variables.AddItem('user');
- ServerVar.Description:='123';
- OpenAPI.Servers.Add(Server);
- TestWrite('var.description','{ "servers" : [ { "variables" : { "user": { "description": "123" } } } ] }');
- end;
- procedure TTestOpenApiWriterServers.TestVariablesEnum;
- var
- Server : TServer;
- ServerVar : TServerVariable;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- ServerVar:=Server.Variables.AddItem('user');
- ServerVar.Enum.Add('123');
- ServerVar.Enum.Add('456');
- OpenAPI.Servers.Add(Server);
- TestWrite('var.enum','{ "servers" : [ { "variables" : { "user": { "enum": [ "123", "456" ] } } } ] }');
- end;
- procedure TTestOpenApiWriterServers.Test2Url;
- var
- Server : TServer;
- begin
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.Url:='123';
- OpenAPI.Servers.Add(Server);
- Server:=TServer.Create(OpenAPI,'servers[0]');
- Server.Url:='456';
- OpenAPI.Servers.Add(Server);
- TestWrite('url','{ "servers" : [ { "url" :"123" }, { "url" :"456" } ] }');
- end;
- { TTestOpenApiWriterPaths }
- function TTestOpenApiWriterPathBase.AddPath(aPath: String): TPathItem;
- begin
- Result:=OpenAPI.Paths.AddItem(aPath);
- end;
- procedure TTestOpenApiWriterPaths.TestEmpty;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- AssertNotNull('Have path',P);
- TestWrite('Path','{ "paths" : { "api" : {} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestRef;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Ref:='abc';
- TestWrite('Path ref','{ "paths" : { "api" : { "$ref": "abc"} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestSummary;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Summary:='abc';
- TestWrite('Path summary','{ "paths" : { "api" : { "summary": "abc"} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestDescription;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.description:='abc';
- TestWrite('Path description','{ "paths" : { "api" : { "description": "abc"} } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetServers;
- var
- P : TPathItem;
- S : TServer;
- begin
- P:=AddPath('api');
- S:=TServer.Create(P,'servers[0]');
- P.Servers.Add(S);
- TestWrite('Path description','{ "paths" : { "api" : { "servers": [ {} ] } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetEmpty;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- AssertEquals('Empty','',P.Get.Summary);
- TestWrite('Path get','{ "paths" : { "api" : { "get": { } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetSummary;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.Summary:='123';
- TestWrite('Path get summary','{ "paths" : { "api" : { "get": { "summary" : "123" } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetDescription;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.Description:='123';
- TestWrite('Path get description','{ "paths" : { "api" : { "get": { "description" : "123" } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetOperationId;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.operationId:='123';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "operationId" : "123" } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetDeprecated;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.Deprecated:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "deprecated" : true } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetExternalDocsEmpty;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- AssertNotNull('Get creates',P.Get.ExternalDocs);
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "externalDocs" : { } } } } }');
- end;
- procedure TTestOpenApiWriterPaths.TestGetExternalDocsURL;
- var
- P : TPathItem;
- begin
- P:=AddPath('api');
- P.Get.ExternalDocs.Url:='123';
- TestWrite('Path get externaldocs','{ "paths" : { "api" : { "get": { "externalDocs" : { "url" : "123" } } } } }');
- end;
- { TTestOpenApiWriterPathRequestBody }
- function TTestOpenApiWriterPathRequestBody.AddBody(aPath: String): TRequestBody;
- begin
- Result:=AddPath(aPath).Get.RequestBody;
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestEmpty;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- AssertNotNull('Get creates',R);
- TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestDescription;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Description:='123';
- TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { "description" : "123" } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestRequired;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Required:=True;
- TestWrite('Path get requestbody','{ "paths" : { "api" : { "get": { "requestBody" : { "required" : true} } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContent;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123');
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentSchema;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Schema:=TJSONSchema.Create();
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "schema" : true } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentExample;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').example:=TJSONObject.Create;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "example" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentExamples;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').examples.AddItem('abc').Summary:='x';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "examples" : { "abc" : { "summary": "x"} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingContentType;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').ContentType:='def';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "contentType": "def"} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingExplode;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').Explode:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "explode": true} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingStyle;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').Style:='def';
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "style": "def"} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingAllowReserved;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').AllowReserved:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "allowReserved": true} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathRequestBody.TestContentEncodingHeaders;
- var
- R : TRequestBody;
- begin
- R:=AddBody('api');
- R.Content.AddItem('123').Encoding.AddItem('abc').Headers.AddItem('def').Explode:=True;
- TestWrite('Path get operationId','{ "paths" : { "api" : { "get": { "requestBody" : { "content" : { "123" : { "encoding" : { "abc" : { "headers": { "def" : { "explode" : true } } } } } } } } } } }');
- end;
- function TTestOpenApiWriterPathParameters.AddParam(aPath: String): TParameter;
- var
- P : TPathItem;
- begin
- P:=addPath(aPath);
- Result:=P.Parameters.AddParam(P);
- end;
- procedure TTestOpenApiWriterPathParameters.TestGetParametersEmpty;
- var
- P : TParameter;
- begin
- P:=AddParam('api');
- AssertNotNull('have p',p);
- TestWrite('Path param','{ "paths" : { "api" : { "parameters": [ {} ] } } }');
- end;
- function TTestOpenApiWriterPathResponses.AddResponse(aPath: String): TResponse;
- begin
- Result:=AddPath(aPath).Get.Responses.AddItem('default');
- end;
- procedure TTestOpenApiWriterPathResponses.TestEmpty;
- begin
- AssertNotNull('Have response',AddResponse('api'));
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : {} } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestHeaders;
- begin
- AddResponse('api').Headers.AddItem('x-content');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "headers" : { "x-content" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestDescription;
- begin
- AddResponse('api').Description:='abc';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "description" : "abc" } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentEmpty;
- begin
- AddResponse('api').content.AddItem('application/json');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentSchema;
- begin
- AssertNotNull('Have schema',AddResponse('api').content.AddItem('application/json').Schema);
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "schema" : true } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentExample;
- begin
- AddResponse('api').content.AddItem('application/json').Example:=TJSONObject.Create;
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "example" : {} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentExamples;
- begin
- AddResponse('api').content.AddItem('application/json').Examples.AddItem('abc').Summary:='x';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "examples" : { "abc" : { "summary": "x"} } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentEncodingEmpty;
- begin
- AddResponse('api').content.AddItem('application/json').Encoding.AddItem('abc'); //.ContentType:='application/json';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "encoding" : { "abc" : { } } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponses.TestContentEncodingContentType;
- begin
- AddResponse('api').content.AddItem('application/json').Encoding.AddItem('abc').ContentType:='application/json';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses": { "default" : { "content" : { "application/json" : { "encoding" : { "abc" : { "contentType" : "application/json" } } } } } } } } } }');
- end;
- { TTestOpenApiWriterPathResponsesLinks }
- function TTestOpenApiWriterPathResponsesLinks.AddLink(aPath: String): TLink;
- begin
- Result:=AddPath(aPath).Get.Responses.AddItem('default').links.AddItem('abc');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestEmpty;
- begin
- AddLink('api');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestOperatonRef;
- begin
- AddLink('api').OperationRef:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "operationRef" : "123" } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestOperatonId;
- begin
- AddLink('api').OperationId:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "operationId" : "123" } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestParameters;
- begin
- AddLink('api').Parameters.Add('x','y');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "parameters" : { "x" : "y" } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestRequestBody;
- begin
- AddLink('api').RequestBody:=TJSONObject.Create();
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "requestBody" : {} } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestDescription;
- begin
- AddLink('api').Description:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "description" : "123" } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathResponsesLinks.TestServer;
- begin
- AddLink('api').Server.Url:='123';
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "responses" : { "default" : { "links": { "abc" : { "server" : { "url" : "123" } } } } } } } } }');
- end;
- procedure TTestOpenApiWriterPathSecurity.TestEmpty;
- var
- O : TAPIOperation;
- begin
- O:=AddPath('api').Get;
- O.Security.AddSecurity(O);
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "security" : [ {} ] } } } }');
- end;
- procedure TTestOpenApiWriterPathSecurity.TestAPIKey;
- var
- O : TAPIOperation;
- begin
- O:=AddPath('api').Get;
- O.Security.AddSecurity(O).AddItem('api_key').Add('akey');
- TestWrite('Path param','{ "paths" : { "api" : { "get" : { "security" : [ { "api_key" : ["akey"] } ] } } } }');
- end;
- { TTestOpenApiWriterWebHooks }
- function TTestOpenApiWriterWebHooks.AddWebHook(const aname: string): TPathItem;
- begin
- Result:=OpenAPI.WebHooks.AddItem(aName);
- end;
- procedure TTestOpenApiWriterWebHooks.TestEmpty;
- begin
- AssertNotNull(OpenAPI.WebHooks.AddItem('newitem'));
- TestWrite('Path param','{ "webhooks" : { "newitem" : { } } }');
- end;
- procedure TTestOpenApiWriterWebHooks.TestSummary;
- begin
- OpenAPI.WebHooks.AddItem('newitem').Summary:='abc';
- TestWrite('Path param','{ "webhooks" : { "newitem" : { "summary" : "abc" } } }');
- end;
- { TTestOpenApiWriterComponents }
- function TTestOpenApiWriterComponents.AddComponentSchema(const aname: string): TJSONSchema;
- begin
- Result:=OpenAPI.Components.Schemas.Add(aName);
- end;
- procedure TTestOpenApiWriterComponents.TestSchemaEmpty;
- begin
- AssertNotNull('Schema',AddComponentSchema('abc'));
- TestWrite('Path componentschema','{ "components" : { "schemas" : { "abc" : true } } }');
- end;
- procedure TTestOpenApiWriterComponents.TestSchemaContentEncoding;
- begin
- AddComponentSchema('abc').Validations.contentEncoding:='utf8';
- TestWrite('Path componentschema','{ "components" : { "schemas" : { "abc" : { "contentEncoding" : "utf8" } } } }');
- end;
- { TTestOpenApiWriterOAuth2Scopes }
- procedure TTestOpenApiWriterOAuth2Scopes.TestOAuth2ScopesAsObject;
- var
- Scheme: TSecuritySchemeOrReference;
- Flow: TOAuthFlow;
- begin
- Scheme := OpenAPI.Components.SecuritySchemes.AddItem('oauth2');
- Scheme.Type_ := 'oauth2';
- Flow := Scheme.Flows.ClientAuthorizationCode;
- Flow.AuthorizationUrl := 'https://example.com/auth';
- Flow.TokenURL := 'https://example.com/token';
- Flow.Scopes.Add('read:user=Read user data');
- Flow.Scopes.Add('write:user=Write user data');
- TestWrite('OAuth2 scopes as object','{ "components" : { "securitySchemes" : { "oauth2" : { "type" : "oauth2", "flows" : { "authorizationCode" : { "authorizationUrl" : "https://example.com/auth", "tokenUrl" : "https://example.com/token", "scopes" : { "read:user" : "Read user data", "write:user" : "Write user data" } } } } } } }');
- end;
- procedure TTestOpenApiWriterOAuth2Scopes.TestOAuth2ScopesEmpty;
- var
- Scheme: TSecuritySchemeOrReference;
- Flow: TOAuthFlow;
- begin
- Scheme := OpenAPI.Components.SecuritySchemes.AddItem('oauth2');
- Scheme.Type_ := 'oauth2';
- Flow := Scheme.Flows.ClientAuthorizationCode;
- Flow.AuthorizationUrl := 'https://example.com/auth';
- Flow.TokenURL := 'https://example.com/token';
- // Access Scopes to ensure it's initialized but leave it empty
- AssertEquals('Scopes count', 0, Flow.Scopes.Count);
- TestWrite('OAuth2 empty scopes','{ "components" : { "securitySchemes" : { "oauth2" : { "type" : "oauth2", "flows" : { "authorizationCode" : { "authorizationUrl" : "https://example.com/auth", "tokenUrl" : "https://example.com/token", "scopes" : { } } } } } } }');
- end;
- { TTestOpenApiWriterSecurity }
- procedure TTestOpenApiWriterSecurity.TestOne;
- begin
- OpenAPI.Security.AddSecurity(OpenAPI).AddItem('abc');
- TestWrite('Security','{ "security" : [ { "abc" : [] } ] }');
- end;
- procedure StartSources;
- begin
- _Sources:=TStringList.Create;
- _Decl:=TStringList.Create;
- _Decl.Add('unit ReadTests;');
- _Decl.Add('');
- _Decl.Add('interface');
- _Decl.Add('');
- _Decl.Add('uses fpcunit;');
- _Decl.Add('');
- _Decl.Add('type');
- _Decl.Add('');
- end;
- procedure WriteSources;
- begin
- _Decl.Add(' end;');
- _Decl.Add('');
- _Decl.Add('implementation');
- _Decl.Add('');
- _Decl.AddStrings(_Sources);
- _Decl.Add('');
- _Decl.Add('end.');
- _Decl.SaveToFile('ReadTests.pp');
- _Sources.Free;
- _Decl.Free;
- end;
- initialization
- // StartSources;
- RegisterTests('Writer',[
- TTestopenApiWriterOpenAPI,
- TTestopenApiWriterInfo,
- TTestopenApiWriterTags,
- TTestOpenApiWriterExternalDocs,
- TTestOpenApiWriterServers,
- TTestOpenApiWriterPaths,
- TTestOpenApiWriterPathRequestBody,
- TTestOpenApiWriterPathParameters,
- TTestOpenApiWriterPathResponses,
- TTestOpenApiWriterPathSecurity,
- TTestOpenApiWriterPathResponsesLinks,
- TTestOpenApiWriterWebHooks,
- TTestOpenApiWriterComponents,
- TTestOpenApiWriterOAuth2Scopes,
- TTestOpenApiWriterSecurity
- ]);
- finalization
- if assigned(_Decl) then
- WriteSources;
- end.
|