mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-16 16:25:54 +01:00
110 lines
2.9 KiB
ObjectPascal
110 lines
2.9 KiB
ObjectPascal
// ***************************************************************************
|
|
//
|
|
// Delphi MVC Framework
|
|
//
|
|
// Copyright (c) 2010-2020 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.
|
|
//
|
|
// *************************************************************************** }
|
|
|
|
unit JSONRPCTestsU;
|
|
|
|
interface
|
|
|
|
uses
|
|
DUnitX.TestFramework;
|
|
|
|
type
|
|
|
|
[TestFixture]
|
|
TTestJSONRPC = class(TObject)
|
|
public
|
|
[Test]
|
|
procedure TestRequestWithArrayParameters;
|
|
[Test]
|
|
procedure TestRequestWithNoParameters;
|
|
[Test]
|
|
procedure TestRequestWithMalformedJSON;
|
|
[Test]
|
|
procedure TestNotificationWithNoParameters;
|
|
end;
|
|
|
|
implementation
|
|
|
|
{ TJSONRPCTest }
|
|
|
|
uses MVCFramework.JSONRPC;
|
|
|
|
procedure TTestJSONRPC.TestNotificationWithNoParameters;
|
|
var
|
|
lReq: IJSONRPCRequest;
|
|
begin
|
|
lReq := TJSONRPCRequest.Create; // FromString('{"jsonrpc": "2.0", "method": "subtract"}');
|
|
lReq.Method := 'subtract';
|
|
Assert.AreEqual(TJSONRPCRequestType.Notification, lReq.RequestType);
|
|
Assert.AreEqual(0, lReq.Params.Count);
|
|
Assert.AreEqual('subtract', lReq.Method);
|
|
end;
|
|
|
|
procedure TTestJSONRPC.TestRequestWithArrayParameters;
|
|
var
|
|
lReq: IJSONRPCRequest;
|
|
begin
|
|
lReq := TJSONRPCRequest.Create;
|
|
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);
|
|
end;
|
|
|
|
procedure TTestJSONRPC.TestRequestWithMalformedJSON;
|
|
begin
|
|
Assert.WillRaise(
|
|
procedure
|
|
var
|
|
lReq: IJSONRPCRequest;
|
|
begin
|
|
lReq := TJSONRPCRequest.Create;
|
|
lReq.AsJSONString := '{"jsonrpc": "2.0", this is wrong}';
|
|
end, EMVCJSONRPCParseError);
|
|
end;
|
|
|
|
procedure TTestJSONRPC.TestRequestWithNoParameters;
|
|
var
|
|
lReq: IJSONRPCRequest;
|
|
begin
|
|
lReq := TJSONRPCRequest.Create;
|
|
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;
|
|
|
|
initialization
|
|
|
|
TDUnitX.RegisterTestFixture(TTestJSONRPC);
|
|
|
|
finalization
|
|
|
|
end.
|