2016-09-18 19:19:23 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2010-2016 Daniele Teti and the DMVCFramework Team
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
//
|
|
|
|
|
// *************************************************************************** }
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
unit TestServerControllerU;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
2016-12-07 19:35:36 +01:00
|
|
|
|
uses MVCFramework, System.SysUtils, MVCFramework.Commons;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
|
TTestServerController = class(TMVCController)
|
2016-06-22 17:49:16 +02:00
|
|
|
|
private
|
|
|
|
|
FFormatSettings: TFormatSettings;
|
|
|
|
|
protected
|
|
|
|
|
procedure MVCControllerAfterCreate; override;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
public
|
|
|
|
|
[MVCPath('/req/with/params/($par1)/($par2)/($par3)')]
|
|
|
|
|
[MVCHTTPMethod([httpGET, httpDELETE])]
|
|
|
|
|
procedure ReqWithParams(ctx: TWebContext);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/echo/($par1)/($par2)/($par3)')]
|
2014-03-24 17:37:08 +01:00
|
|
|
|
[MVCHTTPMethod([httpPOST, httpPUT, httpPATCH])]
|
2013-10-30 01:09:09 +01:00
|
|
|
|
procedure EchoBody(ctx: TWebContext);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/session/($value)')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
|
|
|
|
procedure SessionSet(ctx: TWebContext);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/session')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2016-11-27 23:17:20 +01:00
|
|
|
|
procedure SessionGet;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/headers')]
|
|
|
|
|
procedure EchoHeaders(ctx: TWebContext);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/lotofcookies')]
|
|
|
|
|
procedure GenerateCookies(ctx: TWebContext);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/dataset/($datasetname)')]
|
|
|
|
|
procedure DataSetHandling(ctx: TWebContext);
|
|
|
|
|
|
|
|
|
|
[MVCPath('/login/($username)')]
|
|
|
|
|
// this is only for test!!!!
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure Login;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
[MVCPath('/logout')]
|
|
|
|
|
// this is only for test!!!!
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure Logout;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
2013-11-05 14:57:50 +01:00
|
|
|
|
[MVCPath('/encoding')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
// this is only for test!!!!
|
|
|
|
|
procedure TestEncoding(ctx: TWebContext);
|
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
[MVCPath('/testconsumes')]
|
|
|
|
|
[MVCHTTPMethod([httpGET, httpPOST, httpPUT])]
|
|
|
|
|
[MVCConsumes('application/json')]
|
2014-04-10 13:56:23 +02:00
|
|
|
|
[MVCProduces('application/json', 'utf-8')]
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure TestConsumesProduces(ctx: TWebContext);
|
|
|
|
|
|
2016-12-07 19:35:36 +01:00
|
|
|
|
[MVCPath('/testconsumes/textiso8859_1')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST, httpPUT])]
|
|
|
|
|
[MVCConsumes(TMVCMediaType.TEXT_PLAIN)]
|
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_PLAIN, 'iso8859-1')]
|
|
|
|
|
procedure TestConsumesProducesTextISO8859_1;
|
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
[MVCPath('/testconsumes')]
|
|
|
|
|
[MVCHTTPMethod([httpGET, httpPOST, httpPUT])]
|
|
|
|
|
[MVCConsumes('text/plain')]
|
|
|
|
|
[MVCProduces('text/plain')]
|
|
|
|
|
procedure TestConsumesProducesText(ctx: TWebContext);
|
|
|
|
|
|
2015-01-14 11:39:44 +01:00
|
|
|
|
[MVCPath('/testconsumejson')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
[MVCConsumes('application/json')]
|
|
|
|
|
[MVCProduces('application/json', 'utf-8')]
|
|
|
|
|
procedure TestConsumeJSON(ctx: TWebContext);
|
|
|
|
|
|
2015-02-16 14:25:09 +01:00
|
|
|
|
[MVCPath('/people/($id)')]
|
2015-01-14 11:39:44 +01:00
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
procedure TestGetPersonByID(ctx: TWebContext);
|
|
|
|
|
|
2015-02-16 14:25:09 +01:00
|
|
|
|
[MVCPath('/people/($id)/asfields')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
procedure TestGetPersonByIDAsFields(ctx: TWebContext);
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
[MVCPath('/customers/list')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2016-12-29 19:28:50 +01:00
|
|
|
|
procedure TestJSONArrayAsObjectList;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
|
2015-02-16 14:25:09 +01:00
|
|
|
|
[MVCPath('/people')]
|
2015-01-14 11:39:44 +01:00
|
|
|
|
[MVCHTTPMethod([httpGET, httpPOST, httpPUT])]
|
|
|
|
|
procedure TestGetPersons(ctx: TWebContext);
|
|
|
|
|
|
2016-09-29 18:17:12 +02:00
|
|
|
|
[MVCPath('/wrappedpeople')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
procedure TestGetWrappedPersons(ctx: TWebContext);
|
|
|
|
|
|
2013-11-18 00:44:40 +01:00
|
|
|
|
[MVCPath('/objects')]
|
|
|
|
|
[MVCHTTPMethod([httpPOST, httpPUT])]
|
|
|
|
|
[MVCProduces('application/json')]
|
|
|
|
|
procedure TestPOSTObject(ctx: TWebContext);
|
2013-11-18 00:16:59 +01:00
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
[MVCPath('/speed')]
|
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
|
|
|
|
procedure TestHelloWorld(ctx: TWebContext);
|
|
|
|
|
|
2014-04-02 20:28:53 +02: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])]
|
|
|
|
|
procedure TestMultiplePaths(ctx: TWebContext);
|
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
{ Strongly typed actions }
|
|
|
|
|
[MVCPath('/typed/string1/($value)')]
|
2016-09-18 19:19:23 +02:00
|
|
|
|
procedure TestTypedActionString1(value: string);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
|
2016-12-07 19:35:36 +01:00
|
|
|
|
[MVCPath('/typed/all/($ParString)/($ParInteger)/($ParInt64)/($ParSingle)/($ParDouble)/($ParExtended)')]
|
2016-11-18 18:02:48 +01:00
|
|
|
|
procedure TestTypedActionAllTypes(ParString: string; ParInteger: Integer;
|
|
|
|
|
ParInt64: Int64; ParSingle: Single; ParDouble: Double;
|
|
|
|
|
ParExtended: Extended);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
|
2016-11-18 18:02:48 +01:00
|
|
|
|
[MVCPath('/renderstreamandfreewithownerfalse')]
|
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerFalse;
|
|
|
|
|
|
|
|
|
|
[MVCPath('/renderstreamandfreewithownertrue')]
|
|
|
|
|
procedure TestRenderStreamAndFreeWithOwnerTrue;
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
[MVCPath('/private')]
|
|
|
|
|
TTestPrivateServerController = class(TMVCController)
|
|
|
|
|
public
|
|
|
|
|
[MVCPath('/role1')]
|
|
|
|
|
procedure OnlyRole1(ctx: TWebContext);
|
2016-04-03 22:35:27 +02:00
|
|
|
|
[MVCPath('/role1session')]
|
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
|
procedure OnlyRole1Session(ctx: TWebContext);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
[MVCPath('/role2')]
|
|
|
|
|
procedure OnlyRole2(ctx: TWebContext);
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
uses
|
2014-09-05 12:47:40 +02:00
|
|
|
|
{$IF CompilerVersion < 27}
|
2013-10-30 01:09:09 +01:00
|
|
|
|
Data.DBXJSON,
|
2014-04-16 22:52:25 +02:00
|
|
|
|
{$ELSE}
|
|
|
|
|
System.JSON,
|
|
|
|
|
{$IFEND}
|
2016-12-07 19:35:36 +01:00
|
|
|
|
Web.HTTPApp, BusinessObjectsU, Generics.Collections,
|
2016-11-18 18:02:48 +01:00
|
|
|
|
ObjectsMappers, MVCFramework.DuckTyping, System.Classes;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
{ TTestServerController }
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.DataSetHandling(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
case ctx.Request.HTTPMethod of
|
|
|
|
|
httpGET:
|
|
|
|
|
begin
|
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
httpPOST:
|
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
httpPUT:
|
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
httpDELETE:
|
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
httpHEAD:
|
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
httpOPTIONS:
|
|
|
|
|
begin
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.EchoBody(ctx: TWebContext);
|
|
|
|
|
var
|
2015-01-14 11:39:44 +01:00
|
|
|
|
JSON: TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
2015-01-14 11:39:44 +01:00
|
|
|
|
JSON := ctx.Request.BodyAsJSONObject.Clone as TJSONObject;
|
|
|
|
|
JSON.AddPair('echo', 'from server');
|
|
|
|
|
Render(JSON);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.EchoHeaders(ctx: TWebContext);
|
|
|
|
|
begin
|
2016-01-01 23:00:22 +01:00
|
|
|
|
ctx.Response.ContentType := TMVCMediaType.TEXT_PLAIN;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
Render(ctx.Request.Headers['ACCEPT']);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.GenerateCookies(ctx: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
c: TCookie;
|
|
|
|
|
v: string;
|
|
|
|
|
begin
|
|
|
|
|
v := ctx.Request.Cookie('usersettings');
|
|
|
|
|
|
|
|
|
|
c := ctx.Response.Cookies.Add;
|
|
|
|
|
c.Name := 'usersettings1';
|
2016-06-22 17:49:16 +02:00
|
|
|
|
c.value := 'usersettings1-value';
|
2016-04-24 19:08:21 +02:00
|
|
|
|
c.Path := '/usersettings1';
|
2013-10-30 01:09:09 +01:00
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
|
|
|
|
c := ctx.Response.Cookies.Add;
|
|
|
|
|
c.Name := 'usersettings2';
|
2016-06-22 17:49:16 +02:00
|
|
|
|
c.value := 'usersettings2-value';
|
2016-04-24 19:08:21 +02:00
|
|
|
|
c.Path := '/usersettings2';
|
2013-10-30 01:09:09 +01:00
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
|
|
|
|
c := ctx.Response.Cookies.Add;
|
|
|
|
|
c.Name := 'usersettings3';
|
2016-06-22 17:49:16 +02:00
|
|
|
|
c.value := 'usersettings3-value';
|
2016-04-24 19:08:21 +02:00
|
|
|
|
c.Path := '/usersettings3';
|
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
|
|
|
|
c := ctx.Response.Cookies.Add;
|
|
|
|
|
c.Name := 'usersettings4';
|
2016-06-22 17:49:16 +02:00
|
|
|
|
c.value := 'usersettings4-value';
|
2016-04-24 19:08:21 +02:00
|
|
|
|
c.Path := '/usersettings4';
|
2013-10-30 01:09:09 +01:00
|
|
|
|
c.Expires := 0;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure TTestServerController.Login;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
2016-06-28 13:42:14 +02:00
|
|
|
|
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');
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-06-28 13:42:14 +02:00
|
|
|
|
procedure TTestServerController.Logout;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
2016-06-28 13:42:14 +02:00
|
|
|
|
if not Context.SessionStarted then
|
|
|
|
|
raise EMVCException.Create('Session not available');
|
|
|
|
|
Context.SessionStop(false);
|
|
|
|
|
if Context.SessionStarted then
|
|
|
|
|
raise EMVCException.Create('Session still available');
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-06-22 17:49:16 +02:00
|
|
|
|
procedure TTestServerController.MVCControllerAfterCreate;
|
|
|
|
|
begin
|
|
|
|
|
FFormatSettings.DecimalSeparator := '.';
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
procedure TTestServerController.ReqWithParams(ctx: TWebContext);
|
|
|
|
|
begin
|
2016-04-03 22:35:27 +02:00
|
|
|
|
Render(TJSONObject.Create.AddPair('par1', ctx.Request.Params['par1'])
|
|
|
|
|
.AddPair('par2', ctx.Request.Params['par2']).AddPair('par3',
|
|
|
|
|
ctx.Request.Params['par3']).AddPair('method',
|
2015-04-01 17:01:23 +02:00
|
|
|
|
ctx.Request.HTTPMethodAsString));
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
|
procedure TTestServerController.SessionGet;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
var
|
|
|
|
|
s: string;
|
|
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
|
ContentType := Context.Request.Accept;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
s := Session['value'];
|
2016-11-27 23:17:20 +01:00
|
|
|
|
Render(s);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.SessionSet(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
Session['value'] := ctx.Request.Params['value'];
|
|
|
|
|
end;
|
|
|
|
|
|
2015-01-14 11:39:44 +01:00
|
|
|
|
procedure TTestServerController.TestConsumeJSON(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
Render(TJSONObject.ParseJSONValue('{"key":"Hello World"}'));
|
|
|
|
|
end;
|
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure TTestServerController.TestConsumesProduces(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
Render('Hello World');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestConsumesProducesText(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
Render('Hello World');
|
|
|
|
|
end;
|
|
|
|
|
|
2016-12-07 19:35:36 +01:00
|
|
|
|
procedure TTestServerController.TestConsumesProducesTextISO8859_1;
|
|
|
|
|
begin
|
|
|
|
|
Render(Context.Request.Body);
|
|
|
|
|
end;
|
|
|
|
|
|
2013-11-05 14:57:50 +01:00
|
|
|
|
procedure TTestServerController.TestEncoding(ctx: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
Obj: TJSONObject;
|
|
|
|
|
begin
|
2014-06-27 15:30:39 +02:00
|
|
|
|
ContentCharset := TMVCConstants.DEFAULT_CONTENT_CHARSET;
|
2013-11-05 14:57:50 +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;
|
|
|
|
|
|
2015-01-14 11:39:44 +01:00
|
|
|
|
procedure TTestServerController.TestGetPersonByID(ctx: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
PersonList: TObjectList<TPerson>;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
ID: Integer;
|
2015-01-14 11:39:44 +01:00
|
|
|
|
begin
|
|
|
|
|
ID := ctx.Request.Params['id'].ToInteger;
|
|
|
|
|
PersonList := TPerson.GetList;
|
|
|
|
|
try
|
|
|
|
|
Render(PersonList[ID - 1], false);
|
|
|
|
|
finally
|
|
|
|
|
PersonList.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-02-16 14:25:09 +01:00
|
|
|
|
procedure TTestServerController.TestGetPersonByIDAsFields(ctx: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
PersonList: TObjectList<TPerson>;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
ID: Integer;
|
2015-02-16 14:25:09 +01:00
|
|
|
|
begin
|
|
|
|
|
ID := ctx.Request.Params['id'].ToInteger;
|
|
|
|
|
PersonList := TPerson.GetList;
|
|
|
|
|
try
|
|
|
|
|
Render(PersonList[ID - 1], false, TDMVCSerializationType.Fields);
|
|
|
|
|
finally
|
|
|
|
|
PersonList.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-01-14 11:39:44 +01:00
|
|
|
|
procedure TTestServerController.TestGetPersons(ctx: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
Person: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
case ctx.Request.HTTPMethod of
|
|
|
|
|
httpGET:
|
|
|
|
|
Render<TPerson>(TPerson.GetList);
|
|
|
|
|
httpPOST:
|
|
|
|
|
begin
|
|
|
|
|
Person := ctx.Request.BodyAs<TPerson>();
|
|
|
|
|
Render(Person);
|
|
|
|
|
end;
|
|
|
|
|
httpPUT:
|
|
|
|
|
;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
2016-09-29 18:17:12 +02:00
|
|
|
|
procedure TTestServerController.TestGetWrappedPersons(ctx: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
LWrappedList: IWrappedList;
|
2016-11-18 18:25:09 +01:00
|
|
|
|
lObj: TObject;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
begin
|
2016-11-18 18:25:09 +01:00
|
|
|
|
lObj := TPerson.GetList;
|
|
|
|
|
try
|
|
|
|
|
LWrappedList := WrapAsList(lObj);
|
|
|
|
|
RenderWrappedList(LWrappedList);
|
|
|
|
|
finally
|
|
|
|
|
lObj.Free;
|
|
|
|
|
end;
|
2016-09-29 18:17:12 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
procedure TTestServerController.TestHelloWorld(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := 'text/plain';
|
|
|
|
|
Render('hello world');
|
|
|
|
|
end;
|
|
|
|
|
|
2016-12-29 19:28:50 +01:00
|
|
|
|
procedure TTestServerController.TestJSONArrayAsObjectList;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
var
|
|
|
|
|
vUsers: TObjectList<TCustomer>;
|
|
|
|
|
begin
|
2016-12-29 19:28:50 +01:00
|
|
|
|
vUsers := Context.Request.BodyAsListOf<TCustomer>();
|
2015-04-01 17:01:23 +02:00
|
|
|
|
try
|
|
|
|
|
vUsers.OwnsObjects := True;
|
2016-12-29 19:28:50 +01:00
|
|
|
|
if (vUsers.Count = 3000) then
|
|
|
|
|
Render('Success!')
|
2015-04-01 17:01:23 +02:00
|
|
|
|
else
|
|
|
|
|
Render('Error!');
|
|
|
|
|
finally
|
|
|
|
|
FreeAndNil(vUsers);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2014-04-02 20:28:53 +02:00
|
|
|
|
procedure TTestServerController.TestMultiplePaths(ctx: TWebContext);
|
|
|
|
|
begin
|
2016-01-01 23:00:22 +01:00
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2014-04-02 20:28:53 +02:00
|
|
|
|
Render(ctx.Request.Params['id']);
|
|
|
|
|
end;
|
|
|
|
|
|
2013-11-18 00:44:40 +01:00
|
|
|
|
procedure TTestServerController.TestPOSTObject(ctx: TWebContext);
|
|
|
|
|
var
|
|
|
|
|
Person: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
Person := ctx.Request.BodyAs<TPerson>();
|
|
|
|
|
Render(Person);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-11-18 18:02:48 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2016-09-18 19:19:23 +02:00
|
|
|
|
procedure TTestServerController.TestTypedActionAllTypes(ParString: string;
|
2016-06-22 17:49:16 +02:00
|
|
|
|
ParInteger: Integer; ParInt64: Int64; ParSingle: Single; ParDouble: Double;
|
|
|
|
|
ParExtended: Extended);
|
|
|
|
|
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;
|
|
|
|
|
|
2016-09-18 19:19:23 +02:00
|
|
|
|
procedure TTestServerController.TestTypedActionString1(value: string);
|
2016-06-22 17:49:16 +02:00
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(value + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionTDate1(value: TDate);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(ISODateToString(value) + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionTDateTime1(value: TDateTime);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(ISODateTimeToString(value) + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionBooleans(bool1, bool2, bool3,
|
|
|
|
|
bool4: Boolean);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
2016-11-18 18:02:48 +01:00
|
|
|
|
Render(Format('%s.%s.%s.%s', [BoolToStr(bool1, True), BoolToStr(bool2, True),
|
|
|
|
|
BoolToStr(bool3, True), BoolToStr(bool4, True)]));
|
2016-06-22 17:49:16 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TTestServerController.TestTypedActionTTime1(value: TTime);
|
|
|
|
|
begin
|
|
|
|
|
ContentType := TMVCMediaType.TEXT_PLAIN;
|
|
|
|
|
Render(ISOTimeToString(value) + ' modified from server');
|
|
|
|
|
end;
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
{ TTestPrivateServerController }
|
|
|
|
|
|
|
|
|
|
procedure TTestPrivateServerController.OnlyRole1(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
Render(ctx.LoggedUser.UserName);
|
|
|
|
|
end;
|
|
|
|
|
|
2016-04-03 22:35:27 +02:00
|
|
|
|
procedure TTestPrivateServerController.OnlyRole1Session(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
if ctx.Request.QueryStringParamExists('value') then
|
|
|
|
|
begin
|
|
|
|
|
Session['value'] := ctx.Request.Params['value'];
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
Render(Session['value']);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
procedure TTestPrivateServerController.OnlyRole2(ctx: TWebContext);
|
|
|
|
|
begin
|
|
|
|
|
Render(ctx.LoggedUser.UserName);
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end.
|