delphimvcframework/unittests/general/Several/JSONRPCTestsU.pas

154 lines
3.9 KiB
ObjectPascal
Raw Normal View History

2017-09-24 19:42:25 +02:00
// ***************************************************************************
//
// Delphi MVC Framework
//
2022-01-04 15:44:47 +01:00
// Copyright (c) 2010-2022 Daniele Teti and the DMVCFramework Team
2017-09-24 19:42:25 +02: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 JSONRPCTestsU;
interface
uses
DUnitX.TestFramework;
type
[TestFixture]
TTestJSONRPC = class(TObject)
public
[Test]
procedure TestRequestWithArrayParameters;
[Test]
2020-05-02 16:39:32 +02:00
procedure TestRequestWithNamedParameters;
[Test]
procedure TestRequestWithMixedParamaters;
[Test]
procedure TestRequestWithNoParameters;
[Test]
procedure TestRequestWithMalformedJSON;
[Test]
procedure TestNotificationWithNoParameters;
2017-09-24 19:42:25 +02:00
end;
2017-09-24 19:42:25 +02:00
implementation
{ TJSONRPCTest }
uses MVCFramework.JSONRPC;
procedure TTestJSONRPC.TestNotificationWithNoParameters;
2017-09-24 19:42:25 +02:00
var
2019-01-08 12:48:27 +01:00
lReq: IJSONRPCRequest;
2017-09-24 19:42:25 +02:00
begin
lReq := TJSONRPCRequest.Create; // FromString('{"jsonrpc": "2.0", "method": "subtract"}');
2019-01-08 12:48:27 +01:00
lReq.Method := 'subtract';
Assert.AreEqual(TJSONRPCRequestType.Notification, lReq.RequestType);
Assert.AreEqual(0, lReq.Params.Count);
Assert.AreEqual('subtract', lReq.Method);
end;
2017-09-24 19:42:25 +02:00
procedure TTestJSONRPC.TestRequestWithArrayParameters;
var
2019-01-08 12:48:27 +01:00
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
2019-01-08 12:48:27 +01:00
lReq.Method := 'subtract';
lReq.Params.Add(42);
lReq.Params.Add(23);
lReq.RequestID := 1;
Assert.AreEqual(1, lReq.RequestID.AsInteger);
Assert.AreEqual(42, lReq.Params[0].AsInteger);
Assert.AreEqual(23, lReq.Params[1].AsInteger);
Assert.AreEqual('subtract', lReq.Method);
2017-09-24 19:42:25 +02:00
end;
procedure TTestJSONRPC.TestRequestWithMalformedJSON;
begin
Assert.WillRaise(
procedure
var
2019-01-08 12:48:27 +01:00
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
2019-01-08 12:48:27 +01:00
lReq.AsJSONString := '{"jsonrpc": "2.0", this is wrong}';
end, EMVCJSONRPCParseError);
end;
2020-05-02 16:39:32 +02:00
procedure TTestJSONRPC.TestRequestWithMixedParamaters;
var
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'subtract';
Assert.WillRaise(
procedure
begin
lReq.Params.AddByName('par1', 42);
lReq.Params.Add(42);
end, EMVCJSONRPCException);
lReq := TJSONRPCRequest.Create;
lReq.Method := 'subtract';
Assert.WillRaise(
procedure
begin
lReq.Params.Add(42);
lReq.Params.AddByName('par1', 42);
end, EMVCJSONRPCException);
end;
procedure TTestJSONRPC.TestRequestWithNamedParameters;
var
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
lReq.Method := 'subtract';
lReq.Params.AddByName('par1', 42);
lReq.Params.AddByName('PAR2', 23);
lReq.RequestID := 1;
Assert.AreEqual(1, lReq.RequestID.AsInteger);
Assert.AreEqual('par1', lReq.Params.ItemsName[0]);
Assert.AreEqual('par2', lReq.Params.ItemsName[1]);
Assert.AreEqual('subtract', lReq.Method);
end;
procedure TTestJSONRPC.TestRequestWithNoParameters;
var
2019-01-08 12:48:27 +01:00
lReq: IJSONRPCRequest;
begin
lReq := TJSONRPCRequest.Create;
2019-01-08 12:48:27 +01:00
lReq.AsJSONString := '{"jsonrpc": "2.0", "method": "subtract", "id": 1}';
Assert.AreEqual(1, lReq.RequestID.AsInteger);
Assert.AreEqual(0, lReq.Params.Count);
Assert.AreEqual('subtract', lReq.Method);
Assert.AreEqual(TJSONRPCRequestType.Request, lReq.RequestType);
end;
2017-09-24 19:42:25 +02:00
initialization
TDUnitX.RegisterTestFixture(TTestJSONRPC);
finalization
end.