2013-10-30 01:09:09 +01:00
|
|
|
|
unit LiveServerTestU;
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
TestFramework,
|
|
|
|
|
MVCFramework.RESTClient;
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
TBaseServerTest = class(TTestCase)
|
|
|
|
|
protected
|
|
|
|
|
RESTClient: TRESTClient;
|
|
|
|
|
procedure DoLoginWith(UserName: string);
|
|
|
|
|
procedure DoLogout;
|
|
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
procedure SetUp; override;
|
|
|
|
|
procedure TearDown; override;
|
|
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
TServerTest = class(TBaseServerTest)
|
|
|
|
|
published
|
|
|
|
|
procedure TestReqWithParams;
|
|
|
|
|
procedure TestPOSTWithParamsAndJSONBody;
|
2014-03-24 17:37:08 +01:00
|
|
|
|
// procedure TestPATCHWithParamsAndJSONBody;
|
2013-11-18 00:44:40 +01:00
|
|
|
|
procedure TestPOSTWithObjectJSONBody;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
procedure TestPUTWithParamsAndJSONBody;
|
|
|
|
|
procedure TestSession;
|
|
|
|
|
procedure TestAsynchRequestPOST;
|
|
|
|
|
procedure TestAsynchRequestPUT;
|
|
|
|
|
procedure TestAsynchRequestGET;
|
|
|
|
|
procedure TestAsynchRequestDELETE;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
procedure TestEncodingRenderJSONValue;
|
2015-02-16 14:25:09 +01:00
|
|
|
|
procedure TestSerializationType;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure TestProducesConsumes01;
|
|
|
|
|
procedure TestProducesConsumes02;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
procedure TestProducesConsumesWithWrongAcceptHeader;
|
2013-12-05 16:19:01 +01:00
|
|
|
|
procedure TestExceptionInMVCAfterCreate;
|
|
|
|
|
procedure TestExceptionInMVCBeforeDestroy;
|
2014-03-31 11:25:16 +02:00
|
|
|
|
procedure TestMiddlewareSpeedMiddleware;
|
2014-03-31 11:40:25 +02:00
|
|
|
|
procedure TestMiddlewareHandler;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
procedure TestPostAListOfObjects;
|
|
|
|
|
// test authentication/authorization
|
|
|
|
|
procedure TestAuthentication01;
|
|
|
|
|
procedure TestAuthentication02;
|
|
|
|
|
procedure TestAuthentication03;
|
|
|
|
|
procedure TestAuthentication04;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
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,
|
2014-09-05 12:47:40 +02:00
|
|
|
|
{$ENDIF}
|
2013-10-30 01:09:09 +01:00
|
|
|
|
MVCFramework.Commons,
|
|
|
|
|
System.SyncObjs,
|
2015-04-01 17:01:23 +02:00
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
BusinessObjectsU,
|
|
|
|
|
ObjectsMappers,
|
|
|
|
|
Soap.EncdDecd;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
{ TServerTest }
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.DoLogout;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/logout', []);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Logout Failed');
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.SetUp;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
2014-02-24 10:20:34 +01:00
|
|
|
|
RESTClient := TRESTClient.Create('localhost', 9999);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
RESTClient.ReadTimeout := 60 * 1000 * 30;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TBaseServerTest.TearDown;
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
RESTClient.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestDELETE;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
r: TWaitResult;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
OK: boolean;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
|
OK := false;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure(Response: IRESTResponse)
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
|
OK := true;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
evt.SetEvent;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
|
OK := false;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end).doDELETE('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(true, OK);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestGET;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
r: TWaitResult;
|
|
|
|
|
j: TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure(Response: IRESTResponse)
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
|
|
|
|
try
|
2013-11-09 14:22:11 +01:00
|
|
|
|
j := Response.BodyAsJsonObject.Clone as TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end).doGET('/req/with/params', ['1', '2', '3']);
|
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
|
|
|
|
CheckTrue(Assigned(j));
|
|
|
|
|
CheckEquals('1', j.Get('par1').JsonValue.Value);
|
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestPOST;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
r: TWaitResult;
|
|
|
|
|
j: TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure(Response: IRESTResponse)
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
|
|
|
|
try
|
2013-11-09 14:22:11 +01:00
|
|
|
|
j := Response.BodyAsJsonObject.Clone as TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
|
end).doPOST('/echo', ['1', '2', '3'], TJSONObject.Create(TJSONPair.Create('from client',
|
|
|
|
|
'hello world')), true);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
|
|
|
|
CheckTrue(Assigned(j));
|
|
|
|
|
CheckEquals('from server', j.Get('echo').JsonValue.Value);
|
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAsynchRequestPUT;
|
|
|
|
|
var
|
|
|
|
|
evt: TEvent;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
r: TWaitResult;
|
|
|
|
|
j: TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
|
|
|
|
j := nil;
|
|
|
|
|
evt := TEvent.Create;
|
|
|
|
|
try
|
|
|
|
|
RESTClient.Asynch(
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure(Response: IRESTResponse)
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
|
|
|
|
try
|
2013-11-09 14:22:11 +01:00
|
|
|
|
j := Response.BodyAsJsonObject.Clone as TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
except
|
|
|
|
|
// test should not block...never!
|
|
|
|
|
end;
|
|
|
|
|
evt.SetEvent;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end,
|
|
|
|
|
procedure(E: Exception)
|
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
|
end).doPUT('/echo', ['1', '2', '3'], TJSONObject.Create(TJSONPair.Create('from client',
|
|
|
|
|
'hello world')), true);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
// wait for thred finish
|
|
|
|
|
repeat
|
|
|
|
|
r := evt.WaitFor(2000);
|
|
|
|
|
until r = TWaitResult.wrSignaled;
|
|
|
|
|
|
|
|
|
|
CheckTrue(Assigned(j));
|
|
|
|
|
CheckEquals('from server', j.Get('echo').JsonValue.Value);
|
|
|
|
|
j.Free;
|
|
|
|
|
finally
|
|
|
|
|
evt.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
procedure TServerTest.TestAuthentication01;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAuthentication02;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := '';
|
|
|
|
|
RESTClient.Password := '';
|
|
|
|
|
RESTClient.UseBasicAuthentication := false;
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Unauthorized, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAuthentication03;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
|
|
|
|
LRes := RESTClient.doGET('/private/role2', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.Forbidden, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestAuthentication04;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
RESTClient.UserName := 'user1';
|
|
|
|
|
RESTClient.Password := 'user1';
|
|
|
|
|
RESTClient.UseBasicAuthentication := true;
|
|
|
|
|
LRes := RESTClient.doGET('/private/role1', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
LRes := RESTClient.doGET('/people', []);
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
end;
|
|
|
|
|
|
2013-11-05 14:57:50 +01:00
|
|
|
|
procedure TServerTest.TestEncodingRenderJSONValue;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
2014-04-10 17:30:39 +02:00
|
|
|
|
s: string;
|
2013-11-05 14:57:50 +01:00
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/encoding', []);
|
2014-04-10 17:30:39 +02:00
|
|
|
|
|
|
|
|
|
s := res.BodyAsJsonObject.Get('name1').JsonValue.Value;
|
|
|
|
|
CheckEquals('j<>rn', s);
|
|
|
|
|
|
|
|
|
|
s := res.BodyAsJsonObject.Get('name3').JsonValue.Value;
|
|
|
|
|
CheckEquals('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', s);
|
|
|
|
|
|
|
|
|
|
s := res.BodyAsJsonObject.Get('name2').JsonValue.Value;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals('<27>to je Unicode?', s,
|
|
|
|
|
'If this test fail, check http://qc.embarcadero.com/wc/qcmain.aspx?d=119779');
|
2014-04-10 17:30:39 +02:00
|
|
|
|
{ WARNING!!! }
|
|
|
|
|
{
|
|
|
|
|
If this test fail, check
|
|
|
|
|
http://qc.embarcadero.com/wc/qcmain.aspx?d=119779
|
|
|
|
|
}
|
2013-11-05 14:57:50 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2013-12-05 16:19:01 +01:00
|
|
|
|
procedure TServerTest.TestExceptionInMVCAfterCreate;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/exception/aftercreate/nevercalled', []);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.InternalServerError, res.ResponseCode);
|
2013-12-05 16:19:01 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestExceptionInMVCBeforeDestroy;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/exception/beforedestroy/nevercalled', []);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.InternalServerError, res.ResponseCode);
|
2013-12-05 16:19:01 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2014-03-31 11:40:25 +02:00
|
|
|
|
procedure TServerTest.TestMiddlewareHandler;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
begin
|
2015-02-16 14:25:09 +01:00
|
|
|
|
r := RESTClient.Accept(TMVCMimeType.APPLICATION_JSON).doGET('/handledbymiddleware', []);
|
2014-03-31 11:40:25 +02:00
|
|
|
|
CheckEquals('This is a middleware response', r.BodyAsString);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, r.ResponseCode);
|
2014-03-31 11:40:25 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2014-03-31 11:25:16 +02:00
|
|
|
|
procedure TServerTest.TestMiddlewareSpeedMiddleware;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
P: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
P := TPerson.Create;
|
|
|
|
|
try
|
|
|
|
|
P.FirstName := StringOfChar('*', 1000);
|
|
|
|
|
P.LastName := StringOfChar('*', 1000);
|
|
|
|
|
P.DOB := EncodeDate(1979, 1, 1);
|
|
|
|
|
P.Married := true;
|
2015-04-01 17:01:23 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMimeType.APPLICATION_JSON).doPOST('/objects', [],
|
|
|
|
|
Mapper.ObjectToJSONObject(P));
|
2014-03-31 11:25:16 +02:00
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
CheckNotEquals('', r.GetHeaderValue('request_gen_time'));
|
|
|
|
|
end;
|
|
|
|
|
|
2014-03-24 17:37:08 +01:00
|
|
|
|
// procedure TServerTest.TestPATCHWithParamsAndJSONBody;
|
|
|
|
|
// var
|
|
|
|
|
// r: IRESTResponse;
|
|
|
|
|
// json: TJSONObject;
|
|
|
|
|
// begin
|
|
|
|
|
// json := TJSONObject.Create;
|
|
|
|
|
// json.AddPair('client', 'clientdata');
|
|
|
|
|
// r := RESTClient.doPATCH('/echo', ['1', '2', '3'], json);
|
|
|
|
|
// CheckEquals('clientdata', r.BodyAsJsonObject.Get('client').JsonValue.Value);
|
|
|
|
|
// CheckEquals('from server', r.BodyAsJsonObject.Get('echo').JsonValue.Value);
|
|
|
|
|
// end;
|
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
procedure TServerTest.TestPostAListOfObjects;
|
|
|
|
|
var
|
|
|
|
|
LRes: IRESTResponse;
|
|
|
|
|
LCustomers: TObjectList<TCustomer>;
|
|
|
|
|
begin
|
|
|
|
|
LCustomers := TCustomer.GetList;
|
|
|
|
|
try
|
|
|
|
|
LRes := RESTClient.doPOST('/customers/list', [],
|
|
|
|
|
Mapper.ObjectListToJSONArray<TCustomer>(LCustomers));
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, LRes.ResponseCode);
|
|
|
|
|
finally
|
|
|
|
|
LCustomers.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-11-18 00:44:40 +01:00
|
|
|
|
procedure TServerTest.TestPOSTWithObjectJSONBody;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
P: TPerson;
|
|
|
|
|
begin
|
|
|
|
|
P := TPerson.Create;
|
|
|
|
|
try
|
|
|
|
|
P.FirstName := 'Daniele';
|
|
|
|
|
P.LastName := '<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>';
|
|
|
|
|
P.DOB := EncodeDate(1979, 1, 1);
|
|
|
|
|
P.Married := true;
|
2014-05-14 15:55:41 +02:00
|
|
|
|
try
|
2015-04-01 17:01:23 +02:00
|
|
|
|
r := RESTClient.Accept(TMVCMimeType.APPLICATION_JSON)
|
|
|
|
|
.doPOST('/objects', [], Mapper.ObjectToJSONObject(P));
|
2014-05-14 15:55:41 +02:00
|
|
|
|
except
|
|
|
|
|
Fail('If this test fail, check http://qc.embarcadero.com/wc/qcmain.aspx?d=119779');
|
|
|
|
|
{ WARNING!!! }
|
|
|
|
|
{
|
|
|
|
|
If this test fail, check
|
|
|
|
|
http://qc.embarcadero.com/wc/qcmain.aspx?d=119779
|
|
|
|
|
}
|
|
|
|
|
raise;
|
|
|
|
|
|
|
|
|
|
end;
|
2013-11-18 00:44:40 +01:00
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
2015-02-16 14:25:09 +01:00
|
|
|
|
P := Mapper.JSONObjectToObject<TPerson>(r.BodyAsJsonObject);
|
2013-11-18 00:44:40 +01:00
|
|
|
|
try
|
|
|
|
|
CheckEquals('Daniele', P.FirstName);
|
|
|
|
|
CheckEquals('<27><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>', P.LastName);
|
|
|
|
|
CheckEquals(true, P.Married);
|
|
|
|
|
CheckEquals(EncodeDate(1979, 1, 1), P.DOB);
|
|
|
|
|
finally
|
|
|
|
|
P.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
procedure TServerTest.TestPOSTWithParamsAndJSONBody;
|
|
|
|
|
var
|
2013-11-05 14:57:50 +01:00
|
|
|
|
r: IRESTResponse;
|
2014-05-14 15:55:41 +02:00
|
|
|
|
JSON: TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
2014-05-14 15:55:41 +02:00
|
|
|
|
JSON := TJSONObject.Create;
|
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
|
|
|
|
r := RESTClient.doPOST('/echo', ['1', '2', '3'], JSON);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
CheckEquals('clientdata', r.BodyAsJsonObject.Get('client').JsonValue.Value);
|
|
|
|
|
CheckEquals('from server', r.BodyAsJsonObject.Get('echo').JsonValue.Value);
|
|
|
|
|
end;
|
|
|
|
|
|
2014-04-01 00:02:31 +02:00
|
|
|
|
procedure TServerTest.TestProducesConsumesWithWrongAcceptHeader;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2015-02-16 14:25:09 +01:00
|
|
|
|
res := RESTClient.Accept('text/plain') // action is waiting for a accept: application/json
|
|
|
|
|
.ContentType('application/json').doPOST('/testconsumes', [], TJSONString.Create('Hello World'));
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, res.ResponseCode);
|
2014-04-01 00:02:31 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure TServerTest.TestProducesConsumes01;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
|
res := RESTClient.Accept('application/json').ContentType('application/json')
|
|
|
|
|
.ContentEncoding('utf-8').doPOST('/testconsumes', [], TJSONString.Create('Hello World'));
|
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
2013-11-09 14:22:11 +01:00
|
|
|
|
CheckEquals('"Hello World"', res.BodyAsJsonValue.ToString);
|
|
|
|
|
CheckEquals('application/json', res.GetContentType);
|
2014-04-10 13:56:23 +02:00
|
|
|
|
CheckEquals('utf-8', res.GetContentEncoding);
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestProducesConsumes02;
|
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2015-04-01 17:01:23 +02:00
|
|
|
|
res := RESTClient.Accept('text/plain').ContentType('text/plain').doPOST('/testconsumes', [],
|
|
|
|
|
'Hello World');
|
2013-11-09 14:22:11 +01:00
|
|
|
|
CheckEquals('Hello World', res.BodyAsString);
|
|
|
|
|
CheckEquals('text/plain', res.GetContentType);
|
|
|
|
|
CheckEquals('UTF-8', res.GetContentEncoding);
|
2014-04-01 00:02:31 +02:00
|
|
|
|
|
2015-04-01 17:01:23 +02:00
|
|
|
|
res := RESTClient.Accept('text/plain').ContentType('application/json')
|
|
|
|
|
.doPOST('/testconsumes', [], '{"name": "Daniele"}');
|
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, res.ResponseCode);
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
procedure TServerTest.TestPUTWithParamsAndJSONBody;
|
|
|
|
|
var
|
2013-11-05 14:57:50 +01:00
|
|
|
|
r: IRESTResponse;
|
2014-05-14 15:55:41 +02:00
|
|
|
|
JSON: TJSONObject;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
begin
|
2014-05-14 15:55:41 +02:00
|
|
|
|
JSON := TJSONObject.Create;
|
|
|
|
|
JSON.AddPair('client', 'clientdata');
|
|
|
|
|
r := RESTClient.doPUT('/echo', ['1', '2', '3'], JSON);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
CheckEquals('clientdata', r.BodyAsJsonObject.Get('client').JsonValue.Value);
|
|
|
|
|
CheckEquals('from server', r.BodyAsJsonObject.Get('echo').JsonValue.Value);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TServerTest.TestReqWithParams;
|
|
|
|
|
var
|
|
|
|
|
r: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
r := RESTClient.doGET('/unknownurl/bla/bla', []);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode, '/unknownurl/bla/bla');
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params/', []);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode, '/req/with/params/');
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params', []);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode, '/req/with/params');
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doGET('/req/with/params', ['1', '2', '3']);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, r.ResponseCode);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
CheckEquals('1', r.BodyAsJsonObject.Get('par1').JsonValue.Value);
|
|
|
|
|
CheckEquals('2', r.BodyAsJsonObject.Get('par2').JsonValue.Value);
|
|
|
|
|
CheckEquals('3', r.BodyAsJsonObject.Get('par3').JsonValue.Value);
|
|
|
|
|
CheckEquals('GET', r.BodyAsJsonObject.Get('method').JsonValue.Value);
|
|
|
|
|
|
|
|
|
|
r := RESTClient.doPOST('/req/with/params', ['1', '2', '3']);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doPUT('/req/with/params', ['1', '2', '3']);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.NotFound, r.ResponseCode);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
|
|
|
|
|
r := RESTClient.doDELETE('/req/with/params', ['1', '2', '3']);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, r.ResponseCode);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
CheckNull(r.BodyAsJsonObject);
|
|
|
|
|
end;
|
|
|
|
|
|
2015-02-16 14:25:09 +01:00
|
|
|
|
procedure TServerTest.TestSerializationType;
|
|
|
|
|
var
|
|
|
|
|
LResp: IRESTResponse;
|
|
|
|
|
LPersonProps, LPersonFlds: TPerson;
|
|
|
|
|
LObj: TObject;
|
|
|
|
|
begin
|
|
|
|
|
LResp := RESTClient.doGET('/people', ['1']);
|
|
|
|
|
LPersonProps := Mapper.JSONObjectToObject<TPerson>(LResp.BodyAsJsonObject);
|
|
|
|
|
try
|
|
|
|
|
LResp := RESTClient.doGET('/people', ['1', 'asfields']);
|
|
|
|
|
LObj := Mapper.JSONObjectFieldsToObject(LResp.BodyAsJsonObject);
|
|
|
|
|
try
|
|
|
|
|
CheckEquals('BusinessObjectsU.TPerson', LObj.QualifiedClassName);
|
|
|
|
|
LPersonFlds := TPerson(LObj);
|
|
|
|
|
CheckTrue(LPersonFlds.Equals(LPersonProps),
|
|
|
|
|
'Object tranferred using field serialization is different from the object serialized in the default way');
|
|
|
|
|
finally
|
|
|
|
|
LObj.Free;
|
|
|
|
|
end;
|
|
|
|
|
finally
|
|
|
|
|
LPersonProps.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 01:09:09 +01:00
|
|
|
|
procedure TServerTest.TestSession;
|
|
|
|
|
var
|
2013-11-05 14:57:50 +01:00
|
|
|
|
c1: TRESTClient;
|
2013-10-30 01:09:09 +01:00
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
2014-02-24 10:20:34 +01:00
|
|
|
|
c1 := TRESTClient.Create('localhost', 9999);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
try
|
|
|
|
|
c1.Accept(TMVCMimeType.APPLICATION_JSON);
|
|
|
|
|
c1.doPOST('/session', ['daniele teti']); // imposto un valore in sessione
|
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
|
|
|
|
CheckEquals('"daniele teti"', res.BodyAsString);
|
|
|
|
|
c1.Accept(TMVCMimeType.TEXT_PLAIN);
|
|
|
|
|
res := c1.doGET('/session', []);
|
|
|
|
|
// rileggo il valore dalla sessione
|
|
|
|
|
CheckEquals('daniele teti', res.BodyAsString);
|
|
|
|
|
|
|
|
|
|
// aggiungo altri cookies
|
|
|
|
|
res := c1.doGET('/lotofcookies', []); // rileggo il valore dalla sessione
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckEquals(HTTP_STATUS.OK, res.ResponseCode);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
c1.Accept(TMVCMimeType.TEXT_PLAIN);
|
|
|
|
|
res := c1.doGET('/session', []); // rileggo il valore dalla sessione
|
|
|
|
|
CheckEquals('daniele teti', res.BodyAsString);
|
|
|
|
|
finally
|
|
|
|
|
c1.Free;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
procedure TBaseServerTest.DoLoginWith(UserName: string);
|
2013-10-30 01:09:09 +01:00
|
|
|
|
var
|
|
|
|
|
res: IRESTResponse;
|
|
|
|
|
begin
|
|
|
|
|
res := RESTClient.doGET('/login', [UserName]);
|
2015-04-01 17:01:23 +02:00
|
|
|
|
CheckTrue(res.ResponseCode = HTTP_STATUS.OK, 'Login Failed');
|
2013-10-30 01:09:09 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
|
|
|
|
|
RegisterTest(TServerTest.Suite);
|
|
|
|
|
|
|
|
|
|
end.
|