2017-03-01 21:40:57 +01:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2018-08-08 17:11:45 +02:00
|
|
|
|
// Copyright (c) 2010-2018 Daniele Teti and the DMVCFramework Team
|
2017-03-01 21:40:57 +01:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
unit TestServerControllerU;
|
|
|
|
|
|
2017-06-02 00:10:31 +02:00
|
|
|
|
{$HINTS OFF}
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
interface
|
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
|
uses
|
|
|
|
|
MVCFramework,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
MVCFramework.Commons;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
|
TTestServerController = class(TMVCController)
|
|
|
|
|
private
|
|
|
|
|
FFormatSettings: TFormatSettings;
|
|
|
|
|
protected
|
|
|
|
|
procedure MVCControllerAfterCreate; override;
|
|
|
|
|
public
|
|
|
|
|
[MVCPath('/req/with/params/($par1)/($par2)/($par3)')]
|
|
|
|
|
[MVCHTTPMethod([httpGET, httpDELETE])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure ReqWithParams;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/echo/($par1)/($par2)/($par3)')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST, httpPUT, httpPATCH])]
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure EchoBody;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/session/($value)')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure SessionSet;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/session')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
procedure SessionGet;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/headers')]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure EchoHeaders;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/lotofcookies')]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure GenerateCookies;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/dataset/($datasetname)')]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure DataSetHandling;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/login/($username)')]
|
|
|
|
|
// this is only for test!!!!
|
|
|
|
|
procedure Login;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/logout')]
|
|
|
|
|
// this is only for test!!!!
|
|
|
|
|
procedure Logout;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/encoding')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
// this is only for test!!!!
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestCharset;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/testconsumes')]
|
|
|
|
|
[MVCHTTPMethod([httpGET, httpPOST, httpPUT])]
|
|
|
|
|
[MVCConsumes('application/json')]
|
|
|
|
|
[MVCProduces('application/json', 'utf-8')]
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TestConsumesProduces;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/testconsumes/textiso8859_1')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST, httpPUT])]
|
|
|
|
|
[MVCConsumes(TMVCMediaType.TEXT_PLAIN)]
|
2017-09-08 16:59:02 +02:00
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_PLAIN, TMVCCharset.ISO88591)]
|
2017-03-01 21:40:57 +01:00
|
|
|
|
procedure TestConsumesProducesTextISO8859_1;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/testconsumes')]
|
|
|
|
|
[MVCHTTPMethod([httpGET, httpPOST, httpPUT])]
|
|
|
|
|
[MVCConsumes('text/plain')]
|
|
|
|
|
[MVCProduces('text/plain')]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestConsumesProducesText;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/testconsumejson')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
[MVCConsumes('application/json')]
|
|
|
|
|
[MVCProduces('application/json', 'utf-8')]
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TestConsumeJSON;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/people/($id)')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestGetPersonByID;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/people/($id)/asfields')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestGetPersonByIDAsFields;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/customers/list')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
|
|
|
|
procedure TestJSONArrayAsObjectList;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/people')]
|
|
|
|
|
[MVCHTTPMethod([httpGET, httpPOST, httpPUT])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestGetPersons;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/wrappedpeople')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestGetWrappedPeople;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/objects')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST, httpPUT])]
|
|
|
|
|
[MVCProduces('application/json')]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestPOSTObject;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/speed')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestHelloWorld;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/path1/($id)')]
|
|
|
|
|
[MVCPath('/path2/($id)/2/($par)')]
|
|
|
|
|
[MVCPath('/path3/($id)/2/($par)/3')]
|
|
|
|
|
[MVCPath('/path4/($id)/2/($par)/3/4')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST, httpPUT])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TestMultiplePaths;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
{ Strongly typed actions }
|
|
|
|
|
[MVCPath('/typed/string1/($value)')]
|
|
|
|
|
procedure TestTypedActionString1(value: string);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/integer1/($value)')]
|
|
|
|
|
procedure TestTypedActionInteger1(value: Integer);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/int641/($value)')]
|
|
|
|
|
procedure TestTypedActionInt641(value: Int64);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/single1/($value)')]
|
|
|
|
|
procedure TestTypedActionSingle1(value: Single);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/double1/($value)')]
|
|
|
|
|
procedure TestTypedActionDouble1(value: Double);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/extended1/($value)')]
|
|
|
|
|
procedure TestTypedActionExtended1(value: Extended);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/all/($ParString)/($ParInteger)/($ParInt64)/($ParSingle)/($ParDouble)/($ParExtended)')]
|
2018-08-08 17:11:45 +02:00
|
|
|
|
procedure TestTypedActionAllTypes(ParString: string; ParInteger: Integer; ParInt64: Int64; ParSingle: Single; ParDouble: Double;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
ParExtended: Extended);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/tdatetime1/($value)')]
|
|
|
|
|
procedure TestTypedActionTDateTime1(value: TDateTime);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/tdate1/($value)')]
|
|
|
|
|
procedure TestTypedActionTDate1(value: TDate);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/ttime1/($value)')]
|
|
|
|
|
procedure TestTypedActionTTime1(value: TTime);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/typed/booleans/($bool1)/($bool2)/($bool3)/($bool4)')]
|
|
|
|
|
procedure TestTypedActionBooleans(bool1, bool2, bool3, bool4: Boolean);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/renderstreamandfreewithownerfalse')]
|
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerFalse;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/renderstreamandfreewithownertrue')]
|
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerTrue;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
[MVCPath('/stringdictionary')]
|
|
|
|
|
procedure TestStringDictionary;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/private')]
|
|
|
|
|
TTestPrivateServerController = class(TMVCController)
|
|
|
|
|
public
|
|
|
|
|
[MVCPath('/role1')]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure OnlyRole1;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
[MVCPath('/role1session')]
|
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure OnlyRole1Session;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
[MVCPath('/role2')]
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure OnlyRole2;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
|
[MVCPath('/fault')]
|
|
|
|
|
TTestFaultController = class(TMVCController)
|
|
|
|
|
public
|
|
|
|
|
[MVCPath]
|
|
|
|
|
procedure NeverExecuted;
|
|
|
|
|
constructor Create; override;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/fault2')]
|
|
|
|
|
TTestFault2Controller = class(TTestFaultController)
|
|
|
|
|
public
|
|
|
|
|
[MVCPath]
|
|
|
|
|
procedure NeverExecuted;
|
|
|
|
|
constructor Create; override;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2018-11-24 16:56:21 +01:00
|
|
|
|
System.JSON,
|
2018-08-08 17:11:45 +02:00
|
|
|
|
Web.HTTPApp,
|
|
|
|
|
BusinessObjectsU,
|
|
|
|
|
Generics.Collections,
|
|
|
|
|
MVCFramework.Serializer.Commons,
|
|
|
|
|
MVCFramework.Serializer.Defaults,
|
|
|
|
|
MVCFramework.DuckTyping,
|
|
|
|
|
System.Classes;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
|
|
|
|
{ TTestServerController }
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.DataSetHandling;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
case Context.Request.HTTPMethod of
|
2017-03-01 21:40:57 +01:00
|
|
|
|
httpGET:
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
httpPOST:
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
httpPUT:
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
httpDELETE:
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
httpHEAD:
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
httpOPTIONS:
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TTestServerController.EchoBody;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
JSON: TJSONObject;
|
|
|
|
|
begin
|
2017-09-26 01:02:09 +02:00
|
|
|
|
JSON := TJSONObject.ParseJSONValue(Context.Request.Body) as TJSONObject;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
JSON.AddPair('echo', 'from server');
|
2017-03-20 19:31:42 +01:00
|
|
|
|
Render(JSON, True);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.EchoHeaders;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Context.Response.ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(Context.Request.Headers['ACCEPT']);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.GenerateCookies;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
c: TCookie;
|
|
|
|
|
v: string;
|
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
v := Context.Request.Cookie('usersettings');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
c := Context.Response.Cookies.Add;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
c.Name := 'usersettings1';
|
|
|
|
|
c.value := 'usersettings1-value';
|
|
|
|
|
c.Path := '/usersettings1';
|
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
c := Context.Response.Cookies.Add;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
c.Name := 'usersettings2';
|
|
|
|
|
c.value := 'usersettings2-value';
|
|
|
|
|
c.Path := '/usersettings2';
|
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
c := Context.Response.Cookies.Add;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
c.Name := 'usersettings3';
|
|
|
|
|
c.value := 'usersettings3-value';
|
|
|
|
|
c.Path := '/usersettings3';
|
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
c := Context.Response.Cookies.Add;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
c.Name := 'usersettings4';
|
|
|
|
|
c.value := 'usersettings4-value';
|
|
|
|
|
c.Path := '/usersettings4';
|
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.Login;
|
|
|
|
|
begin
|
|
|
|
|
if Context.SessionStarted then
|
|
|
|
|
raise EMVCException.Create('Session already started');
|
|
|
|
|
Session['username'] := Context.Request.Params['username'];
|
|
|
|
|
if not Context.SessionStarted then
|
|
|
|
|
raise EMVCException.Create('Session still not started');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.Logout;
|
|
|
|
|
begin
|
|
|
|
|
if not Context.SessionStarted then
|
|
|
|
|
raise EMVCException.Create('Session not available');
|
|
|
|
|
Context.SessionStop(false);
|
|
|
|
|
if Context.SessionStarted then
|
|
|
|
|
raise EMVCException.Create('Session still available');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.MVCControllerAfterCreate;
|
|
|
|
|
begin
|
|
|
|
|
FFormatSettings.DecimalSeparator := '.';
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.ReqWithParams;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Render(TJSONObject.Create.AddPair('par1', Context.Request.Params['par1']).AddPair('par2', Context.Request.Params['par2']).AddPair('par3',
|
|
|
|
|
Context.Request.Params['par3']).AddPair('method', Context.Request.HTTPMethodAsString));
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.SessionGet;
|
|
|
|
|
var
|
|
|
|
|
s: string;
|
|
|
|
|
begin
|
|
|
|
|
ContentType := Context.Request.Accept;
|
|
|
|
|
s := Session['value'];
|
|
|
|
|
Render(s);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TTestServerController.SessionSet;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2017-09-26 01:02:09 +02:00
|
|
|
|
Session['value'] := Context.Request.Params['value'];
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TTestServerController.TestConsumeJSON;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
Render(TJSONObject.ParseJSONValue('{"key":"Hello World"}'));
|
|
|
|
|
end;
|
|
|
|
|
|
2017-09-26 01:02:09 +02:00
|
|
|
|
procedure TTestServerController.TestConsumesProduces;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
Render('Hello World');
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestConsumesProducesText;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
Render('Hello World');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestConsumesProducesTextISO8859_1;
|
|
|
|
|
begin
|
|
|
|
|
Render(Context.Request.Body);
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestCharset;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
Obj: TJSONObject;
|
|
|
|
|
begin
|
2017-10-16 22:57:27 +02:00
|
|
|
|
ContentType := BuildContentType(TMVCMediaType.APPLICATION_JSON, TMVCCharset.UTF_8);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
Obj := TJSONObject.Create;
|
|
|
|
|
Obj.AddPair('name1', 'j<>rn');
|
|
|
|
|
Obj.AddPair('name2', '<27>to je Unicode?');
|
|
|
|
|
Obj.AddPair('name3', '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>');
|
|
|
|
|
Render(Obj);
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestGetPersonByID;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
PersonList: TObjectList<TPerson>;
|
|
|
|
|
ID: Integer;
|
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
ID := Context.Request.Params['id'].ToInteger;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
PersonList := TPerson.GetList;
|
|
|
|
|
try
|
|
|
|
|
Render(PersonList[ID - 1], false);
|
|
|
|
|
finally
|
|
|
|
|
PersonList.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestGetPersonByIDAsFields;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
PersonList: TObjectList<TPerson>;
|
|
|
|
|
ID: Integer;
|
|
|
|
|
begin
|
|
|
|
|
raise Exception.Create('Not implemented');
|
2018-10-23 16:18:34 +02:00
|
|
|
|
ID := Context.Request.Params['id'].ToInteger;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
PersonList := TPerson.GetList;
|
|
|
|
|
try
|
2017-06-02 00:10:31 +02:00
|
|
|
|
// Render(PersonList[ID - 1], false, TDMVCSerializationType.Fields);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
PersonList.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestGetPersons;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
Person: TPerson;
|
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
case Context.Request.HTTPMethod of
|
2017-03-01 21:40:57 +01:00
|
|
|
|
httpGET:
|
2017-08-21 15:31:31 +02:00
|
|
|
|
Render<TPerson>(TPerson.GetList, True);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
httpPOST:
|
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Person := Context.Request.BodyAs<TPerson>();
|
2017-03-01 21:40:57 +01:00
|
|
|
|
Render(Person);
|
|
|
|
|
end;
|
|
|
|
|
httpPUT:
|
|
|
|
|
;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestGetWrappedPeople;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
LWrappedList: IWrappedList;
|
|
|
|
|
lObj: TObject;
|
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
if not Context.Request.QueryStringParamExists('count') then
|
|
|
|
|
begin
|
|
|
|
|
lObj := TPerson.GetList;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
lObj := TPerson.GetList(Context.Request.ParamsAsInt64['count']);
|
|
|
|
|
end;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
try
|
|
|
|
|
LWrappedList := WrapAsList(lObj);
|
2017-03-20 19:31:42 +01:00
|
|
|
|
Render(LWrappedList);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
finally
|
|
|
|
|
lObj.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestHelloWorld;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
ContentType := 'text/plain';
|
|
|
|
|
Render('hello world');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestJSONArrayAsObjectList;
|
|
|
|
|
var
|
|
|
|
|
vUsers: TObjectList<TCustomer>;
|
|
|
|
|
begin
|
|
|
|
|
vUsers := Context.Request.BodyAsListOf<TCustomer>();
|
|
|
|
|
try
|
|
|
|
|
vUsers.OwnsObjects := True;
|
|
|
|
|
if (vUsers.Count = 3000) then
|
|
|
|
|
Render('Success!')
|
|
|
|
|
else
|
|
|
|
|
Render('Error!');
|
|
|
|
|
finally
|
|
|
|
|
FreeAndNil(vUsers);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestMultiplePaths;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Render(Context.Request.Params['id']);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestServerController.TestPOSTObject;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
Person: TPerson;
|
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Person := Context.Request.BodyAs<TPerson>();
|
2017-03-01 21:40:57 +01:00
|
|
|
|
Render(Person);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestRenderStreamAndFreeWithOwnerFalse;
|
|
|
|
|
var
|
|
|
|
|
LStream: TMemoryStream;
|
|
|
|
|
begin
|
|
|
|
|
LStream := TMemoryStream.Create;
|
|
|
|
|
try
|
|
|
|
|
Render(LStream, false);
|
|
|
|
|
finally
|
|
|
|
|
LStream.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestRenderStreamAndFreeWithOwnerTrue;
|
|
|
|
|
var
|
|
|
|
|
LStream: TMemoryStream;
|
|
|
|
|
begin
|
|
|
|
|
LStream := TMemoryStream.Create;
|
|
|
|
|
Render(LStream, True);
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-31 01:07:23 +01:00
|
|
|
|
procedure TTestServerController.TestStringDictionary;
|
|
|
|
|
var
|
|
|
|
|
lDict: TMVCStringDictionary;
|
|
|
|
|
begin
|
|
|
|
|
lDict := Context.Request.BodyAs<TMVCStringDictionary>;
|
|
|
|
|
try
|
|
|
|
|
lDict['fromserver'] := 'changed';
|
|
|
|
|
Render(lDict, false);
|
|
|
|
|
finally
|
|
|
|
|
lDict.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
|
procedure TTestServerController.TestTypedActionAllTypes(ParString: string; ParInteger: Integer; ParInt64: Int64; ParSingle: Single;
|
|
|
|
|
ParDouble: Double; ParExtended: Extended);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
var
|
|
|
|
|
lJObj: TJSONObject;
|
|
|
|
|
begin
|
|
|
|
|
lJObj := TJSONObject.Create;
|
|
|
|
|
lJObj.AddPair('ParString', ParString);
|
|
|
|
|
lJObj.AddPair('ParInteger', TJSONNumber.Create(ParInteger));
|
|
|
|
|
lJObj.AddPair('ParInt64', TJSONNumber.Create(ParInt64));
|
|
|
|
|
lJObj.AddPair('ParSingle', TJSONNumber.Create(ParSingle));
|
|
|
|
|
lJObj.AddPair('ParDouble', TJSONNumber.Create(ParDouble));
|
|
|
|
|
lJObj.AddPair('ParExtended', TJSONNumber.Create(ParExtended));
|
|
|
|
|
Render(lJObj);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionDouble1(value: Double);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(FloatToStr(value, FFormatSettings) + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionExtended1(value: Extended);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(FloatToStr(value, FFormatSettings) + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionSingle1(value: Single);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(FloatToStr(value, FFormatSettings) + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionInt641(value: Int64);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(value.ToString + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionInteger1(value: Integer);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(value.ToString + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionString1(value: string);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(value + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionTDate1(value: TDate);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2017-05-25 16:57:49 +02:00
|
|
|
|
Render(DateToISODate(value) + ' modified from server');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionTDateTime1(value: TDateTime);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2017-05-25 16:57:49 +02:00
|
|
|
|
Render(DateTimeToISOTimeStamp(value) + ' modified from server');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
|
procedure TTestServerController.TestTypedActionBooleans(bool1, bool2, bool3, bool4: Boolean);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2018-08-08 17:11:45 +02:00
|
|
|
|
Render(Format('%s.%s.%s.%s', [BoolToStr(bool1, True), BoolToStr(bool2, True), BoolToStr(bool3, True), BoolToStr(bool4, True)]));
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionTTime1(value: TTime);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2017-05-25 16:57:49 +02:00
|
|
|
|
Render(TimeToISOTime(value) + ' modified from server');
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{ TTestPrivateServerController }
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestPrivateServerController.OnlyRole1;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Render(Context.LoggedUser.UserName);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestPrivateServerController.OnlyRole1Session;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
if Context.Request.QueryStringParamExists('value') then
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Session['value'] := Context.Request.Params['value'];
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
Render(Session['value']);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-10-23 16:18:34 +02:00
|
|
|
|
procedure TTestPrivateServerController.OnlyRole2;
|
2017-03-01 21:40:57 +01:00
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
Render(Context.LoggedUser.UserName);
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
|
{ TTestFaultController }
|
|
|
|
|
|
|
|
|
|
constructor TTestFaultController.Create;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
raise Exception.Create('BOOOM!!!');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestFaultController.NeverExecuted;
|
|
|
|
|
begin
|
|
|
|
|
// do nothing
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{ TTestFault2Controller }
|
|
|
|
|
|
|
|
|
|
constructor TTestFault2Controller.Create;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestFault2Controller.NeverExecuted;
|
|
|
|
|
begin
|
2018-10-23 16:18:34 +02:00
|
|
|
|
// do nothing
|
2018-08-08 17:11:45 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-01 21:40:57 +01:00
|
|
|
|
end.
|